In this article I will try to explain how to create a simple Windows application written in C# programming language. For editing and creating this application we will use Microsoft Visual Studio C# 2010 Express. Microsoft Visual Studio C# 2010 Express is a free development tool from Microsoft. To start creating Windows applications first you need to download and install Visual Studio C# 2010 Express from Microsoft site.
The idea for application is simple. We will create one application window with two controls on it: label and button. When user clicks on the button it should change the text of the label.
Start Visual Studio. Go to menu File and select New Project. New widow will open. Now select Windows From Application and name it HelloWorld. It should look like this:
Click OK. Visual studio will create a new application and you will see the empty form in work area. This form is the main window of your application. Now from the control Toolbox (located on the left in the Visual Studio) under the Common Controls tab select the Label control.
Drag the selected Label control into your empty form. Do the same for Button control. Now you should have a form with the two controls on it: Label and Button. These controls were automatically named by Visual studio label1 and button1. These are their names and they will be referenced in the code by their names. Your application form should now look like this:
Now right click on the Label control on the form and select Properties. Find the Text property and change its value to Say hello. Now do the same for the button only change the Text property value into Hello.
Double click on the Button control on the form. This will open code file for your form. This is where we will enter the code. When you double clicked on the button Visual Studio automatically created the empty function that will be executed when you click on the button. Visual Studio will name the function button1_Click.
Now enter the following code into the function:label1.Text = "Hello from the button";
This line of code will change the visible text on the label into Hello from the button. The whole function should now look like this:
- private void button1_Click(object sender, EventArgs e)
- label1.Text = "Hello from the button";
That is it. Save your application and test it. You can start application by pressing CTRL+F5 on the keyboard. When the application starts you can click on the button with caption Hello. This should trigger our code and change the text of the label from Say hello into Hello from the button. The finished application should look like this: