3. ListBox, ComboBox

ListBox, ComboBox, and CheckedListBox in Windows Forms

ListBox

A ListBox control displays a list of items from which the user can select one or more. You can add, select, or remove ListBox items programmatically.

ListBox Basics

Selecting an Item

You can specify the selected item index in the ListBox.

Example:

listBox1.SelectedIndex = 2; // The third item in the list becomes the selected item

ListBox DataSource

The DataSource property is used to set the source of the data that will be shown in the ListBox.

Example:

listBox.DataSource = dataSource; // dataSource is the source of data

Note: The DataSource cannot be used in conjunction with listBox.items for adding or deleting items.

Displaying Objects in ListBox

To display a specific property of an object in the ListBox, use the DisplayMember property.

Example:

listBox1.DisplayMember = "Name"; // The Name property of the object is displayed

A safer way to do this is using nameof to avoid typing errors:

listBox1.DisplayMember = nameof(Student.Name); // The Name property of the Student object is displayed

Multiple Selected Items

ListBox.SelectedItems property gives you access to all selected items in the ListBox.

CheckedListBox

A CheckedListBox control displays a ListBox with a CheckBox displayed next to each item. The user can select or deselect items.

CheckedListBox Basics

ComboBox

A ComboBox control is a combination of a TextBox and a ListBox. This control allows the user to select an item from a drop-down list or enter a new value.

ComboBox Basics

Code examples and more detailed explanations on these topics will be added as per the lesson plan. Markdown formatting will be used to structure the notes for clarity and easy navigation.

Reference

The content in this document is based on the original notes provided in Azerbaijani. For further details, you can refer to the original document using the following link:

Original Note - Azerbaijani Version