Friday, October 14, 2011

C# Getting Started with Windows Program

After our first step programming console application in C#, now we are going to build our very first Windows form application. For this purpose I will use Microsoft Visual C# 2008 Express Edition.

Let's begin:
  • Open Microsoft Visual C# 2008 Express Edition.
  • Create new project. You can choose between these two methods:
    • From Start Page go to Recent Projects box and double click on link Create: Project... 
    • Choose menu File - New Project...
  • In the New Project dialog choose Windows Forms Application from installed templates, insert name of the application Helloworld, and press OK.

  • You will be provided with blank form of a standard Windows application. As you can see on the picture below, at the right hand side there are Solution Explorer and Properties views. If you don't have them, you can open them using menu View - Solution Explorer and View - Properties Window. On the left hand side you will see a toolbox contains Windows components.
 
  • Now drag 2 labels, 1 textbox, and 1 button from the toolbox and drop them onto your form. C# will create the code for you so far.
 
  • Let's make some adjustments to our form, you can use the Properties window. Properties of a selected component will be shown in this windows. We will customize all components used in our application. The result will look similar to the picture below.
    • Form1 component, change the size and set Text property to Hello, World!!!
    • Label1 component, change its Name to labelAsk and set Text property to Give me your name, please...
    • Label2 component, change its Name  to labelReply and adjust font as you like.
    • Textbox1 component, change its Name to textBoxName and adjust its size.
    • Button1 component, change its Name to buttonSayHello.
 
  • Now we will write down code that react if the Hello! is pressed. To make sure that our code is driven by click of that button, simply double click Hello! button in the form editor. C# will open the code editor for you and put the cursor on the newly created function/method that correspondent to Hello! button click. Usually the method has the name like buttonName_Click(). Now you can insert the code as you see below.
 
  • Save the project using menu File - Save All.
  • Your program is now ready. Build the project by choosing menu Build - Build Solution.
  • Run the application by selecting green triangle/play button on the upper toolbar or choosing menu Debug - Start Debugging.

 

Congratulations! This is your first Windows application. To improve your programming knowledge you can also browse another programming categories.

No comments:

Post a Comment