Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?
*/

Other Views

corner
*/

C# School - Windows Controls and Dialog Boxes - Lesson #12 - Page 1

                     Next Page



More Windows Controls and Standard Dialog Boxes

If you are new to C# School
This is the 12th in the series of lessons of our C# School. The C# School is a kind of interactive learning platform where those who want to learn .NET with C# can find help and support. With one issue a week, describing some areas of the C# 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: The complete source code of the program can be downloaded by clicking here.

Lesson Plan
Today 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 List View controls. Later, we will learn about other common controls, including the main menu, image list, Toolbar and 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 type of collection, like 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. 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 of 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 indexer property that allows items in the list box to be accessed programatically (through the code).

	lbxBooks.Items[0] = "Program Development in Java"; 
					// changing list box item
	string book1 = (string) lbxBooks.Items[1]; 
					// reading list box item


In the above code, the first statement uses the indexer 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.

You may also get, 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 void lbxBooks_SelectedIndexChanged
  (object sender, System.EventArgs e)
{
    MessageBox.Show("The selected item is " + 
      lbxBooks.SelectedItem.ToString(), "Selected Item");
}



                     Next Page

corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.
Resource Listings