Using The ActiveX Controls - 2
Using The ActiveX Controls - 2
Visual Basic (also see "Using the ActiveX Controls – 1). For many of these, you will
also find scenarios — along with code — featuring the controls in sample
applications.
Contents
· Using the Slider Control
· Using the StatusBar Control
Possible Uses
· To set the value of a point on a graph.
· To select a range of numbers to be passed into an array.
· To resize a form, field, or other graphic object.
1
TickStyle and TickFrequency Properties
The Slider control consists of two parts: the thumb and the ticks, as shown below:
—1
—2
Selecting Ranges
If the SelectRange property is set to True, the Slider control changes its appearance,
as shown below:
—3
—4
—5
If Shift = 1 Then
' If the user has the Shift key down,
' handle it here.
End If
End Sub
9
MouseDown Event: Set the SelStart and SelLength Properties
If the SHIFT key is being held down by the user, the code then sets the SelStart and
SelLength properties to appropriate values. The SelStart property specifies where a
selection of values will begin. In the present context, the SelStart property would be
set to where the thumb is placed — the Slider control's Value property.
The SelLength property specifies a range of values to select; this property begins at
the SelStart value. In the MouseDown event, a new range is being selected, so any
previous range must be deselected by setting the SelLength property to 0. This is
shown in the code below:
—6
If Shift = 1 Then
' If user selects backwards from a point,
' an error will occur.
On Error Resume Next
' Else set SelLength using SelStart and
' current value.
sldSelect.SelLength = _
sldSelect.Value - sldSelect.SelStart
Else
'If user lifts SHIFT key, set SelLength
' to 0 (to deselect the SelRange) and exit.
sldSelect.SelLength = 0
End If
End Sub
12
The Complete Code
The complete code is shown below:
Private Sub Form_Load()
sldSelect.SelectRange = True
End Sub
If Shift = 1 Then
sldSelect.SelStart = sldSelect.Value
—7
If Shift = 1 Then
' If user selects backwards from a point,
' an error will occur.
On Error Resume Next
' Else set SelLength using SelStart and
' current value.
sldSelect.SelLength = _
sldSelect.Value - sldSelect.SelStart
Else
'If user lifts SHIFT key, set SelLength
' to 0 (to deselect the SelRange) and exit.
sldSelect.SelLength = 0
End If
End Sub
13
Possible Uses
· To inform the user of a database table's metrics, such as number of records, and
the present position in the database.
· To give the user information about a RichTextBox control's text and font status.
· To give status about key states (such as the Caps Lock or the Number Lock)
9
The Panel Object and the Panels Collection
The StatusBar control is built around the Panels collection. Up to sixteen Panel
objects can be contained in the collection. Each object can display an image and
text, as shown below:
—8
11
Using this dialog box, you can add individual Panel objects, and set the various
properties for each panel.
Use the Set Statement with the Add Method to Create Panels
at Run Time
To add Panel objects at run time, use the Set statement with the Add method. First
declare an object variable of type Panel, then set the object variable to a Panel
created with the Add method, as shown in the code below:
' The StatusBar control is named "sbrDB."
Dim pnlX As Panel
Set pnlX = sbrDB.Panels.Add()
14
Once you have created a Panel object and set the object variable to reference the
new object, you can set the various properties of the Panel:
pnlX.Text = Drive1.Drive
pnlX.Picture = LoadPicture("mapnet.bmp")
—9
—10
12
Settings for the Bevel property are:
Constant Value Description
sbrNoBevel 0 The Panel displays no bevel, and text looks like it is displayed
right on the status bar
sbrInset 1 The Panel appears to be sunk into the status bar.
sbrRaised 2 The Panel appears to be raised above the status bar.
19
The AutoSize property determines how a Panel object will size itself when the
parent container (either a Form or a container control) is resized by the user. The
figure below shows a StatusBar control before being resized:
13
When the container (the Form on which the StatusBar control is placed) of the
control is resized, notice that the first panel retains its width, the second "springs" to
—11
14
Settings for the AutoSize property are:
Constant Value Description
sbrNoAutoSize 0 None. No autosizing occurs. The width of the panel is always and
exactly that specified by the Width property.
sbrSpring 1 Spring. When the parent form resizes and there is extra space
available, all panels with this setting divide the space and grow
accordingly. However, the panels' width never falls below that
specified by the MinWidth property.
sbrContents 2 Content. The panel is resized to fit its contents.
20
Tip Set the AutoSize property to Content (2) when you want to assure that the contents of a
particular panel are always visible.
21
The Alignment property specifies how the text in a panel will align relative to the
panel itself as well as any image in the panel. As with a word processor, the text can
be aligned left, center, or right, as shown below:
15
Settings for the Alignment property are:
Constant Value Description
sbrLeft 0 Text appears left-justified and to right of bitmap.
sbrCenter 1 Text appears centered and to right of bitmap.
sbrRight 2 Text appears right-justified but to the left of any bitmap.
22
Style Property and the SimpleText Property
The StatusBar control features a secondary mode in which the multiple panels are
replaced by a single panel that spans the width of the control. This single panel has
one property, the SimpleText property which specifies what text is displayed on the
panel. To display this single panel, set the Style property of the StatusBar to
sbrSimple (1).
One reason for switching to the Simple style and displaying a single panel is to
notify the user that a lengthy transaction is occurring. For example, if you are
—12
16
Possible Uses
· To create a tabbed dialog that sets various text attributes for a RichTextBox
control.
· To create a tabbed dialog that sets preferences for an application.
17
The Tabs Collection
The control consists of one or more Tab objects in a Tabs collection. At both design
time and run time, you can affect the Tab object's appearance by setting properties,
and at run time, by invoking methods to add and remove Tab objects.
—13
—14
20
Create Tab Objects at Run Time Using the Add Method
To create Tab objects at run time, use the Add method for Tab objects.
Note One Tab object is created for you by default.
25
To create a collection of Tab objects at run time
12 Declare a variable as type Tab. As you add each Tab object, the variable will
contain the reference to the newly created object. Use this reference to set
various properties of the new Tab object.
13 Using the Set statement with the Add method, set the object variable to the new
Tab object.
14 Using the object variable, set the properties of the new Tab object.
21
The code below uses the Form object's Load event to create two Tab objects, then
sets the Caption, Image, and Key properties of the new Tab object.
Private Sub Form_Load()
' The TabStrip control is named "tabData"
' Declare a variable, then use the Set
' statement with the Add method to create a new
' Tab object, while setting the object variable to
' the new Tab. Use the reference to set properties.
Dim tabX As Tab
' Tab 1: Find text.
—15
22
At run time, when the user clicks on a tab, you must program the client area to be
reconfigured with a different set of container controls (discussed below in
"Managing Tabs and Container Controls").
At design time, draw a container control, such as the PictureBox or Frame control,
on the form. If you use a Frame control, you can set its BorderStyle property to be
invisible at run time. Copy and paste the same control to create an array of controls;
create one control for each Tab object you have created.
On each container control, draw the controls that should appear on a tab. Your form
may appear something like Figure 2.37, below:
—16
23
After you have created the container controls, there is one additional technique
required to position them over the TabStrip control's client area: use the Move
method with the ClientLeft, ClientTop, ClientWidth, and ClientHeight properties of
the Tabstrip control, as shown in the code below:
Private Sub Form_Load()
' The name of the TabStrip is "tabRTF."
' The Frame control is named "fraTab."
For i = 0 To fraTab.Count - 1
With fraTab(i)
.Move tabRTF.ClientLeft, _
tabRTF.ClientTop, _
tabRTF.ClientWidth, _
tabRTF.ClientHeight
End With
Next i
—17
25
or push buttons (Buttons).
26
The advantages of each are outlined below:
—18
32
Multi-Row Tabs
Another feature of the TabStrip control is the MultiRow property. When this
property is set to True, a large number of Tab objects appear in rows, as seen in the
figure below:
27
If the MultiRow property is set to False, the same set of tabs appears in a single row,
with a pair of scroll buttons at the rightmost end:
28
The TabWidthStyle property determines the appearance of each row, and, if
TabWidthStyle is set to Fixed, you can use the TabFixedHeight and TabFixedWidth
properties to set the same height and width for all tabs in the TabStrip control.
29
Typically, a toolbar contains buttons that correspond to items in an application's
menu, providing a graphic interface for the user to access an application's most
—19
Possible Uses
· Provide a consistent interface between applications with matching toolbars.
· Place commonly used functions, such as File operations, in an easy to access
place.
· Provide a graphical, intuitive interface for your application.
30
The Buttons Collection
The Toolbar control consists of one or more Button objects in a Buttons collection.
At both design time and run time, you create Button objects. Each button can have
an image, a caption, a Tooltip, or all three, as shown below:
31
Each button object also has a Style property (discussed below) that determines how
the button will behave.
—20
—21
1
33
20 Click Insert Button to insert a new Button object.
21 Set appropriate properties, such as Key, Caption, Image, and ToolTipText.
22 Set the Button object's Style property by clicking the Style box and selecting a
style.
34
To create a collection of Button objects at run time
23 Declare an object variable of type Button. As you add each Button object, the
variable will contain the reference to the newly created object. Use this
reference to set various properties of the new Button object.
24 Using the Set statement with the Add method, set the object variable to the new
Button object.
25 Using the object variable, set the properties of the new Button object.
35
The code below uses the Form object's Load event to create one Button object, then
sets the Key, Caption, TooltipText, and Style properties of the new Button object.
Private Sub Form_Load()
' Declare a variable, then set using the Set
' statement with the Add method, create a new
' Button object, and set the object variable to
' the new Button. Use the reference to set
' properties.
Dim myButton As Button
—22
—23
—24
37
You can add a ToolTip to any button at design time by typing the text you want to
appear in the ToolTipText box of the Toolbar control's Property Pages.
At run time, you can dynamically change the ToolTip by setting the ToolTipText
property for the Button object. The following code occurs in a CommandButton
control that changes the Key and ToolTipText property of one button:
Private Sub cmdChangeButton_Click()
' The name of the toolbar is "tlbFunctions"
' Reset the Key and ToolTipText properties of
' a button with Key property value "1 funct"
tlbfuncts.Buttons("1 funct"). _
ToolTipText = "Function 7"
—25
38
Alternatively, you can display the dialog box by invoking the Customize method.
—26
39
Possible Uses
· To create an organization tree that can be manipulated by the user.
· To create a tree that shows at least two or more levels of a database.
40
—27
41
Each node in a tree is actually a programmable Node object, which belongs to the
Nodes collection. As in other collections, each member of the collection has a
unique Index and Key property which allows you to access the properties of the
node. For example, the code below uses the Index of a particular node ("7") to set
the Image and Text properties:
tvwMyTree.Nodes(7).Image = "closed"
tvwMyTree.Nodes(7).Text = "IEEE"
42
However, if a unique key, for example "7 ID" had been assigned to the node, the
same code could be written as follows:
tvwMyTree.Nodes("7 ID").Image = "closed"
tvwMyTree.Nodes("7 ID").Text = "IEEE"
43
Node Relationships and References to Relative Nodes
Each node can be either a child or a parent, depending on its relationship to other
nodes. The Node object features several properties which return various kinds of
—28
—29
—30