0% found this document useful (0 votes)
9 views3 pages

Practical - 3

Uploaded by

happylifehome924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Practical - 3

Uploaded by

happylifehome924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ComboBox Control

C# controls are located in the Toolbox of the development environment, and you
use them to create objects on a form with a simple series of mouse clicks and
dragging motions. A ComboBox displays a text box combined with a ListBox, which
enables the user to select items from the list or enter a new value

The user can type a value in the text field or click the button to display a drop down
list. You can add individual objects with the Add method. You can delete items with
the Remove method or clear the entire list with the Clear method

The following code stepwise illustrate the usage of ComboBox

private void Form1_Load(object sender, EventArgs e)


{
//First step

comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
//OR
this.comboBox1.Items.Add("A");
this.comboBox1.Items.Insert(0, "B");
this.comboBox1.Items.Insert(1, "C");
this.comboBox1.Items.Insert(2, "D");

//step no-5
this.comboBox2.Items.Add("Week");
this.comboBox2.Items.Add("Month");

}
private void button1_Click(object sender, EventArgs e)
{
//step no-3: this code will prevent empty space
if (this.textBox1.Text != "")
{
//Second step
// if TextBox is empty, then space will be added to
ComboBox after every click of Add button
this.comboBox1.Items.Add(this.textBox1.Text);
this.textBox1.Text = "";
this.textBox1.Focus();
}
}

private void removebtn_Click(object sender, EventArgs e)


{
// step no-4
this.comboBox1.Items.Clear();
// comboBox1.Items.Remove("A");

// this is used to remove selected items from ComboBox


// comboBox1.Items.Remove(comboBox1.SelectedItem);

private void comboBox2_SelectedIndexChanged(object sender,


EventArgs e)
{
// step no-6: the following code will add days of the week
or months of the year if we select week from ComboBox2
days of the week will be added automatically to ComboBox1
and so on.

comboBox1.Items.Clear();
if (this.comboBox2.SelectedItem == "Week")
// if (comboBox2.SelectedItem.Equals ("week") )

{
this.comboBox1.Items.Add("Sunday");
this.comboBox1.Items.Add("Monday");
this.comboBox1.Items.Add("Tuesday");
this.comboBox1.Items.Add("Wednesday");
this.comboBox1.Items.Add("Thursday");
this.comboBox1.Items.Add("Friday");
}
else if(comboBox2.SelectedItem =="Month")
// else if (comboBox2.SelectedItem.Equals ("Month") )

{
this.comboBox1.Items.Add ("January");
this.comboBox1.Items.Add ("February");
this.comboBox1.Items.Add ("March");
this.comboBox1.Items.Add ("April");
this.comboBox1.Items.Add ("May");
} }

-------------------------------------------------------------------------------------------------------------------------------

You might also like