Monday, August 25, 2008

Simple Winform

Lets look at a better program now, which takes in some information about an employee and displays out the same. We plan to add the following Controls in our program.
  1. Lables

  2. TextBoxes

  3. ComboBox

  4. GroupBoxes

  5. RadioButons

  6. ListBox

  7. Button

These Controls are available in the Toolbox. If u don�t see the Toolbox just press Ctrl+Alt+X .We now have to drag the controls onto the form. When we do this VS.Net will automatically add some code to the InitializeComponent( ) method. Whenever we drag a control in the form, its reference is created first. Next the Location, Size, Text and TabIndex properties of the corresponding controls also get initialized.

In all we will have to drag 2 TextBoxes, 3 Lables, 1 ComboBox, 2 GroupBoxes, 2 RadioButtons, 1 ListBox, and 1 Button.

Open the �Properties� window and keep it pinned. Pinning the window down prevents auto hiding of the window, i.e. the window does not slide back when mouse pointer is not on it. To Pin the window down click the small pin present on the top right corner of the window. Whenever we click a control to highlight it, the Properties window shows that control�s properties.

We lhave made the following changes in the properties window

Control Change To
Form1 Text Employee
1st label Name lname
1st label Text Name
2nd label Name lage
2nd label Text Age
3rd label Name lsal
3rd label Text Salary
1st Text Box Name name
1st Text Box Name age
Combo Box Name sal
1st Group Box Name grpgenr
1st Group Box Text Gender
2nd Group Box Name grpexp
2nd Group Box Text Experience
List Box Name exp
1st Radio Button Name male
1st Radio Button Text Male
2nd Radio Button Name female
2nd Radio Button Text Female
Button Text OK
Button Name ok

We have kept the Text fields of both TextBoxes to null. This is because when we start the application nothing should be already written in the text boxes.

This is how our Form should look now


But then we have not yet added any items to the ListBox and ComboBox ! To do so, click on the small button near the Items (Collection) property in the Properties window of the ListBox and add the strings separated by paragraphs (or Enters).

This will make the VS.Net to add the following code in Form1.cs

this.exp.Items.AddRange ( new object [ ] { "Less than a year", "1 - 2 yrs",

"2 - 5 yrs", "more than 5 yrs" } ) ;

In the same manner, add items for the ComboBox. The following code gets added

this.sal.Items.AddRange ( new object [ ]

{"2500","3500","5000","10000","20000" }) ;

We can see that the items �2500� and �Less than a year� are already selected. This is because of the following Code

sal.SelectedIndex = 0 ;
exp.SelectedIndex = 0;

We should add these statements in the constructor after the call to the InitializeComponent( ) method.

Next we have added an event for the Button �OK�. To add an event, open the Properties window of this Button. Click on the button, which has an icon of a yellow lightning on it. This gives us a list of Events we can add for that control. Double click on the �Click� entry. This adds an event �ok_Click( )� to our application., and the following gets added in the InitializeComponent( ) method.

ok.Click += new System.EventHandler ( this.ok_Click ) ;

Such code gets added for each event we add. In this event we plan to display the information entered by our user.

protected void ok_Click (object sender, System.EventArgs e)
{

string str1,str = "Name: " + name.Text +"\n" ;
str += "Age: " + age.Text + "\n" ;
str += "Salary: " + sal.Text + "\n" ;
str += "Experience: " + exp.Text+ "\n" ;
if( male.Checked == true )

str1 = "male" ;

else

str1 = "female" ;

str += "Gender: " + str1 ;
MessageBox.Show( str,"Emp Information" ) ;

}

The code is pretty much self-explanatory. What we are doing here is nothing but concatenating our string object str every time with the information. The Text field will contain text entered by the user. In the end we display the whole string using the static Show( ) method of the class MessageBox . The first parameter is the text to be displayed while the second gives the caption of the message box.

Your Title