Winforms C# in .Net Framework Visual studio 2010


I am going to discuss how to create simple window form in .NET framework. Let’s start tour of simple window form creation.
Go To->VS2010->File->New->Project.

.Net FrameWork
 Then-> Go To->Visual C# ->Window Form application and then enter your application name in textbox.

Window Form
 Now here you can see default Form1.cs created in application. In c# cs means C sharp.
Window Form C#

Now I am explain what is the use of  program.cs  file.
program.cs: This is a class file which contains code for execution of application. This file is main execution entry point for the application.

Program.cs class

In Program.cs file you can see three line of code which is automatically generated.
Here I am explain about   Application.Run() method of application class.
Application.Run(Form mainForm)-> This is a static method of application class which accept one parameter type form class. So here we can pass only form name as a parameter. This method start form which we have passed in it.
Purpose of this method runs you application.

In The program .cs class you can write own code which you need to execute before starting of window form. For example you can write this code before starting window application.
Here I have just write code for get MAC address of system and stored it in another variable which is accessible through out all window form. I have just call SystemMACAddress() this method in Main method.
SystemMACAddress()-> This method  get Mac address of PC and stored it in the variable name MACAddress.
Now we can use this variable throughout in all window form where we need to MAC address.

 Now we go to the Form1.cs file which is located in solution explorer.
Form1.resx file


Window Form-> Form1.cs contains two file one is Form1.Designer.cs and second one is Form1.resx. Now we see the purpose of both file. Form1.Designer.cs-> Purpose of this class separate designer code from custom code. This contains code related to design of window form. Form1.resx->This file contains resource such as images etc. This is hold by window form locally.
After that we design form using tool box control. We need to just drag and drop control over window form.

ToolBox for design
Then we go to the Form1.cs code from here. You just right click anywhere in window application and select view code option or use shortcut F7. 
C# code window
Then we can see Form1.cs code here.
Form1.cs

Here we can see code like this. In this window we can write own code which you need to write. We can changes the name of form as per requirement.
Here we see class type is partial -> Partial means we can split the code for a single class in different parts.
Then we can see constructor name Form1->In this constructor InitializeComponent() called which is created in Form1.Designer.cs  class file. If we remove this method from here we are unable to start window form and get error.
Debug  .Net application
 Then we can see output look like this.
 
Getting MAC Address in C#



 Download SampleApplication from here.
MyDemoForWindowForm
In this blog we have seen how to create simple window form. Thanks for reading!!
 
 
Author

Priti Kumari

I am technical blogger.I blogs at www.c-Sharpcorner.com and https://aspdotnetmyblog.blogspot.in/.

No comments:

Post a Comment