Sunday, 8 May 2011

Combo Box Control in C#

ComboBox Controls
The ComboBox control is used to save space on a dialog because the only part of the combo box that is permanently visible are the text box and button parts of the control. When the user clicks the arrow button to the right of the text box, a list box unfolds in which the user can make a selection. As soon as he or she does so, the list box disappears and the display returns to normal.
   As the name implies, a combo box combines a number of controls, to be specific the TextBox, Button, and ListBox controls. Unlike the ListBox, it is never possible to select more than one item in the list of items contained in a ComboBox and it is optionally possible to type new entries in the list in the TextBox part of the ComboBox.
                                                                          
ComboBox Properties
Name
Availability
Description
DropDownStyle Read/Write A combo box can be displayed with three different styles:
q DropDown: The user can edit the text box part of the control, and must click the arrow button to display the list part of the control.

q Simple: Same as DropDown, except that the list part of the control is always visible, much like a normal ListBox.

q DropDownList: The user cannot edit the text box part of the control, and must click the arrow button to display the list part of the control.
DroppedDown Read/Write Indicates whether the list part of the control is dropped down or not. If you set this property to true, the list will unfold.
Items Read-only This property is a collection, which contains all the items in the list contained in the combo box.
MaxLength Read/Write By setting this property to anything other than zero, you control the maximum number of characters it is possible to enter into the text box part of the control.
SelectedIndex Read/Write Indicates the index of the currently selected item in the list.
SelectedItem Read/Write Indicates the item that is currently selected in
the list.
SelectedText Read/Write Represents the text that is selected in the text box part of the control.
SelectionStart Read/Write In the text box part of the control, this property represents the index of the first character that
is selected.
SelectionLength Read/Write The length of the text selected in the text box part of the control.
Sorted Read/Write Set this property to true to make the control sort the items in the list portion alphabetically.
Text Read/Write If you set this property to null, any selection in the list portion of the control is removed. If you set it to a value, which exists in the list part of the control, that value is selected. If the value doesn't exist in the list, the text is simply shown in the text portion.
ComboBox Events
Name
Description
DropDown Occurs when the list portion of the control is dropped down.
SelectedIndexChanged Occurs when the selection in the list portion of the
control changed.
KeyDown, KeyPress, KeyUp These events occur when a key is pressed while the text portion of the control has focus. Please refer to the descriptions of the events in the text box section earlier in this chapter.
TextChanged Occurs when the Text property changes
ComboBox Text Example   In following image i have designed a window form for saving the address of user. Make design of your application in following image manner and change properties according to you.
                                                                           
In following figure for selecting country i have used ComboBox for selecting country and after that he/she will get the name of corresponding state name of that country he/she selected.
Code for Combobox1 to add the items in Combobox2. Write following code on Combobox1 selected index change

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            comboBox2.Enabled = true;

            if (comboBox1.SelectedItem.ToString() == "India")
            {
                comboBox2.Items.Add("NewDelhi");
                comboBox2.Items.Add("Chennai");
                comboBox2.Items.Add("Banglore");
                comboBox2.Items.Add("Mumbai");
            }
            else if (comboBox1.SelectedItem.ToString() == "Australia")
            {
                comboBox2.Items.Add("Sydney");
                comboBox2.Items.Add("Johnsberg");
                comboBox2.Items.Add("Perth");
                comboBox2.Items.Add("Melborn");
                
            }
            else if (comboBox1.SelectedItem.ToString() == "United States")
            {
                comboBox2.Items.Add("NewYork");
                comboBox2.Items.Add("Los Ageles");
                comboBox2.Items.Add("California");
                comboBox2.Items.Add("LosBegas");
              
            }
        }
                                                                           

After that choosing State name he/she will get the location in ComboBox 3. Like in following figure.
Code for ComboBox2 is given below write that code on Combobox2 Selected Index Changed

 private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox3.Items.Clear();
            comboBox3.Enabled = true;
            if (comboBox2.SelectedItem.ToString() == "NewDelhi")
            {
                comboBox3.Items.Add("East Delhi");
                comboBox3.Items.Add("West Delhi");
                comboBox3.Items.Add("North Delhi");
                comboBox3.Items.Add("South Delhi");
            }
            else if (comboBox2.SelectedItem.ToString() == "Chennai")
            {
                comboBox3.Items.Add("Chennai");
                
            }
            else if (comboBox2.SelectedItem.ToString() == "Mumbai")
            {
                comboBox3.Items.Add("Mumbai");
                comboBox3.Items.Add("Navi Mumbai");
            }
            else if (comboBox2.SelectedItem.ToString() == "Banglore")
            {
                comboBox3.Items.Add("Banglore");
                
            }
            else if (comboBox2.SelectedItem.ToString() == "Johnsberg")
            {
                comboBox3.Items.Add("Johnsberg");

            }
            else if (comboBox2.SelectedItem.ToString() == "Sydney")
            {
                comboBox3.Items.Add("Sydney");

            }

            else if (comboBox2.SelectedItem.ToString() == "Perth")
            {
                comboBox3.Items.Add("Perth");
            }


        }
Code for Save Button Click

private void button1_Click(object sender, EventArgs e)
        {
            string output;
            output = "Name: " + this.textBox1.Text + "\r\n";
            output += "Address : " + this.textBox2.Text + "\r\n";
            output += "          " + this.textBox3.Text + "\r\n";
            output += "          " + this.comboBox3.Text+ "\r\n";
            output += "          " + this.comboBox2.Text + "\r\n";
            output += "Country:  " + this.comboBox1.Text + "\r\n";
            textBox4.Text = output;
        }
Final you will get output Like this.

No comments:

Post a Comment