Widows controls

By themselves, one or more forms provide very little functionality to most desktop applications. For the most part, forms are valuable insofar as they serve as containers for controls. In this chapter, we'll complete our discussion of building desktop applications by focusing on the objects that forms contain—in particular, controls and components, common dialogs, and menus. 

Common Controls and Components
This section contains a summary of the controls and components defined in the System.Windows.Forms namespace. Components are classes derived from the Component class (defined in the System.ComponentModel namespace). They may or may not provide a visual interface. They are often used as elements of forms but don't have to be. Controls are classes derived from the Control class (defined in the System.Windows.Forms namespace). Controls generally are used to build the visual appearance of a form. The Control class itself is derived from the Component class, so controls are also components.

The Button Class
This class represents a button control, which is one of the most commonly used controls in Windows applications. The Button class's Click event, which it inherits from Control, is its most commonly used event. The Button class inherits two important properties from ButtonBase: FlatStyle and Image. The first determines the appearance of the button and can take any value of the FlatStyle enumeration: Flat, Popup, Standard (the default), and System. Buttons with these four settings are shown in Figure 14. Assigning FlatStyle.System as the value of the FlatStyle property makes the appearance of the button dependent on the operating system.

Figure 14
The Image property allows you to embed an image into a button. The following code shows how to programmatically set the Image property of Button:
CODES:
The CheckBox Class
The CheckBox class represents a checkbox control. Its appearance is determined by its Appearance property, which can take either value of the Appearance enumeration: Button or Normal (the default). The Button value is rarely used because this setting makes the checkbox look like a Button control. The CheckBox class's Checked property can be set to True to make the checkbox checked or False to uncheck it.


The ComboBox Class
Both the ComboBox and ListBox classes derive from the ListControl class; therefore, the ComboBox class is very similar to the ListBox class and has properties and methods similar to those of the ListBox class.
The following properties are specific to the ComboBox class:


DropDownStyle
Defines the drop-down style of the ComboBox. It can take any value of the ComboBoxStyle enumeration: DropDown (the default value), DropDownList, and Simple. Both DropDown and DropDownList require the user to click the arrow button to display the drop-down portion of the ComboBox; however, DropDown allows the user to edit the text portion of the ComboBox. Simple makes a ComboBox's text portion editable and its drop-down portion always visible.


DroppedDown
Specifies whether the drop-down portion of a ComboBox is visible.


The DateTimePicker Class
The DateTimePicker class represents a control that allows users to select a date in the calendar, just like the MonthCalendar control. Unlike MonthCalendar, however, the DateTimePicker control only displays a box, which looks like a combo box, containing the selected date. When the user clicks the arrow, the control displays a drop-down calendar similar to the MonthCalendar control, from which the user can select a date. This drop-down portion closes as soon as the user selects a date. The user can also click on the day, date, month, or year portion of the control for editing. The DateTimePicker class has MinDate and MaxDate properties that are similar to the ones in the MonthCalendar class. To set the current date or to obtain the selected date, use the Value property of the DateTimePicker class. The selected date is readily available as a DateTime data type.


The GroupBox Class
As the name implies, a GroupBox control is used for grouping other controls, such as radio buttons or
checkboxes; it corresponds to the Frame control in Visual Basic 6.0.A GroupBox grouping two radio
buttons is shown in Figure 15


Figure 15
The Controls property of GroupBox represents a Control.ControlCollection class. It has methods such as Add, AddRange, Clear, GetEnumerator, and Remove, which behave exactly as do the same methods in Form.ControlCollection. For example, you can add several controls at once to a GroupBox using its AddRange method, as demonstrated by the following code that adds two radio buttons to a GroupBox named groupBox1:
CODES:
The ImageList Class
The ImageList class allows you to manage a collection of images. The most important property of this class is Images, which returns an ImageList.ImageCollection object. The ImageList.ImageCollection class has methods to add and remove images from the collection.
The Add method of the ImageList.ImageCollection class adds a bitmap image or an icon to the ImageList's image collection. The Add method has three overloads, whose signatures are given as follows:
CODES:
The first overload allows you to add an icon, and the second overload is used to add an object of type System.Drawing.Image. The third overload is used to add an image and make a color of that image transparent. For example, if you have an image with a blue background color, you can make the added image transparent by passing a blue color as the second argument to the third overload of the Add method.


The RemoveAt method of the ImageList.ImageCollection class allows you to remove an image. Once you instantiate an ImageList object, you can start adding images or icons. The following code, for example, adds three images and icons using three different overloads of the Add method:
CODES:
Important properties of the ImageList class include ColorDepth and ImageSize. The ColorDepth property determines the number of colors available for the ImageList. For example, a value of 4 means that 2^4 = 16 colors are available.
The ImageSize property determines the sizes of all images in the list. By default, the value is System.Drawing.Size(16, 16).
You can assign an ImageList object to the ImageList property of controls such as Label, Button, and ListView. You can then select an image from the ImageList to be displayed on the control using the control's ImageIndex property. For example, the following code uses an ImageList for a button and selects the first image as the background image for the button:
CODES:
The Label Class
This class represents a Label control. Its appearance is determined by two properties: BorderStyle and FlatStyle. The BorderStyle property defines the appearance of the control's border and takes any of the three members of the BorderStyle enumeration: None (the default), FixedSingle, and Fixed3D. Figure 16 shows three labels with three different values of BorderStyle.


