Creating a Simple C# Windows Forms Application Using Visual Studio 2010


Today, I am going to talk about how to create a C# windows application using Visual Studio 2010.  To do this, you will need the following:

  1. A PC running Windows
  2. An installed copy of Microsoft Visual Studio 2010.  (Click here to get the free edition directly from Microsoft.)

Creating a Visual Studio project

To begin, open Visual Studio and select from the menu bar File->New->Project.  In the New Project dialog box, you will see a tree view of the Installed Templates that are available.  Choose Visual C# -> Windows, then select Windows Forms Application in the center.  In the Name textbox below, enter the name, “CSharpWinFormsAppDemo1″.  I recommend that you create a new folder under the Visual Studio 2010 folder called “Demos” and change the Location textbox to create the project there.  Separating your real projects from “learning” projects can be useful in the long run. The solution option to “Create a new solution” should be left alone. Finally, the solution name should already be the same as your project name, so that also should be left alone.

You can read more about creating projects and solutions here.

Solution Explorer

Once this is completed, you should see your solution, “CSharpWinFormsAppDemo1″, in the Solution Explorer which is a tree view of your solution and all of its projects. Under this is your project, “CSharpWinFormsAppDemo1″.  Under that are three items, Properties, References, Form1.cs, and Program.cs.  The contents of the Form1.cs file are what we are concerned with for the moment and that should be open in a tabbed document in the middle of the screen.

You can read more about Solution Explorer here .

Form1.cs [Design]

If you select Form1.cs, a tab marked “Form1.cs” appears in the middle of the window that displays a simple Window form.  You may notice that the word “[Design]” is after the filename in the tab.  While it should be obvious, the purpose is to tell you that you are looking at the Design view of Form1.cs.  If you right-click outside the actual form, but within the tabbed area, you will get a context menu.  Click on “View Code” at the top.

Form1.cs

A new tab should appear marked simply “Form1.cs”.  This is the code view of this file.  You should already have eight standard “using” directives, a namespace declaration, and a partial class definition for Form1.  Using directives and namespace declarations work together to allow the compiler to recognize and use types defined in other files.

You may be curious about the “partial” keyword here.  It refers to the fact that Form1 is also defined in another file, “Form1.Designer.cs”.  If you expand the Form1.cs node in Solution Explorer, you will “Form1.Designer.cs” is listed below “Form1.cs”.  Normally, you shouldn’t bother with this file.  Most of it, namely the region marked “Windows Form Designer generated code” is generated automatically by Visual Studio.  If you attempt to change any of this code manually, Visual Studio will overwrite those changes.

Toolbox

As a fast introduction, all that we shall have our windows application do is display a simple message, “Hello, world!”, a familiar exercise for anyone has ever learned to program.  To accomplish this, first click on the tab marked “Form1.cs[Design]” to bring it to the front.  Now simply go to the Toolbox pane and drag a Label control located under the “Common Controls” category to the design view of Form1.  A small square with the word “label1″ should appear in the form.  Right-click on the word and select Properties.  In the Properties window, find the Text property and beside it change “label1″ to “Hello, world!”.

Building your project in Debug mode

Press F5 to build and debug your application.  At this point, a window will appear with the message “Hello, world!”.  Click on the X in the upper right corner and the window will automatically close.  Since everything went according to plan, you are done.  The application is a success.

In order to get the executable file, you will need to go into the folder where your project is located.  Right click on the project name in the Solution Explorer and click on “Open Folder in Windows Explorer”.  You should see folders named “bin”, “obj”, and “Properties”.  You should also see several other files.  Those aren’t important right now.  Double click on the bin folder.  Inside this folder are the binaries created during the build process.

You should see a “Debug” folder.  If you checked other bin folders, you may see other folders, particularly one named “Release”.  These names correspond to the build configuration.  Since the default build configuration for debugging is “Debug”, this is why you see a Debug folder.  Unless you build using a Release configuration, you won’t see a Release folder at this point.  Open the Debug folder by double clicking on it.

You should now see four files.  The only one that you are concerned with at this time is the file named ”CSharpWindFormsAppDemo1.exe”.  This is the executable file that you just build.  Although you would not normally distribute this file, you could easily copy this file and give it to all your friends and they could have the same “Hello, world!” application that you have.  To verify that it does work, double-click on this file and see what happens.  You should have seen the same window that popped up when you hit F5 in Visual Studio.

Building your project in Release mode

Go back to Visual Studio and you will see the word “Debug” as an item in a pulldown menu next to a green arrow.  Click on this menu and you will see a few choices that include “Debug”, “Release”, and “Configuration Manager”.  Select “Release” and hit F5.  As before, a window should appear with the familiar “Hello, world!” message.  Again, right-click on the project in Solution Explorer and select “Open Folder in Windows Explorer”.  Double click on the folder and you should now see two folders, “Debug” and “Release”.  Double click on “Release”.  You should again see four files including one named ”CSharpWindFormsAppDemo1.exe”.  Double click on this file and again, the window containing the “Hello, world!” message appears.  This is the release build of the console application and the one that is normally distributed to other users.  The reason is that the Release version has code optimizations that complicate debugging.

You can read more about the differences between Debug and Release here.

Conclusion

Today, I discussed how to create a simple C# windows application using Visual Studio 2010.  This was a fairly trivial example, but it should provide a solid starting point for creating your own windows applications.

 

One response to “Creating a Simple C# Windows Forms Application Using Visual Studio 2010

  1. Pingback: Creating a Simple C# Windows Forms Application « According to Ed…

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s