Home

Introduction to Microsoft Visual C#

 

Microsoft Visual C# Fundamentals

 

Startup

Microsoft Visual C# is a programming environment used to create graphical user interface (GUI) applications for the Microsoft Windows family of operating systems. It usually ships either by itself or as part of Microsoft Visual Studio. To use these lessons, you must have installed either Microsoft Visual C# 2008 Express Edition, Microsoft Visual C# 2008 Professional, or Microsoft Visual Studio 2008 (Professional). To get Microsoft Visual C# 2008 Express Edition, you can download it free from the Microsoft web site. After downloading it, you can install it.

From now on, unless specified otherwise, we will use the expressions "Microsoft Visual C#" or "Visual C#" to refer to Microsoft Visual C# 2008.

After installing it, to use Microsoft Visual C#, you must launch. To launch Microsoft Visual C# 2008 Express Edition, you can click Start -> (All) Programs -> Microsoft Visual C# 2008 Expression Edition:

Microsoft Visual C# 2008 Express Edition

If you are using Microsoft Visual C# 2008 Professional, to start it, on the task bar, you can click Start -> (All) Programs -> Microsoft Visual Studio 2008 -> Microsoft Visual Studio 2008:

Microsoft Visual Studio 2008 Professional

 

The Microsoft Visual C# Interface

Microsoft Visual C# presents itself as a series of tools used to assist you in creating computer programs. As a normal Windows application, it starts on top with a menu and some toolbars. It is also equipped with various windows, considered as tools, you will be using. Most of these tools are available or are functional only if you have primarily created or opened a project.

Practical LearningPractical Learning: Starting Microsoft Visual C#

  • To launch Microsoft Visual C#:
    If are using Microsoft Visual C# 2008 Express Edition, on the taskbar, click Start -> (All) Programs -> Microsoft Visual C# 2008 Express Edition
    If you are using Microsoft Visual Studio 2008, on the task bar, click Start -> (All) Programs -> Microsoft Visual Studio 2008 -> Microsoft Visual Studio 2008

The Studio Windows

 

The Toolbars

A toolbar is an object made of buttons. These buttons provide the same features you would get from the (main) menu, only faster. Under the main menu, the Integrated Development Environment (IDE) is equipped with the Standard toolbar. By default, the Standard toolbar is positioned under the main menu but you can position it anywhere else on the IDE. To move a toolbar, position the mouse on the dotted line on its left section. The mouse pointer would change into a cross:

Toolbar

Then click and drag away from that position:

Moving a Toolbar

In the same way, you can position the toolbar anywhere on the screen. You can also attach or "dock" it to one of the four sides of the IDE. When a toolbar is not docked to one side of the IDE, it is said to float. When a toolbar is floating, you can resize it by dragging one of its borders. If a toolbar is floating, to put it back to its previous position, you can double-click its title bar.

By default, when you start Microsoft Visual Studio, it is equipped with one toolbar: Standard. To get more toolbars, on the main menu, you can click View -> Toolbars and click the toolbar of your choice. You can also right-click any available toolbar or the main menu. This displays a list of all the available toolbars. Those that are currently opened have a check mark next to them.

A toolbar is equipped with buttons that could be unfamiliar. Just looking at one is not obvious. To know what a button is used for, you can position the mouse on top of it. A tool tip will come up and display for a few seconds.

In our lessons, each button on any toolbar will be named after its tool tip. This means that, if a tool tip displays "New", its button will be called the New button. If a tool tip displays "Exercises and Assignments", its button will be called the Exercises and Assignments button. If you are asked to click a button, position your mouse on different buttons until one displays the referred to name.

Some buttons present an arrow on their right side. This arrow represents a menu.

Like the menu, the toolbars can be customized. To customize the Standard toolbar by adding buttons to it, you can right-click anything on the main menu or the toolbar and click Customize... On the Customize dialog box, you can click the Commands tab. In the Categories list, you can click a category, such as Debug. In the Commands list, you can click and drag an item, position it somewhere in the Standard toolbar, and release the mouse. Here is an example:

