ListBox Control

for RuBoard

The .NET Framework provides a number of controls that you can use to display lists of items to the user. These controls also allow the user to select an item from the list, typically by clicking on the item to be selected. In this section we examine the ListBox control.

Our example program is HotelAdmin\Step3 . The main form in MainAdminForm.cs contains the listbox listHotels , which maintains a list of hotels. Each hotel is represented by a string with values separated by commas.

Populating a ListBox

When the HotelAdmin program starts up, it populates the listbox listHotels with a list of hotels as part of the initialization in the MainAdminForm constructor.

 public MainAdminForm()  {     //     // Required for Windows Form Designer support     //     InitializeComponent();     //     // TODO: Add any constructor code after     // InitializeComponent call     //     hotelBroker = new HotelBroker();  ShowHotelList(hotelBroker.GetHotels());  } 

The ShowHotelList method displays an array list of hotels in a listbox. This array list is obtained by calling HotelBroker.GetHotels . Here is the code for ShowHotelList .

 private void ShowHotelList(ArrayList array)  {  listHotels.Items.Clear();  if (array == null)     {        return;     }     for each(HotelListItem hotel in array)     {        string city = hotel.City.Trim();        string name = hotel.HotelName.Trim();        string rooms = hotel.NumberRooms.ToString();        string rate = hotel.Rate.ToString();        string str = city + "," + name + ","                 + rooms + "," + rate;  listHotels.Items.Add(str);  }  } 

A ListBox has a property Items which maintains a collection of object references. We first call Items.Clear to clear out the listbox of items currently being displayed. We then loop through the hotels in the array list and build up a string consisting of the fields of the hotel structure, separated by commas. This string is added to the listbox by calling Items.Add .

Selecting an Item From a ListBox

An item in a listbox is selected by clicking on the item, generating a SelectedIndexChanged event. You can access the selected item through the SelectedIndex and SelectedItem properties. If no item is selected, SelectedIndex is -1. Here is the code for the event handler for SelectedIndexChanged .

  private void listHotels_SelectedIndexChanged(object sender,   System.EventArgs e)  {     if (  listHotels.SelectedIndex  != -1)     {        string selected = (string)  listHotels.SelectedItem  ;        char[] sep = new char[] {','};        string[] fields;        fields = selected.Split(sep);        currHotel = new HotelListItem();        currHotel.City = fields[0];        currHotel.HotelName = fields[1];        currHotel.NumberRooms = Convert.ToInt32(fields[2]);        currHotel.Rate = Convert.ToDecimal(fields[3]);     }     else     {        currHotel.HotelName = "";     }  } 

Since the items in a listbox are stored as object references, we cast the selected item to a string . We use String.Split to extract the fields that are separated by commas and store them in the fields string array. The values are then moved from the array and stored in currHotel . In the previous section we saw currHotel used to initialize the "New Hotel" and "Change Hotel Information" dialog boxes.

for RuBoard


Application Development Using C# and .NET
Application Development Using C# and .NET
ISBN: 013093383X
EAN: 2147483647
Year: 2001
Pages: 158

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net