How do I make my first “Hello, WinForm” .NET form application using VS.NET and Visual C#?
C# Version Using Visual Studio.NET to build the “Hello WinForm” Application So we have had a lot of Visual Studio.NET introduction. Now, let’s use Visual Studio.NET IDE to build the “Hello WinForm” application which we created earlier in the lesson.Creating a new Project First of all, we need to create a new C# Windows Application Project. For this, start Visual Studio.NET and click File -> New -> Project. It will show the following screen
[IMAGE1]
From the above screen, select ‘Visual C# Projects’ in Project types and ‘Windows Application’ in Templates. Write the name of the new project (‘LearningWinForm’ in the above figure) in the text box labeled Name. Select the disk path where you wish to store the project using Browse… Button and click OK. It will show you an empty form in the designer view similar to the figure below
[IMAGE2]
The layout of the form designer screen may be somewhat different from the one shown above. Your toolbox and properties window may be visible and some other windows may not be visible. You can change these settings using View menu as described earlier in the lesson.
Setting various properties of the form You can change the default properties of the form using the Properties window. For this, select (click) the form and select the properties window (If the properties window is not visible in the right hand pan, select View -> Properties Window). Now change the title of the form and the name of the form’s class in the code using Text and Form’s Name property respectively as shown in the following figure.
IMAGE3]
Adding Controls to the Form Now select the Label control from toolbox and place it on the form and resize it as appropriate. Select (click) the label on the form and from the properties window, set its Text property to “Hello WinForm” and the Name property to ‘lblGreeting’. The name of a control is the name of its corresponding instance in the source code. Now select the Button control from toolbox and place it on the form and resize it appropriately. Select (click) the button and set its Name property as ‘btnExit’ and Text property as ‘Exit’ from the properties window. The form should now look like
[IMAGE4]
Adding Event Handling Now, we need to add the event handling code for the Exit button. For this, simply double click the Exit button in the designer. It will create a new event handler for the Exit button’s Click event and take you to the source code as shown in the following figure
[IMAGE5]
Write the code to close the application (Application.Exit()) in the event handler. The IDE in fact has not only created the event handler but also has registered it with the Exit button’s Click event.
Executing the application That is it! The ‘Hello WinForm’ application is complete. To compile and execute the application, select Build -> Build Solution (or press Ctrl+Shift+B) and then select Debug -> Start Without Debugging (or press Ctrl+F5).
Author’s Note: Unfortunately, my copy of Visual Studio.NET 2001 does not change the name of form (from Form1 to MyForm in our case) in the call to Application.Run() method in the Main() method. I have to explicitly change the Main() method like this
static void Main()
{
Application.Run(new MyForm());
}
If you are also experiencing the same problem with your copy of Visual Studio.NET, make sure to change the name of the form in Main() method also whenever you change its name in the Properties Window in order to avoid the compilation error
This will compile and start the ‘Hello WinForm’ application in a new window as shown below
[IMAGE6]
To close the application, click the Exit button or the close button at the title bar of the window.
The code generated by the Form Designer You can toggle between Form Designer and Code using View -> Designer and View -> Code. After switching to the code, you will find the code generated by the form designer to be very similar to that we have written earlier in the lesson. To understand the code better, we recommend removing all the comments and region boundaries.
Index
Sponsored links
Check For Updates
Easily add update features to your applications. A complete .Net updating solution.
Easily add update features to your applications. A complete .Net updating solution.
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Online Crash Analysis
Automatically capture customer crash data, no debugger required. Support for .NET, C++, OS X, Java.
Automatically capture customer crash data, no debugger required. Support for .NET, C++, OS X, Java.
.Net Localization Tool Sisulizer
Localize your .Net apps: ResX and assemblies. Database, XML & HTML Help localization. Try Sisulizer!
Localize your .Net apps: ResX and assemblies. Database, XML & HTML Help localization. Try Sisulizer!