Customize

When you have finished, you can click the Close button on the Customize dialog box

The Start Page

The Start Page is the first wide area that appears when Microsoft Visual Studio comes up. The section displays a title as Recent Projects. At any time, to display the Start Page:

  • You can click its tab on the left side just under the Standard toolbar
  • On the main menu, you can click View -> Other Windows -> Start Page
  • On the main menu, you can click Windows -> Start Page

If you have just installed Microsoft Visual Studio or have not previously opened a project, the Recent Projects section would be empty. Once you start creating and using projects, they display in the Recent Projects section by their names. Here is an example:

The Projects section of the Start Page

The middle section allows you to check new articles from Microsoft and partners directly from Visual Studio 2008 through an Internet connection.

Showing and Closing a Window

When you start or open a project, Microsoft Visual Studio 2008 makes some windows available. These are the most regularly used windows. If you think that one of them is not regularly used in your types of assignments, you can remove it from the screen. To hide a window:

  • You can click its Close button Close
  • You can click its title bar and click Hide

All of the windows you can use are listed in the View menu. Therefore, if a window is not displaying, you can click View on the main menu and click a window of your choice.

Auto Hiding a Window

When creating your applications, you will use a set of windows that each accomplishes a specific purpose. Some windows are represented with an icon but hide the rest of the body. To display such a window, you can position the mouse on it. This would expand the window:

Dockable Windows

If you expand a window, it would display a title bar with two buttons. One is called Auto Hide and the other is the classic Close button:

Auto Hide

If you expand a window but find out you don't need it any more, you can just move the mouse away from it. The window would return to its previous state. Based on this functionality, if you are working with a window and move the mouse away from it, it would retract. If you need it again, you would have to reopen it using the same technique. If you are going to work with a certain window for a while, you can keep it open even if you move the mouse away. To do this, you can click the Auto Hide button. If clicked, the Auto Hide button changes from pointing left to pointing down Auto-Hide.

Dockable Windows

By default, Visual Studio 2008 installs some windows to the left and some others to the right of the screen. You can change this arrangement if you want. To do this, expand a window, then click its title bar and start dragging. When you do this, the screen would display 5 buttons: one to each side and one in the middle:

Dockable Windows

To position a window on one side of the screen, drag it title bar to one of the four buttons on the sides.

You can dock a window only if it is dockable. To make sure that a window is dockable, you can right-click its title bar and click Dockable:

If you don't want the window to be dockable, you can right-click its title bar and click Floating.

Floating Windows

Most of the windows you will use are positioned on one side of the screen. If you want, you can have a window that stays on top of other window but cannot  be "glued" to one side. Such a window is said to float. To float a window, drag its title bar and release it somewhere in the middle of the screen but not on one of the previously mentioned button because, while dragging, if you release the mouse on one of the buttons, and if the window is dockable, it would assume the position of where you released the mouse.

If you don't want a window to be dockable and you only want it to float, right-click its title bar and click Floating.

Tabbing a Window

Instead of accessing a window from one side of the screen or from its sharing an area with another window, you can make it display a tab. To do this, drag its title bar and release the mouse when its gets to the middle button that displays some tabs:

When a window is tabbed, you cannot drag its tab to position it on one side of the screen. If you want to remove it from its tabbing position, first right-click its tab and click either Floating or Dockable.

Coupling Windows

You can make two or more windows share one side of the screen or to share an area. To do this, drag its title bar to the window whose area you want to share, then position the mouse on the middle button and release it.

A Project

 

Introduction

To create a computer program, also called an application, you create a series of files and group them in an ensemble called a project. The project contains various types of files, such as code files, assemblies (or libraries), and resource files.

These lessons assume that you already know a good deal of the C# language, but only the language. We assume that you have no prior knowledge of graphical application programming. In these lessons, we will create Windows applications, the type referred to as graphical user interface (GUI).

