Creating Windows Applications in Visual Studio .NET Using C#
v0.2a, JL Popyack, Feb. 2004 (Revised July 2004)

 

Find and open Visual Studio .NET.  This should take you to the Start Page.  If your screen does not show the "Solution Explorer" visible (as shown here on the right side), select View->Solution Explorer to make it visible.  (In Visual Studio .NET, a "Solution" is a collection of files comprising source code and libraries to be compiled and linked. 

 

 

To Create a New Project with C#: You will use a project template to create your project.  As the online help explains, “a project template creates the initial files, references, code framework, property settings, and tasks appropriate for the selected project.”

To do this, select "File->New->Project…", which should bring up the following dialog:

 

Since you’re going to create a C# application, choose the “Visual C# Projects” item in the left pane, and choose the “Windows Application” item in the right pane.  Type in a name for your application – in this example, we have chosen “MyWindowsApplication” as a name – then click “OK”. 

 

A new solution should be created for you.  This will take a few moments.  The default location for Visual Studio .NET projects is in your “My Documents” folder, in a subfolder called “Visual Studio Projects”. 

 

 

When setup is completed, the application space should look like this:

 

The Solution Explorer at the right shows the files that have been added, which are needed for your application.  The file Form1.cs as shown in this picture contains code necessary to produce a window called Form1, which looks like the form shown in the left pane.  The left pane shows the Design View of the form, which is why the tab above it says “Form1.cs [Design]”. 

 

There are some icons at the top of the Solution Explorer window, just below the title bar.  To see the code that produces this window, select the leftmost one (if you place the cursor above this icon, the words “View Code” appear, as shown in the screen view below).  Clicking the icon causes a window to appear that shows the C# code used to produce the form

 

A new tab labeled “Form1.cs” has appeared for the window containing the actual C# code.  Scrolling through the code shows a few interesting features of the Visual Studio .NET/C# environment:

 - There are little boxes next to some lines of code that have “-” in them.  Clicking on one of those will collapse a section of the code, so that it may be hidden when you aren’t interested in looking at it.  For instance, click the box next to Line 35.  This should cause lines 35-46 to collapse with only the header line for the Dispose() routine still visible (and a “+“ sign in the box.)

[JP NOTE: ??? do line numbers show by default?  Check this with a new installation.???]

 

 

- [section deleted, “Creating Online Documentation”]

 

 

 

You can return to Design View either by clicking the “View Design” icon next to the “View Code” icon in the Solution Explorer window, or by clicking the “Form1.cs [Design]” tab in the left pane. 

 

- Notice that some of the code bears the designation “Windows Form Designer generated code”.  This code has been generated automatically from the Form Designer window.  To see the effects of this, find line 47 in the Code window, and click the “+” next to it.  Several lines are made visible, including the following:

 

private void InitializeComponent()

{

      //

      // Form1

      //

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

      this.ClientSize = new System.Drawing.Size(292, 266);

      this.Name = "Form1";

      this.Text = "Form1";

}

Now go back to Form Designer view, click on the form, and resize the form by dragging one of the resizing boxes. 

 

 

After resizing, go back to Code view and examine the lines of code generated by this form.  You should find the line corresponding to form size has been altered.

 

To add more GUI items to your application, click the “Toolbox” in the left margin of the application window.  (If the Toolbox is not visible, select View->Toolbox)  A set of applicable choices appears.  [NOTE: Make sure you are in Design view.  If you are in Code view, a very different set of choices appears.]

 

To add a button, click Button and while holding the mouse button depressed, drag it to the form. 

 

 

You may resize and reposition the button by clicking on it and/or dragging.

 

 

 

 

 

You can modify an object’s properties by clicking on it, right-clicking, and selecting “Properties” from the pop-up menu.  An assortment of properties and their values appears.

 

 

Change the text that appears in the button by selecting the item next to Text in the Properties window, and typing the desired text.

 

 

Further customizations are simple.  For instance, to change the background color of the text in the button, click the BackColor item, choose from the “Custom”, “Web”, or “System” pallets to find the desired color, and select it.

 

 

Many other customizations are possible, for instance the choice of Font attributes:

 

 

Likewise, the Form’s properties can also be changed

 

 

The code view shows the new values

 

 

To compile, link and run your program, select Build -> Build Solution, followed by

Debug -> Start Without Debugging .  This should make your form window appear, with its button.  The window may be moved, minimized, maximized, or closed in the usual way.  Because no semantic content has been given to the “Apologize!” button, nothing happens when you click it.