How to Insert a ComboBox in C-Sharp
C-Sharp developers have the option to add ComboBox controls to forms as they build them or to generate ComboBoxes as needed. C-Sharp's "Controls.Add" method makes dynamic control generation possible. You may find this functionality useful when you need to collect information from a user on a form that has no ComboBox. By using the Controls.Add method, you can create a ComboBox, populate it with data and place it anywhere you like on the form as the program runs.
Instructions
-
-
1
Launch Microsoft Visual Studio and open any C-Sharp project. Move to the Solution Explorer and double-click the project's startup form. Visual Studio displays that form in its Design window.
-
2
Double-click the title bar at the top of the form. The form's "Load" method opens in the Code window. Add the code shown below to that method:
ComboBox combo1 = new ComboBox();
combo1.Name = "combo1";
combo1.Location = new Point(100, 200);The first two statements create a ComboBox named "combo1." The next statement sets the control's location on the form by creating a new point. That location is 100 pixels from the top and 200 pixels from the left, as shown in the call to the Point method.
-
-
3
Paste the following code after the code shown in the previous step:
string[] comboItems = new String[]
{
"Item 1", "Item 2", "Item 3"
};
combo1.Items.AddRange(comboItems);
combo1.Items.Add("Another Item");This first line of code creates a string array containing three items. The next statement uses the AddRange method to add those items to the newly created ComboBox. The final statement adds another item to the ComboBox using the Add method. Either of these methods work -- however, the AddRange method enables you to insert items quickly into a ComboBox when you have many to add.
-
4
Paste the code shown below after the last line of code described in the previous step:
combo1.SelectedIndexChanged += new EventHandler(combo1_SelectedIndexChanged);
this.Controls.Add(combo1);This first line defines an event handler named "combo1_SelectedIndexChanged." This event handler runs when users select one of the items in the ComboBox. The last line of code adds the new ComboBox to the form using the Controls.Add method.
-
5
Add this final block of code after the Load method:
void combo1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
string selectedText = comboBox.SelectedItem.ToString();
MessageBox.Show("You selected " + selectedText);
}This code determines the ComboBox's selected item and displays it in a popup message box.
-
6
Press F5. Visual Studio runs the code and creates a new ComboBox. Click the ComboBox's "Down" arrow, and then click one of the Items that appear in the drop-down menu. The event handler code runs and displays the item you selected.
-
1
Tips & Warnings
You don't have to create a new C-Sharp ComboBox when your form loads as in this example. Simply place the code shown in these steps inside any method. You can then call that method to create ComboBoxes as needed.
Use any name you like when naming ComboBoxes. Giving them names allows you to reference them later. The ComboBox's name is "combo1" in this example. Position a new ComboBox anywhere on the form by setting its Location property using the "new Point" statement described previously. For example, by determining where a user's cursor is, you can create a new ComboBox and position it in that location when the user clicks an object on the form.