To create most of the files you need for an application, you don't need Microsoft Visual C#. In fact, you can use Notepad to create a completely functional application. All you need is to have the whole .NET Framework, which is freely downloadable from the Microsoft web site. When using this approach, you can use Notepad (or any other text editor) to write your code, save each document appropriately in the folder you would have designated.

Creating a Project

A typical application consists of more than one code file and can even be as complex as you want. To make it faster and a little easier to graphically create an application, you would need a good functioning environment like Microsoft Visual C#. Using it, you can create a new project or you can open an existing one.

To create a Visual C# project, you can display the New Project dialog box. To open the New Project dialog box:

  • On the Start Page, on the right side of Project, click Create...
  • If you are using Microsoft Visual C# 2008 Express Edition, on the main menu, you can click File -> New Project... If you are using Microsoft Visual C# 2008 Professional, on the main menu, you can click File -> New -> Project...
  • On the Standard toolbar, you can click the New Project button New Project
  • You can press Ctrl + Shift + N

In the New Project dialog box, select Visual C# Projects, select the type of project, give it a name, specify its directory, and click OK.

Practical LearningPractical Learning: Creating a Project

  1. On the main menu, click File -> New Project or File -> New -> Project...
  2. In the Project Types list, expand the Visual C# node and click Windows
  3. In the Templates section, click Empty Project
  4. In the Name edit box, type Exercise1
     
    New Project
  5. Accept the name in the  Location text box and click OK. This creates a new project
  6. On the main menu, click Project -> Add New Item...
  7. In the Templates list, click Code File
  8. Change the Name to Exercise and
     
    Add New Item
  9. Click Add
  10. In the empty file, type the following:
     
    using System;
    
    public class Exercise
    {
        public static int Main(string[] args)
        {
            return 0;
        }
    }
  11. Save the file

Compiling and Executing a Project

The instructions created for a Visual C# project are written in plain English in a language easily recognizable to the human eye. After creating the file(s) of a project, you would compile the project to get an executable that becomes ready to be distributed to your users.

To compile and execute a project in one step, on the main menu, you can click Debug -> Start Without Debugging. Although there are other techniques or details in compiling (or debugging) and executing a project, for now, this is the only technique we will use until further notice.

Practical LearningPractical Learning: Executing a Project

  1. To execute the application, on the main menu, click Build -> Build Exercise1

  2. To execute the application, on the Standard toolbar, click the Start Debugging button Start Debugging

Opening a Project

As opposed to creating a new project, you can open a project that either you or someone else created. To open an existing project:

  • On the Start Page, on the right side of Project, click Open...
  • If you are using Microsoft Visual C# 2008 Express Edition, on the main menu, you can click File -> Open Project... If you are using Microsoft Visual C# 2008 Professional, on the main menu, you can click File -> Open -> Project...
  • You can press Ctrl + Shift + O

This action would display the Open Project dialog box. This allows you to select a project and open it.

Using V arious Projects in the Same Solution

With Microsoft Visual Studio, you can add one project to another, instead of starting one anew. To add a project to an existing one:

  • On the main menu, you can click File -> Add -> New Project...
  • If more than one project exist already, in the Solution Explorer, you can right-click the top node (the name of the first project), position the mouse on Add, and click New Project...

Any of these actions would display the Add New Project dialog box. You can then select the type of project in the Templates list, give a name to the project, and click OK. In the same way, you can add as many projects as you judge necessary to your solution. When a solution possesses more than one project, the first node in the Solution Explorer becomes Solution 'ProjectName' (X Projects). The ProjectName represents the name of the first project and X represents the current number of projects.

When you are using more than one project in the same solution, one of the projects must be set as the startup. The project that is set as the startup has its name in bold characters in the Solution Explorer. You can change and use any project of your choice as the startup. To do this, in the Solution Explorer, you can right-click the desired project and click Set As StartUp Project.