Figure 16 
The FlatStyle property defines the appearance of the control and can take as its value any member of the FlatStyle enumeration: Flat, Popup, Standard, and System. However, if the value of the BorderStyle property is set to None, the label's FlatStyle property can take no other value than FlatStyle.Standard.


You normally assign a String to the label's Text property. However, you can also assign an image to its Image property. For example, the following code programmatically sets the Image property of a label:
CODES:
Another important property is TextAlign, which determines how the label's text is aligned. This property can take any member of the ContentAlignment enumeration, including BottomCenter, BottomLeft, BottomRight, MiddleCenter, MiddleLeft, MiddleRight, TopCenter, TopLeft (the default value), and TopRight.


The UseMnemonic property can be set to True so that the label accepts an ampersand character in the Text property as an access-key prefix character.


The LinkLabel Class
The LinkLabel class represents a label that can function as a hyperlink, which is a URL to a web site. Its two most important properties are Text and Links. The Text property is a String that defines the label of the LinkLabel object. You can specify that some or all of the Text property value is a hyperlink. For example, if the Text property has the value "Click here for more details", you can make the whole text a hyperlink, or you can make part of it (e.g., the word "here") a hyperlink. How to do this will become clear after the second property is explained.
For a LinkLabel to be useful, it must contain at least one hyperlink. The Links property represents a LinkLabel.LinkCollection class of the LinkLabel object. You use the Add method of the LinkLabel.LinkCollection class to add a LinkLabel.Link object. The Add method has two overloads; the one that will be used here has the following signature:
CODES:
The start argument is the first character of the Text property's substring that you will turn into a hyperlink. The length argument denotes the length of the substring. The linkData argument is normally a String containing a URL, such as "www.oreilly.com". For example, if your Text property contains "Go to our web site", and you want "web site" to be the hyperlink, here is how you would use the Add method:
CODES:
10 is the position of the character w in the Text property, and 8 is the length of the substring "web site". The LinkLabel class has a LinkClicked event that you can capture so that you can run code when a LinkLabel object is clicked. The following example creates a LinkLabel object that is linked to the URL www.oreilly.com and starts and directs the default browser to that URL when the LinkLabel is clicked:
CODES:
The LinkLabel class has a number of properties that are related to the appearance of a LinkLabel object:


ActiveLinkColor
Represents the color of the LinkLabel when it is being clicked (i.e., when you press your mouse button but before you release it). By default, the value of ActiveLinkColor is System.Drawing.Color.Red.


DisabledLinkColor
Represents the color of the LinkLabel when it is disabled.


LinkColor
Represents the color of the LinkLabel in its normal condition. By default, the value is System.Drawing.Color.Blue.


VisitedLinkColor
Represents the color of a LinkLabel that has been visited. By default, this property value is System.Drawing.Color.Purple. The LinkLabel does not automatically display in its VisitedLinkColor after it is clicked. You must change its LinkVisited property to True. Normally, you do this inside the LinkClicked event handler of the LinkLabel object. Therefore, in the previous example, if you want the LinkLabel to change color after it is clicked, you can modify its LinkClicked event handler with the following:
CODES:
LinkBehavior
Determines how the LinkLabel is displayed. This property can take any member of the LinkBehavior enumeration: AlwaysUnderline, HoverUnderline, NeverUnderline, and SystemDefault (the default value).


The ListBox Class
The ListBox class represents a box that contains a list of items. The following are its more important properties:


MultiColumn
This is a Boolean that indicates whether the listbox has more than one column. Its default value is False.


ColumnWidth
In a multicolumn listbox, this property represents the width of each column in pixels. By default, the value of this property is zero, which makes each column have a default width.


Items
This is the most important property of the ListBox class. It returns the ListBox.ObjectCollection class, which is basically the Items collection in the ListBox. You can programmatically add an item using the Add method or add a range of items using the AddRange method of the ListBox.ObjectCollection class. For example, the following code adds the names of vegetables and fruits to a ListBox object named listBox1:
CODES:
SelectionMode
This property determines whether multi-item selection is possible in a ListBox object. It can be assigned any member of the SelectionMode enumeration: None, One (the default value), MultiSimple, and MultiExtended. Both MultiSimple and MultiExtended allow the user to select more than one item. However, MultiExtended allows the use of the Shift, Ctrl, and arrow keys to make a selection.


SelectedIndex
This is the index of the selected item. The index is zero-based. If more than one item is selected, this property represents the lowest index. If no item is selected, the property returns




