Professional Documents
Culture Documents
The ListBox control displays a list of items from which we can make a selection. We can
select one or more than one of the items from the list. The ListBox control is based on the
ListControl class which is based on the Control class. The image below displays a
ListBox.
Notable property in the Data section of the Properties window is the Items property. The
Items property allows us to add the items we want to be displayed in the list box. Doing
so is simple, click on the ellipses to open the String Collection Editor window and start
1
entering what you want to be displayed in the ListBox. After entering the items click OK
and doing that adds all the items to the ListBox.
ListBox Event
The default event of ListBox is the SelectedIndexChanged which looks like this in code:
Drag a TextBox and a ListBox control to the form and add some items to the ListBox
with it's items property.
Items in a ListBox are referred by index. When items are added to the ListBox they are
assigned an index. The first item in the ListBox always has an index of 0 the next 1 and
so on.
When you run the code and select an item from the ListBox, it's index is displayed in the
textbox.
Add a Button to the form and place the following code in it's click event.
2
When you run the code and click the Button it will display the number of items available
in the ListBox.
When you run the code and click an item in the ListBox that item will be displayed in the
TextBox.
You can remove all items or one particular item from the list box.
ComboBox
3
ComboBox is a combination of a TextBox and a ListBox. The ComboBox displays an
editing field (TextBox) combined with a ListBox allowing us to select from the list or to
enter new text. ComboBox displays data in a drop-down style format. The ComboBox
class is derived from the ListBox class. Below is the Image of a ComboBox.
The DropDownStyle property in the Appearance section of the properties window allows
us to set the look of the ComboBox. The default value is set to DropDown which means
that the ComboBox displays the Text set by it's Text property in the Textbox and displays
it's items in the DropDownListBox below. Setting it to simple makes the ComboBox to
be displayed with a TextBox and the list box which doesn't drop down. Setting it to
DropDownList makes the ComboBox to make selection only from the drop down list and
restricts you from entering any text in the textbox.
We can sort the ComboBox with it's Sorted property which is set to False by Default.
ComboBox Event
The default event of ComboBox is SelectedIndexChanged which looks like this in code:
4
Working with ComboBoxes
Drag a ComboBox and a TextBox control onto the form. To display the selection made in
the ComboBox in the Textbox the code looks like this:
You can remove all items or one particular item from the list box part of the
ComboxBox. Code to remove a particular item by it's Index number looks like this:
The Windows Forms Tree View Control helps to display the hierarchy of nodes that
can be used to represent the organization structure, file system or any other system
which includes hierarchical representation. For each node added in the hierarchy,
user can add a child node to it or a sibling node to it provided there is a parent node
for the selected node present. we explore the Tree View Control and depicts the way
as how to add a Child node or a sibling node to the selected node.
5
Case 1: How to add a Child node to a selected node
Create a tree node object and then add it to the selected node in the control.
To add a sibling to a selected node (provided it has a parent node) Below code
implements this concept:
treeView1.SelectedNode.Remove()
6
Case 4: Use of context Menu
All the above three cases can be performed via use of context menu also.
Thus a user can use this control for displaying the hierarchical structures