When a solution possesses more than one project, you can build any project of your choice and ignore the others. To build one particular project, you can right-click it in the Solution Explorer and click Build.

The Code Editor

 

Introduction

There are two main ways you will manipulate an object of your application, visually or using code. In future sections, we will explore details of visually designing a control. As you should have found out from learning C#, code of an application is ASCII text-based, written in plain English and readable to human eyes. For an application, you can use any text editor to write your code but one of Visual Studio's main strengths is the code editor. It is very intuitive.

The Code Editor is a window specially designed for code writing.

Author Note Although all languages of the Visual Studio programming environment share the Code Editor, once you have started a type of application, the Code Editor is adapted to the language you are using. Its parser (a program used internally to analyze your code) behaves according to the language of your choice. The features and behaviors of the Code Editor are also different, depending on your language.

To display the code editor, in the Solution Explorer, you can click the View Code button View Code.

The Code Editor is divided in 4 sections:

The Code Editor
 

Practical LearningPractical Learning: Introducing the Code Editor

  1. To create a new class, on the main menu, click Project -> Add Class...
  2. Set the Name to Circle and click Add

The Tabs Bar

The top section of the Code Editor displays tabs of property pages. Each tab represents a file. To add a new file to the project, on the main menu, you can click

  • File -> New -> File...
  • Project -> Add New Item...

Once in the Add New Item dialog box, in the Templates section, click the type of file you want to create, type a name in the Name text box, and press Enter. After the file has been created, it is represented by a tab in the top section of the Code Editor. In the same way, you can add as many files as you judge them necessary. To access a tab:

  • You can click its name in the Tabs Bar
  • On the main menu, you can click Window and click the name of the desired tab

By default, the tabs display in the order their files were created or added to the project, from left to right. If you don't like that arrangement, click and drag its tab either left or right beyond the next tab

Practical LearningPractical Learning: Introducing the Code Editor

  1. To create a new class, on the main menu, click Project -> Add Class...
  2. Set the Name to Square and click Add
  3. To create a new class, on the main menu, click Project -> Add Class...
  4. Set the Name to Triangle  and click Add
  5. To access the Circle tab, on the main menu, click Window -> Circle.cs
  6. Change the file as follows:
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Exercise1
    {
        class Circle
        {
            private double rad;
    
            public double Radius
            {
                get { return rad; }
                set { rad = value; }
            }
    
            public double Area
            {
                get { return rad * rad * Math.PI; }
            }
        }
    
        class Sphere : Circle
        {
            public new double Area
            {
                get { return 4 * Radius * Radius * Math.PI; }
            }
        }
    
        class Cylinder : Circle
        {
            private double hgt;
    
            public double Height
            {
                get { return hgt; }
                set { hgt = value; }
            }
        }
    }
  7. To access another tab, in tabs section, click Triangle
  8. Change the file as follows:
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Exercise1
    {
        class Triangle
        {
            private double bas;
            private double hgt;
    
            public double Base
            {
                get { return bas; }
                set { bas = value; }
            }
            
            public double Height
            {
                get { return hgt; }
                set { hgt = value; }
            }
        }
    
        class Kite
        {
        }
    }
  9. Save all

The Types Combo Box

The top-left section of the Code Editor displays a combo box named Types. As its name indicates, the Types combo box holds a list of the types as classes and structures that were created in the current file. You can display the list if you click the arrow of the combo box:

Types

Each item of the Types combo box displays the name of its type associated with its parent as implemented in the code. The parent can be a class or a namespace. If you select a class in the list, the Code Editor jumps to that class and positions the caret at the beginning of the class' definition.

Practical LearningPractical Learning: Using the Types Combo Box

  1. Click the Circle tab
  2. In the Types combo box, select Exercise1.Circle

The Members Combo Box