SelectedIndices
This read-only property returns the indices to all items selected in a ListBox object in the form of a ListBox.SelectedIndexCollection object. The ListBox.SelectedIndexCollection class has a Count property that returns the number of selected indices and an Item property that returns the index number. For example, the following code returns the index number of all selected items in a ListBox control named listBox1:
CODES:
SelectedItem
This read-only property returns the selected item as an object of type Object. You must cast the returned value to an appropriate type, which is normally String. If more than one item is selected, the property returns the item with the lowest index.


SelectedItems
This read-only property returns all items selected in a ListBox object in the form of a ListBox.SelectedObjectCollection object. The ListBox.SelectedObjectCollection class has a Count property that returns the number of items in the collection and an Item property that you can use to obtain the selected item. For example, the following code displays all the selected items of a ListBox control called listBox1:
CODES:
Sorted
A value of True means that the items are sorted. Otherwise, the items are not sorted. By default, the value of this property is False.


Text
This is the currently selected item's text.


TopIndex
This is the index of the first visible item in the ListBox. The value changes as the user scrolls through the items.


The ListBox.ObjectCollection Class
This class represents all the items in a ListBox object. It has a Count property that returns the number of items in the ListBox and an Item property that returns the item object in a certain index position.

The following sample code reiterates all the items in a ListBox control named listBox1:
CODES:
In addition, the ListBox.ObjectCollection class has the following methods:


Add
Adds an item to the ListBox object. Its syntax is:
CODES:
where item is data of type Object that is to be added to the collection. The method returns the
zero-based index of the new item in the collection.


AddRange
Adds one or more items to the ListBox object. Its most common syntax is:
CODES:
where items is an array of objects containing the data to be added to the ListBox.


Clear
Clears the ListBox, removing all the items. Its syntax is:
CODES:
Contains
Checks whether an item can be found in the list of items. Its syntax is:
CODES:
where value is an Object containing the value to locate in the ListBox. The method returns True if value is found; otherwise, it returns False.


CopyTo
Copies all items to an object array. Its syntax is:
CODES:
where dest is the Object array to which the ListBox items are to be copied, and arrayIndex is the starting position within dest at which copying is to begin.


IndexOf
Returns the index of a particular item. Its syntax is:
CODES:
where value is an Object representing the item to locate in the collection. The method returns the item's index. If the item cannot be found, the method returns -1.


Insert
Inserts an item into the ListBox at the specified index position. Its syntax is:
CODES:
where index is the zero-based ordinal position at which the item is to be inserted, and item is an Object containing the data to be inserted into the collection.


Remove
Removes the item that is passed as an argument to this method from the ListBox. Its syntax is:
CODES:
where value is an Object representing the item to remove from the collection.


RemoveAt
Removes an item at the specified index position. Its syntax is:
CODES:
here index is the zero-based ordinal position in the collection of the item to be removed.


The ListView Class
A ListView is a container control that can hold a collection of items. Each item in a ListView can have descriptive text and an image, and the items can be viewed in four modes. The righthand pane of Windows Explorer is a ListView control.
An item in a ListView is represented by an object of type ListViewItem. The various constructors of the ListViewItem class permit a ListViewItem to be constructed with a String or with a String and an index number. If an index number is used, it represents the index of the item's image in the ImageList referenced by the ListView.
The following code constructs two ListViewItem objects. The first has the text "Item1" and uses the first image in the ImageList. The second has the text "Item2" and uses the second image of the ImageList:
CODES:
Once you have references to one or more ListViewItem objects, you can add the items to your ListView object. To add an item or a group of items, you first need to reference the ListView.ListViewItemCollection collection of the ListView object. This collection can easily be referenced using the Items property of the ListView class. The ListView.ListViewItemCollection has Add and AddRange methods that you can use to add one item or a group of items. For instance, the following code uses the AddRange method to add two ListViewItem objects to a ListView object:
CODES:
The Add method of the ListView.ListViewItemCollection has three overloads, two of which allow you
add to a ListViewItem without first creating a ListViewItem object.
To add a ListViewItem object to the collection, you can use the following overload of the Add method:
CODES:
Or, to add a String and convert it into a ListViewItem object, use the following overload:
CODES:
Alternatively, you can pass a String and an image index to the third overload:
CODES:
The following code demonstrates how to add two ListViewItem objects to a ListView. The ListView is linked to an ImageList that has two images in its collection. When the code is run, it produces something similar to Figure 17.


 Figure 17.
CODES:
Two properties of the ListView class tell you which item(s) are selected: SelectedIndices and SelectedItems. The first returns a ListView.SelectedIndexCollection object, and the second returns a ListView.SelectedListViewItemCollection object.
The ListView.SelectedIndexCollection class has a Count property that tells you how many items are selected and an Item property that returns the index of the designated item. For example, you can retrieve the index of the first selected item by passing 0 to the Item property, as follows:
CODES:
The ListView.SelectedListViewItemCollection class is very similar to ListView.SelectedIndexCollection. Its Count property indicates how many items are selected. However, its Item property returns the item itself, not an index number.



No comments:

Post a Comment