115-c-sharp-listbox-and-combobox-example

C sharp listbox and c sharp combobox are among most popular visual controls in C# windows forms applications.

c_sharp_windows_forms_application

They are similar and they have similar proporties and methods. In this example, we will prepare one form with one listbox, one combobox and one command button on it.

form_with_listbox_and_comobobox

Now, let’s rename listBox1 to ltbxBox1, comboBox1 to cmbxBox1 and button1 to cmbtFill. We can also rename Text property of cmbtFill from button1 to Fill. On Form1_Load event we can clear both controls and add title to the form.

private void Form1_Load(object sender, EventArgs e)
{

this.Text = “ListBox and ComboBox example”;
ltbxBox1.Items.Clear();
cmbxBox1.Items.Clear();

}

note: ComboBox and listBox are visual controls similar to arraylists, and most of the properties are the same (such as Clear(), Add() and so on), but ComboBox and listBox requires property Item to be included before above mentioned methods are invoked. Property this regards to form itself.

Now, let’s fill our C# listbox and combobox from cmbtFill with following code:

for (int int_i = 1; int_i < 6; int_i++ )
{

ltbxBox1.Items.Add(int_i);

}
for (int int_i = 6; int_i < 11; int_i++)
{

cmbxBox1.Items.Add(int_i);

}

Problem with ComboBox is that it is visual control with editable content. In most cases we don’t want that, so it is practical to change DropDownStyle from default DropDown to DropDownList. Then we are safe from unwanted change of ComboBox content by user.

note: If we want program to take some action depending of ComboBox current content, following code can do the job.

private void cmbxBox1_SelectedIndexChanged(object sender, EventArgs e)
{

if( cmbxBox1.Text == “7” )
{

MessageBox.Show(cmbxBox1.Text, “Title”, MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

In Show method of MessageBox control, first string argument is message, second string argument is the title, third argument is button type, and fourth argument is icon type.

If we would like to change content of the listbox, on doubleclick event, we need to add code in events:

listbox_events_view

private void ltbxBox1_DoubleClick(object sender, EventArgs e)
{

try
{

ltbxBox1.Items[ltbxBox1.SelectedIndex] = “Hello World!”;

}
catch
{
}

}

listbox_items_changed

When items is changing, no additional method is used, apart from Items. If, however we want to remove Item, Remove method has to be used and in that case argument is not SelectedIndex but SelectedItem.

ltbxBox1.Items.Remove(ltbxBox1.SelectedItem);

Since ListBox is visual object, while working with databases, it is useful to have ArrayList that is non-visual object with the same content as ListBox to keep ID’s of some data from data table:

string selectedId = alContacts[ltbxBox1.SelectedIndex].ToString();

This will be explained more detailed in lesson about working with databases in C#.

External links:

C Sharp Listbox and C Sharp Combobox on Homeandlearn
C Sharp Listbox and C Sharp Combobox on Codeproject
C Sharp Listbox and C Sharp Combobox on Microsoft

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*