The top-right section of the Code Editor displays a combo box named Members. The Members combo box holds a list of the members of classes. The content of the Members combo box depends on the item that is currently selected in the Types combo box. This means that, before accessing the members of a particular class, you must first select that class in the Types combo box. Then, when you click the arrow of the Members combo box, the members of only that class display:

Members

If you select an item from the Members combo box, the Code Editor jumps to that members and positions the cursor to the left of the member.

Practical LearningPractical Learning: Using the Members Combo Box

  1. In the Members combo box, select Area
  2. Press the up arrow key and add a Diameter property
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Exercise1
    {
        class Circle
        {
            private double rad;
    
            public double Radius
            {
                get { return rad; }
                set { rad = value; }
            }
    
            public double Diameter
            {
                get { return rad * 2; }
            }
    
            public double Area
            {
                get { return rad * rad * Math.PI; }
            }
        }
    
        . . . No Change
    }
  3. Save all

Code Colors

Code is written in a wide area with a white background. In that area, you use the keyboard to insert code with common readable characters. The Code Editor uses some colors to differentiate categories of words or lines of text. The colors used are highly customizable. To change the colors, on the main menu, you can click Tools -> Options... In the Options dialog box, in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items section, click the category. In the Item Foreground combo box, select the desired color. If you want the words of the category to have a colored background, click the arrow of the Item Background combo box and select one:

The Options Dialog Box

In both cases, the combo boxes display a fixed list of colors. If you want more colors, you can click a Custom button to display the Color dialog box that allows you to "create" a color.

Regions

When code of a file is long, it can be tiresome to scroll up and down. The Visual Studio .NET's Code Editor allows you to create sections that allow the code to behave like the tree arrangement of the left pane of Windows Explorer. This means that you can expand or collapse section of code. The Code Editor supports this feature automatically by adding + buttons at the beginning of the lines of sections that can be expanded or collapsed. This is the case for namespaces, classes, methods, interfaces, properties, etc. The end of an expandable section displays a - button to the beginning of the line.

Besides the default sections that the Code Editor is intuitively aware of, you can create your own region. A region must have a beginning and an end. To specify the start of a section, type #region. You can optionally add a label to the right of #region to name the region. After creating a region with #region, the Code Editor adds a + button to its left. To expand a region, you can click its + button. This changes it into a - button. To collapse the region, click the - button.

If you don't specify the end of the region, the code from #region to the end of the file would be considered as belonging to the to the region. Therefore, you should specify the end of the region you created. To mark the end of the region, in the desired line, type #endregion.

Here are examples of regions:

using System;

namespace GeometricFormulas
{	
	interface IGeometry
	{
		double Length { get; set; }
		double Height { get; set; }
	}
	
	class Square
	{
		#region Formulas to Calculate a Square's Perimeter and Area
		public double Perimeter(double side)
		{
			return side * 4;
		}

		public double Area(double side)
		{
			return side * side;
		}
		#endregion
	}

	class Trapezoid
	{
		private double _base1;
	}

	class Rectangle : IGeometry
	{
		private double len;
		private double hgt;

		public Rectangle()
		{
			this.len = 0.00;
			this.hgt = 0.00;
		}
	
		public Rectangle(double L, double H)
		{
			this.len = L;
			this.hgt = H;
		}

		#region IGeometry Members

		public double Length
		{
			get
			{
				// TODO:  Add Rectangle.Length getter implementation
				return len;
			}
			set
			{
				// TODO:  Add Rectangle.Length setter implementation
				len = value;
			}
		}

		public double Height
		{
			get
			{
				// TODO:  Add Rectangle.Height getter implementation
				return hgt;
			}
			set
			{
				// TODO:  Add Rectangle.Height setter implementation
				hgt = value;
			}
		}

		#endregion

		public double Perimeter
		{
			get { return (len + hgt) * 2; }
		}

		public double Area
		{
			get { return len * hgt; }
		}
	}

}

The Code Editor can help you specify the beginning and end of a region.

 
 

Previous Copyright © 2008-2009, yevol.com Next