More Windows Controls and Standard Dialog Boxes
If you are new to VB.Net School
This is the 12th in the series of lessons in the VB.NET School. The VB.NET School is a kind of interactive learning platform where those who want to learn .NET with VB.NET can find help and support. With one issue a week, describing some areas of the VB.NET Programming Language with the Microsoft .Net Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated
message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author (
farazrasheed@acm.org) or the WEBMASTER (
info@programmersheaven.com).
For previous lessons click
here
Lesson Plan
In this installment we will learn about some more windows controls and standard dialog boxes. We will start out by looking at the collection controls, such as the List box, Combo box, Tree View and the List View control. Later we will learn about other common controls, including the main menu, image list, Toolbar and the Date Time Picker. Finally we will explore some of the standard dialog boxes, e.g. the Open File, Save File, Font and Color Dialog Boxes.
Collection Controls
Some of the windows controls contain some types of collection, such as names of books and images in a folder. These are called Collection controls. Examples of collection controls include the List Box, Combo Box, Tree View, List View, Toolbar, Image List and Main Menu. In this lesson we will explore these controls one by one.
List Box Control
A list box control contains a list of items which can be selected by the user. A list box can be set to allow the user to select one or more items. In .NET the list box is represented by the System.Windows.Forms.ListBox class. Each list box instance contains a collection called 'Items' that holds the items present in the list. An item (usually a string) can be added or removed from the list box.
The following screen shot presents a window containing a list box (#1) and a combo box (#2).
http://www.programmersheaven.com/articles/faraz/lesson12_img1.gif
A list box can be added to the form from the Visual Studio.NET toolbar. Usually a label is also added above the list box to serve as the title of the list box (e.g., The label 'Available Books' in the above picture). The Name property of the list box is used to set the name of the list box object in the code. The System.Windows.Forms.ListBox contains a property called 'Items' which provides access to the items contained in the list box. The Items collection can be used to read, add and remove items from the list box.
Adding items to the list box
A list box can be populated either at design time using the Visual Studio IDE or at runtime using code. To add items to the list box using the Visual Studio IDE, select the list box control in the designer and in the properties window click the Items property. It will show you an interface where you can enter the list of items to be added to the list box.
Alternatively, you can write code to add items to the list box. An item can be added to the list box using the Add() method from the Items collection of the ListBox object. Assume that we have made a list box to store the names of books and that we have set the Name property of the list box to 'lbxBooks'. The following code can be used to add items to the list box.
lbxBooks.Items.Add("Programming C#")
lbxBooks.Items.Add("Professional C#")
Or if you want to add a series of items, you can use the AddRange() method of the Items collection
lbxBooks.Items.AddRange(New String() _
{"Beginning C# by Wrox", _
"Professional C# by Wrox", _
"Programming C# by O' Reilly", _
"Professional ASP.Net", _
"Beginning Java 2 by Wrox", _
"C++ - The complete Reference", _
"Java Servlets Programming", _
"Java Server Pages - JSP)"})
Accessing items in the list box
The Items collection of the list box provides the default property that allows items in the list box to be accessed programmatically (through code).
lbxBooks.Items(0) = "Program Development in Java"
' changing list box item
Dim book1 as String = CStr(lbxBooks.Items(1))
' reading list box item
In the above code, the first statement uses the default property of the Items collection to change a list box item and the second statement reads an item in the list box. Note that we have applied the cast to the list box item as the return type of the Items indexer (Items()) is object. Remember you will only need this cast if you are using the Option Strict Option 'On' which we prefer to use.
You may also see at run time, the currently selected item using the SelectedItem property of the list box.
MessageBox.Show(lbxBooks.SelectedItem.ToString())
Removing items from the list box
Individual items can be removed from the list box either by calling the Remove() or RemoveAt() method of the Items collection of the list box. The Remove() method accepts an object to be removed from the list box as
lbxBooks.Items.Remove("Programming C#")
The above line will remove the item 'Programming C#' from the list box. You can also use the RemoveAt() method which accepts the index of the item to be removed from the list box.
lbxBooks.Items.RemoveAt(0)
This line will remove the element at index 0 (the first element) from the list box. Finally, to remove all the items contained in a list box, you can call the Clear() method.
lbxBooks.Items.Clear()
List Box Events
The most important and frequently used event of the list box is SelectedIndexChanged. This event is triggered when an item is selected in the list box. To add an event handler for this event, double click the list box in the form designer. The following code will show the selected item in the message box whenever the selection in the list box is changed.
Private Sub lbxBooks_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles lbxBooks.SelectedIndexChanged
MessageBox.Show("The selected item is " + _
lbxBooks.SelectedItem.ToString(), "Selected Item")
End Sub
School Home