Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

11. More Windows Controls & Standard Dialog Boxes

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).

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.

211

Programmers Heaven: C# School

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 programmatically (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());

212

Programmers Heaven: C# School

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.

213

Programmers Heaven: C# School

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");

}

Combo Box Control

The combo box is similar to the list box in that it is used to display a list of items. The combo box is presented in

.Net through the System.Windows.Forms.ComboBox class. The combo box has three visual designs which can be toggled using the ComboBox class' 'DropDownStyle' property. The three designs are named 'Simple', 'DropDown' and 'DropDownList'. The following screen shot demonstrates these three drop down styles.

The Simple style shows the list of items and the selected item at the same time in separate areas. The simple drop down style combo box is typically used in the font dialog box (discussed later in the lesson). The DropDown style shows the items in a drop down list. The user can write a new item in its text area. The DropDownList style is similar to the drop down style but here the user can only select one of the available choices and can not provide a new item himself.

Items can be inserted, removed and accessed in just the same way they were in the list box.

Tree View

The Tree View Control is used for hierarchical representations of items.It is represented in .Net by the ystem.Windows.Forms.TreeView class. The following screen shot gives an example of how the tree view control can look.

214

Соседние файлы в папке books