15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

st<strong>and</strong>ard Controls <strong>and</strong> Components ❘ 1127<br />

The Appearance property takes an Appearance enumeration value. This can be either Button or Normal.<br />

When you choose Normal, the radio button looks like a small circle with a label beside it. Selecting the<br />

button fills the circle; selecting another button deselects the currently selected button <strong>and</strong> makes the<br />

circle look empty. When you choose Button, the control looks like a st<strong>and</strong>ard button, but it works like a<br />

toggle — selected is the in position, <strong>and</strong> deselected is the normal, or out, position.<br />

The CheckedAlign property determines where the circle is in relation to the label text. It could be on top of<br />

the label, on either side, or below it.<br />

The CheckedChanged event is raised whenever the value of the Checked property changes. This way, you<br />

can perform other actions based on the new value of the control.<br />

Combobox, listbox, <strong>and</strong> Checkedlistbox<br />

ComboBox, ListBox, <strong>and</strong> CheckedListBox are all derived from the ListControl class. This class provides<br />

some of the basic list management functionality. The most important aspects of using list controls are<br />

adding data to <strong>and</strong> selecting data from the list. Which list is used is generally determined by how the list is<br />

used <strong>and</strong> the type of data that is going to be in the list. If there is a need to have multiple selections or if the<br />

user needs to be able to see several items in the list at any time, the ListBox or CheckedListBox is the best<br />

choice. If only a single item is ever selected in the list at any time, a ComboBox may be a good choice.<br />

Data must be added to a list box before it can be useful. You add data by adding objects to the ListBox<br />

.ObjectCollection. This collection is exposed by the list’s Items property. Because the collection<br />

stores objects, any valid .<strong>NET</strong> type can be added to the list. In order to identify the items, two important<br />

properties need to be set. The first is the DisplayMember property. This setting tells the ListControl what<br />

property of your object should be displayed in the list. The other is ValueMember, which is the property of<br />

your object that you want to return as the value. If strings have been added to the list, by default the string<br />

value is used for both of these properties. The ListExample form in the sample application shows how data<br />

can be loaded into a list box. The example uses Vendor objects for the list data. The Vendor object contains<br />

just two properties: Name <strong>and</strong> Id. The DisplayMember property is set to the Name property. This tells the list<br />

control to display the value from the Name property in the list to the user.<br />

Once the data is loaded in the list, the SelectedItem <strong>and</strong> SelectedIndex properties can be used to get at<br />

the data. The SelectedItem property returns the object that is currently selected. If the list is set to allow<br />

multiple selections, there is no guarantee which of the selected items will be returned. In this case, the<br />

SelectedItems collection should be used. This contains a list of all of the currently selected items in the list.<br />

If the item at a specific index is needed, the Items property can be used to access the ListBox<br />

.ObjectCollection. Because this is a st<strong>and</strong>ard .<strong>NET</strong> collection class, the items in the collection can be<br />

accessed in the same way as any other collection class.<br />

If DataBinding is used to populate the list, the SelectedValue property will return the property value of the<br />

selected object that was set to the ValueMember property. If Id is set to ValueMember, the SelectedValue<br />

will return the Id value from the selected item. In order to use ValueMember <strong>and</strong> SelectedValue the list<br />

must be loaded by way of the DataSource property. An ArrayList or any other IList-based collection must<br />

be loaded with the objects first, <strong>and</strong> then the list can be assigned to the DataSource property.<br />

The Items property of the ComboBox returns ComboBox.ObjectCollection. A ComboBox is a combination<br />

of an edit control <strong>and</strong> a list box. You set the style of the ComboBox by passing a DropDownStyle enumeration<br />

value to the DropDownStyle property. The following table lists the various DropDownStyle values.<br />

Value<br />

DropDown<br />

DropDownList<br />

Simple<br />

desCriPTion<br />

The text portion of the combo box is editable, <strong>and</strong> users can enter a value. They can<br />

also click the arrow button to show the drop-down list of values.<br />

The text portion is not editable. Users must make a selection from the list.<br />

This is similar to DropDown except that the list is always visible.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!