Windows Application - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Win App Combo Box

Windows Application Combo Box

shape Description

The combo box is a control which is used to display some list and also used for adding items. Simply Combo Box is a combination of TextBox Control and List Box Control. Combo Box Control have an event called "SelectedIndexChanged". When double clicked on the control, then it will raise SelectedIndexChanged event. Whatever needed to do after index changed, all that functionality have to write under this event.

Example

shape Steps

To see a simple example on combo box control follow the steps listed below. (i) Drag and drop the combo box control on to form as shown in the below figure. (ii) See the properties window.There are some important properties as listed below.
  • Items: This is a property which is very important for combo box control. This property is used to give the list of items which are displayed in the output.Following image shows how to enter the items to the combo box.
  • ValueMember Value Member is used to give an identity to the combo box. Value Member is mainly used while using SQL. Value member is used for reference."Where combo box is used" can be discussed in later chapters.
  • DisplayMember Display Member is a property which is used to set one of the fields from a list of the fields in a table that which we want to display in the combo box. Example : Take an employee table which contains fields like empId, empName and empAddress.When displaying a list of employee names in a combo box, then the display member is empName and Value member is empId.
(iii) Give the items list how you want to display as shown in the above figure. (iv) Press f5 to run the application. The output will be displayed as shown in the below figure.  

shape Example

Now, see a simple example on SelectedIndexChanged  event. Double click on the Combo box. Then, write the below code under SelectedIndexChanged event as shown below. [csharp] private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if(comboBox1.SelectedIndex==0) //which is the first item in the list { MessageBox.Show("Welcome to SPlessons PHP Tutorial");//displays a messagebox whenever you click on the first item } else if(comboBox1.SelectedIndex==1) { MessageBox.Show("Welcome to SPlessons ASP.NET Tutorial");//displays a messagebox whenever you click on the second item } else if (comboBox1.SelectedIndex == 2) { MessageBox.Show("Welcome to SPlessons Java Tutorial");//displays a messagebox whenever you click on the third item } } [/csharp] Press F5 and see the output as shown below after click on 'SPlessons PHP Tutorial'.     In later chapters, I will explain about 'How to bind a combo box using value member of another combo box'.