189
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:
groupBox1.Controls.AddRangeNew Control {radioButton1, radioButton2}
5.1.6 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 ImageLists image collection. The Add method has three overloads, whose signatures are given as
follows:
Overloads Public Sub Add ByVal value As System.Drawing.Icon Overloads Public Sub Add ByVal value As System.Drawing.Image
Overloads Public Sub Add ByVal value As System.Drawing.Image, _ ByVal transparentColor as System.Drawing.Color
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: Imports System.Drawing
Dim imageList1 As ImageList1 = New ImageList ImageList1.Images.AddNew IconC:\Palm.ico
ImageList1.Images.AddNew BitmapC:\TV.bmp ImageList1.Images.AddNew BitmapC:\Dog.bmp, Color.White
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 24 = 16 colors are available.
The ImageSize property determines the sizes of all images in the list. By default, the value is System.Drawing.Size16, 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
controls 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:
button1.ImageList = imageList1 button1.ImageIndex = 0
5.1.7 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 controls border and takes any of
190
the three members of the BorderStyle enumeration: None
the default, FixedSingle
, and Fixed3D
. Figur e 5- 3
shows three labels with three different values of BorderStyle.
Figure 5-3. Three labels with different BorderStyle values
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 labels FlatStyle property can take no other value than FlatStyle.Standard
. For more information on the FlatStyle property, see Sect ion 5.1.1
earlier in this chapter.
You normally assign a String to the labels 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:
Label1.Image = New System.Drawing.Bitmapfilepath Another important property is TextAlign, which determines how the labels 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.
5.1.8 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:
Overloads Public Function Add _ ByVal start As Integer, _
ByVal length As Integer, _ ByVal linkData As Object _
As Link
The start
argument is the first character of the Text propertys 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
191
contains Go to our web site, and you want web site to be the hyperlink, here is how you would use the Add method:
linkLabel1.Links.Add10, 8, www.oreilly.com 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:
Dim WithEvents linkLabel1 As LinkLabel = new LinkLabel linkLabel1.Text = Go to our web site
linkLabel1.Links.Add10, 8, www.oreilly.com linkLabel1.Location = New System.Drawing.Point64, 176
linkLabel1.Name = LinkLabel1 linkLabel1.Size = New System.Drawing.Size120, 16
Add to a form. Me.Controls.AddlinkLabel1
Private Sub LinkLabel1_LinkClicked _ ByVal sender As Object, _
ByVal e As LinkLabelLinkClickedEventArgs _ Handles linkLabel1.LinkClicked
Start the default browser and direct it to www.oreilly.com. System.Diagnostics.Process.Starte.Link.LinkData.ToString
End Sub
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:
Private Sub LinkLabel1_LinkClicked _ ByVal sender As Object, _
192
ByVal e As LinkLabelLinkClickedEventArgs _ Handles linkLabel1.LinkClicked
LinkLabel1.LinkVisited = True Start the default browser and direct it to www.oreilly.com.
System.Diagnostics.Process.Starte.Link.LinkData.ToString End Sub
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.
5.1.9 The ListBox Class