Visual Basic in 12 Easy Lessons vel23.htm

Previous Page TOC Next Page



Lesson 12, Unit 23


The Scroll Bars, Grid, and Mouse



What You'll Learn


This unit wraps up the remaining controls that come with the Visual Basic Primer system by showing you ways to access and control special scroll bars and grid controls, as well as how to interpret the user's mouse movements and clicks. As with all the controls, understanding how to work with scroll bars and the grid requires that you master the properties related to those controls.



Note: The grid control requires a little preparation. In Lesson 2, you removed the grid control from the AUTOLOAD.MAK default project. Therefore, the grid control won't appear on your toolbox until you add the control once again.


Scrolling the Scroll Bars


Concept: The scroll bars give the user the ability to control changing values. Rather than type specific values, the user can move the scroll bars with the mouse to specify relative positions within a range of values.

Figure 23.1 shows you the location of the two scroll bar controls on the Toolbox window. There is a horizontal scroll bar and a vertical scroll bar control. In addition, you'll see two shapes whose width and height properties are being adjusted by the user's clicking in the scroll bar.

Figure 23.1a. The scroll bars and their descriptions.

Figure 23.1b. The scroll bars and their descriptions.

Table 23.1 contains a list of the scroll bar properties. The most unique and important property values for a scroll bar are the LargeChange, Max, Min, and SmallChange.

Table 23.1. The scroll bar properties.

Property Description
DragIcon Specifies the icon that appears when the user drags the scroll bar around on the form. (You'll only rarely allow the user to move a scroll bar, so the Drag... property settings aren't usually relevant.)
DragMode Contains either 1 for manual mouse dragging requirements (the user can press and hold the mouse button while dragging the control) or 0 (the default) for automatic mouse dragging, meaning that the user can't drag the scroll bar but that you, through code, can initiate the dragging if needed.
Enabled If set to True (the default), the scroll bar can respond to events. Otherwise, Visual Basic halts event processing for that particular control.
Height Contains the height, in twips, of the scroll bar.
HelpContextID If you add advanced, context-sensitive help to your application, the HelpContextID provides the identifying number for the help text.
Index If the scroll bar is part of a control array, the Index property provides the numeric subscript for each particular scroll bar.
LargeChange Specifies the amount that the scroll bar changes when the user clicks within the scroll bar's shaft area.
Left Holds the number of twips from the left edge of the Form window to the left edge of the scroll bar.
Max Indicates the maximum number of units that the scroll bar value represents at its highest setting. The range is from 1 to 32767 (the default).
Min Indicates the minimum number of units the scroll bar value represents at its lowest setting. The range is from 1 (the default) to 32767.
MousePointer Contains the shape that the mouse cursor changes to if the user moves the mouse cursor over the scroll bar. The possible values are from 0 to 12, and represent a range of different shapes that the mouse cursor can take on.
Name Contains the name of the control. By default, Visual Basic generates the names VScroll1, VScroll2, and so on (for vertical scroll bars), and HScroll1, HScroll2, and so on (for horizontal scroll bars) as you add subsequent scroll bars to the form.
SmallChange Specifies the amount that the scroll bar changes when the user clicks an arrow at either end of the scroll bar.
TabIndex Determines that the focus tab order begins at 0 and increments every time you add a new control. You can change the focus order by changing the controls' TabIndex to other values. No two controls on the same form can have the same TabIndex value.
TabStop If True, the user can press Tab to move the focus to this scroll bar. If False, the scroll bar can't receive the focus.
Tag Unused by Visual Basic. This is for the programmer's use for an identifying comment applied to the scroll bar.
Top Holds the number of twips from the top edge of a scrollbar to the top of the form.
Value Contains the unit of measurement currently represented by the position of the scroll bar.
Visible Contains either True or False, indicating whether the user can see (and, therefore, use) the scroll bar.
Width Holds the number of twips wide that the scroll bar consumes.

Tip: Prefix the names of your horizontal scroll bars with the hsb prefix and your vertical scroll bars with the vsb prefix so that you can easily distinguish them from each other.

When you place a scroll bar on a form, you must decide at that time what range of values the scroll bar is to represent. The scroll bar's full range can extend from 1 to 32767. Set the Min property to the lowest value represented by the scroll bar. Set the Max property to the highest value represented by the scroll bar.

When the user eventually uses the scroll bar, the scroll bar arrows control small movements in the scroll bar's value determined by the SmallChange property. Clicking the empty shaft on either side of the scroll box produces a positive or negative change in the value represented by the LargeChange property. The user can drag the scroll bar itself to any position within the scroll bar shaft to jump to a specific location instead of changing the value gradually.

Suppose, for example, that a horizontal scroll bar was to represent a range of whole dollar amounts from $5 to $100. When the user clicks the scroll arrows, the scroll bar's value is to change by one dollar. When the user clicks the empty shaft on either side of the scroll box, the scroll bar's value is to change by five dollars. Here are the property values that you would set that determine how Visual Basic interprets each click of the scroll bar:

The physical size of the scroll bar has no bearing on the scroll bar's returned values when the user selects from the scroll bar. Adjust the scroll bars on your form so that the scroll bars are wide enough or tall enough to look appropriate sizes for the items that they represent.

Stop and Type: Listing 23.1 contains the SCROLL.MAK code that you can load and run to adjust the circle and bar sizes that you saw in Figure 23.1.

Review: There are two scroll bars, a horizontal scroll bar and a vertical scroll bar, that give the user the ability to select from a range of possible values without having to enter individual values.

Listing 23.1. The code for the SCROLL.MAK application.


1: Option Explicit

2:

3: Sub Form_Load ()

4: ' Set 
initial scroll bar values

5: hsbBar.Value = 1800 ' Circle's default width

6: vsbBar.Value = 1800 ' Bar's default height

7: End Sub

8:

9: Sub hsbBar_Change ()

10: ' As user clicks the scroll bar,

11: ' the width of the circle adjusts

12: 
shpCircle.Width = hsbBar.Value

13: End Sub

14:

15: Sub vsbBar_Change ()

16: ' As user clicks the scroll bar,

17: ' the height of the bar adjusts

18: shpBar.Height = vsbBar.Value

19: End Sub

Analysis: Here are the vital horizontal scroll bar properties that were set during the design of the form:

The shape control that contains the circle (named shpCircle) has its Width property set to 2100, so the largest that the circle could appear within the control was 2100 twips. Hence the use of 2100 for the Max property.

Here are the vital vertical scroll bar properties that were set during the design of the form:

The shape control that contains the bar (named shpBar) has its Height property set to 2300, so the tallest that the bar could appear within the control was 2300 twips. Hence the use of 2300 for the Max property.

The two Min properties of 50 keep both the circle and bar from shrinking entirely when the user minimizes either control.

Lines 5 and 6 set the initial values for the circle's width and the bar's height to 1800, so both shapes are fairly large to begin with. Actually, the lines set the initial runtime values for both the scroll bars, which, in turn, generates the Change() event procedures that follow in the code.

Line 12 ensures that the circle's width increases or decreases, depending on the value of the horizontal scroll bar's Value property. Line 18 ensures that the bar's height increases or decreases, depending on the value of the vertical scroll bar's Value property.

Prepare for the Grid Control


Concept: Before you can use the grid control, you must add the grid control to your AUTOLOAD.MAK's Toolbox window.

Lesson 2 removed the grid control from the Toolbox window. The grid control is a special custom control that wasn't part of Visual Basic's original toolbox. All custom controls reside in files on your disk with the filename extension of VBX. All applications that use the grid control must list the GRID.VBX file in the Project window.

When you don't need the grid control, your applications will load more quickly and consume less disk space if you remove the GRID.VBX file from the application's Project window. Until now, the lessons in this book didn't need the grid control; therefore, Lesson 2 walked you through the steps to remove the grid control's customer GRID.VBX file from the AUTOLOAD.MAK's Project window.

If you want to work with the grid control, you must add the GRID.VBX file to any application that requires the grid control. You can decide now whether you want to add GRID.VBX to AUTOLOAD.MAK so that all subsequent projects will contain the grid control or just add the GRID.VBX file to individual projects that use the grid control.

The following steps explain how to add the grid control to AUTOLOAD.MAK. (If you want to remove the control after you complete this unit, you can do so by removing the control as explained in the second unit of Lesson 2.) To add the grid control to AUTOLOAD.MAK:

  1. Load the AUTOLOAD.MAK project.

  2. Select File Add File. Visual Basic displays the Add File dialog box.

  3. Select the file named GRID.VBX. The file should reside in your VBPRIMER directory.

  4. Press Enter or click the OK command button to close the dialog box. Visual Basic updates the Toolbox window so that the window contains the custom grid control as shown in Figure 23.3.

  5. Save the AUTOLOAD.MAK project.

Figure 23.2. The location of the grid control on the toolbox.



Note: All new projects that you now create will contain the grid control in the Toolbox window and the GRID.VBX listing in the Project window.

Review: Before learning how to use the grid control, you must load the GRID.VBX custom control file into your project's Project window. By loading the file into AUTOLOAD.MAK's project, all subsequent projects will contain the grid control until you remove the custom control file.

Using the Grid Control


Concept: The grid control produces a table of rows and columns in which you can display text, numeric values, and even graphics.

When you must display several values at once, the grid control is one of the handiest controls to use. Although labels are great for messages and individual data items, and scrolling list boxes are fine for lists of values from which the user can select, the grid gives your application a two-dimensional table display of data.

Suppose that you just started a lawn fertilization company, and you begin with an initial route of eight customers. Each customer requires five annual fertilizations. That's a total of 40 applications that you need to track for the upcoming year. Each yard size differs and each application requires different amounts and kinds of fertilization mixtures.

Thinking about the eight customer's five applications, you realize that a table with eight rows and five columns would be the perfect place for placing pricing data in a computer application. After you build a Visual Basic application that contains the values, you could easily modify the table or store the values in a disk file. As you add additional customers, you could easily expand the size of the grid through the program's code.



Definition: A cell is one row and column intersection of a grid.

Before looking at an actual demonstration of a grid that tracks this lawn care business, take a few moments to study Figure 23.3. The figure shows the layout of cells on a grid that might appear on a form. The grid's size is determined by the grid's properties, which you can set or adjust using the mouse when you place the grid control on the form.

Figure 23.3. The grid control produces a table of rows and columns.

Tip: The grid doesn't have be large enough to display all of its data. If the grid's size isn't large enough to hold all the grid's cells, Visual Basic adds scroll bars so that the user can scroll through the grid.

The figure's shaded row and column is known as a fixed row and fixed column. The grid's property value settings determine whether you want to set aside a fixed row or column. When you request one or more fixed rows or one or more fixed columns (or a combination of both), Visual Basic keeps those rows and columns from scrolling when the user scrolls through the grid's values.

As the user views values inside the grid, the user can scroll through the grid, selecting (with the mouse or keyboard) one or more cells in the grid. Through programming, you can update or copy selected cell values to other grid controls, to variables, or to data files.

Table 23.2 contains the property values available for a grid control. Although you've seen several of the properties for other controls, the grid does contain properties unique to the grid control that determine the dimensions of the grid as well as the number of fixed rows and columns.

Table 23.2. The grid control's properties.

Property Description
About... Clicking this About property opens a description dialog box that displays information about the grid control. Most custom controls come with these About dialog boxes that detail copyright information about the custom control. The About property is available only during program design time and does nothing at runtime.
BackColor Specifies the grid control's background color, chosen as a hexadecimal color code or from the color palette. The BackColor describes the nonfixed row and column cell colors. All fixed row and column cells are gray.
BorderStyle Specifies that a border appears around the grid if set to 1-Fixed Single (the default). No border appears if set to 0-None.
Cols Holds the number of columns in the grid.
DragIcon Contains the icon that appears when the user drags the grid around on the form. (You'll only rarely allow the user to move a grid, so the Drag... property settings aren't usually relevant.)
DragMode Holds either 1 for manual mouse dragging requirements (the user can press and hold the mouse button while dragging the control) or 0 (the default) for automatic mouse dragging, meaning that the user can't drag the grid but that you, through code, can initiate the dragging if needed.
Enabled Determines whether the grid can respond to events. If set to True (the default), the grid can respond to events. Otherwise, Visual Basic halts event processing for that particular control.
FillStyle If set to 0-Single (the default), a value is to be assigned only to a single selected cell, and if 1-Repeat, a value is to be assigned to a range of selected cells.
FixedCols Holds the number of fixed columns. The FixedCols value must be at least two fewer than the Cols value.
FixedRows Holds the number of fixed rows. The FixedRows value must be at least two fewer than the Rows value.
FontBold Holds True (the default) if the grid values are to display in boldfaced characters; False otherwise.
FontItalic Holds True (the default) if the grid values are to display in italicized characters; False otherwise.
FontName Contains the name of the grid values' font styles. Typically, you'll use the name of a Windows TrueType font.
FontSize Holds the size, in points, of the font used for the grid values.
FontStrikethru Holds True (the default) if the grid values are to display in strikethru letters (characters with dashes through them); False otherwise.
FontUnderline Holds True (the default) if the grid values are to display in underlined letters; False otherwise.
ForeColor Specifies the color of the characters in the grid values.
GridLines Holds True (the default) if the grid is to control separating row and column lines; False otherwise.
Height Holds the height, in twips, of the grid.
HelpContextID If you add advanced, context-sensitive help to your application, the HelpContextID provides the identifying number for the help text.
Highlight Holds True (the default) or False to determine whether selected cell or cells are to appear highlighted.
Index If the grid is part of a control array, the Index property provides the numeric subscript for each particular grid control.
Left Contains the number of twips from the left edge of the Form window to the left edge of the grid.
Name Contains the name of the control. By default, Visual Basic generates the names Grid1, Grid2, and so on as you add subsequent grids to the form.
Rows Holds the number of rows in the grid.
ScrollBars Holds 0-None, 1-Horizontal, 2-Vertical, or 3-Both (the default) to describe the scroll bars that appear in the grid if the grid requires more row and column space than the grid size allows.
TabIndex Determines that the focus tab order begins at 0 and increments every time you add a new control. You can change the focus order by changing the controls' TabIndex to other values. No two controls on the same form can have the same TabIndex value.
TabStop If True, determines whether the user can press Tab to move the focus to this grid. If False, the grid can't receive the focus.
Tag Unused by Visual Basic. This is for the programmer's use for an identifying comment applied to the grid.
Top Holds the number of twips from the top edge of a grid to the top of the form.
Visible Holds True or False, indicating whether the user can see (and, therefore, use) the grid.
Width Holds the number of twips wide that the grid consumes.


Warning: The grid control enables your user to view and scroll through values but doesn't allow the user to enter new values into the grid. Your program can update the grid, but the user can't.

In addition to the property values that you can set at program design time (those listed in Table 23.2), there are additional property values that your program can add and modify at runtime. Table 23.3 lists the runtime properties available to your code.

Table 23.3. The grid control's runtime properties.

Property Description
Col Contains the column number, counting from 0, of the currently selected cell.
ColAlignment Contains 0 (the default) for left justification of the cell values, 1 for right justification, and 2 for center alignment of the cell values. This property applies only to the cells in nonfixed columns.
ColWidth Holds the width, in twips, of an individual column.
FixedAlignment Contains 0 (the default) for leftjustification of the cell values, 1 for right justification, and 2 for center alignment of the cell values. This property applies only to the cells in fixed columns.
HighLight Holds True or False to indicate whether or not the user selected a cell or a range of cells.
Picture At runtime, you can assign the LoadPicture() procedure (see the previous unit) to display a graphic image in a selected cell.
Row Contains the row number, counting from 0, of the currently selected cell.
RowHeight Holds the height, in twips, of an individual row.
SelEndCol Holds the rightmost column of a selected range.
SelEndRow Holds the bottom row of a selected range.
SelStartCol Holds the leftmost column of a selected range.
SelEndCol Holds the top row of a selected range.
Text Holds the value of any given cell.

The two most frequently used runtime property values are the Row and Col properties. These two properties determine which cell you're currently formatting and which cell you want to assign text to. Before assigning a grid's cell a value, you must set the Row and Col values to the row and column that intersect the cell to assign to.



Note: Many of the property values in Table 23.3 make sense only when the user runs the program because, until then, no cells are selected. The project application at the end of this lesson demonstrates how to use these properties to initialize a grid and respond to the user's selected range of cells.

Review: The grid control is useful for displaying tables of data values for the user. The grid control is a custom control located inside the GRID.VBX file that you must add to any application that uses the grid control.

Monitoring the Mouse Cursor


Concept: Your program can respond to mouse movements and the user's mouse clicks through event procedures. The mouse object supports property values and methods that you can use to monitor the mouse. One important property that you may want to control during the user's mouse movement is the MousePointer property, which determines how the mouse looks when the user moves the mouse over a control.

If you need to, you can monitor the user's mouse movements and clicks. As you've seen throughout this book, Visual Basic takes care of monitoring most of the important mouse functions. For example, you already know that if the user clicks a mouse over a command button, Visual Basic ensures that the command button's Click event procedure automatically executes without your having to worry about looking for the mouse click.

One of the most common ways that a program works with the mouse is to change the appearance of the mouse cursor. In Windows, the term cursor is technically used solely for the mouse cursor, whereas the term caret is used for the text cursor. Despite Microsoft's original design and the technical manuals that promote these "accurate" names, most users and programmers refer to the mouse cursor simply as the mouse cursor and the text cursor as the text cursor. This chapter will continue to use the vernacular.

Table 23.4 contains a list of every mouse cursor shape that you can display. Most controls contain the MousePointer property. For example, command buttons contain a MousePointer property. The value of each control's MousePointer property, described in Table 23.4, determines what shape the mouse cursor takes on when the user moves the mouse over that control.

Table 23.4. The thirteen mouse cursor values.

Value Description
0-Default The cursor assumes the control's default mouse cursor shape. Each control has its own default mouse cursor shape. Most controls use the common mouse cursor arrow for the default shape.
1-Arrow The typical arrow mouse pointer. (Generally, this is the same shape as most controls' default mouse cursor shape.)
2-Cross A crosshair pointer.
3-I-Beam The vertical mouse cursor most often used as a text cursor.
4-Icon A small black square within another square.
5-Size The sizing cursor that looks like a plus sign with arrows pointing in the four directions.
6-Size NE SW A diagonal arrow pointing northeast and southwest.
7-Size N S A vertical arrow pointing north and south.
8-NW SE A diagonal arrow pointing northwest and southeast.
9-W E A vertical arrow pointing west and east.
10-Up Arrow An arrow pointing straight up.
11-Hourglass The hourglass shape.
12-No Drop The familiar roadsign "No" circle with a slash through it.

Depending on the application, you may need to keep the user from clicking on a control. For example, you may want to ignore all clicks of a command button that prints a report of daily activities until after a 5:00 p.m. closing time. Your application could respond to the command button's click after 5:00 p.m. and return without responding if the time is before 5:00 p.m.

During the time that the command button is to be ignored, you could set the command button's MousePointer shape to the 12-No Drop mouse cursor. Therefore, the user's mouse cursor changes to the no drop shape whenever the user moves the mouse cursor over that particular command button. The mouse cursor could remain the standard arrow pointer for the other controls. After 5:00 p.m., inside the click procedure, you could respond to the command button click by printing the report and also set the MousePointer property to 1-Default (or 1-Arrow, which is the same shape for command buttons because the default mouse cursor is the arrow for command button controls).

Table 23.5 lists the CONSTANT.TXT file's named constants that you can use to set the MousePointer property values.

Table 23.5. The CONSTANT.TXT's named MousePointer property values.

Property Description
DEFAULT The control's default mouse cursor shape.
ARROW The typical mouse cursor arrow.
CROSSHAIR A crosshair.
IBEAM An I-Beam (the typical text cursor)
ICON_POINTER An icon square within a square
SIZE_POINTER The sizing cursor that looks like a plus sign with arrows pointing in the four directions.
SIZE_NE_SW A diagonal arrow pointing northeast and southwest.
SIZE_N_S A vertical arrow pointing north and south.
SIZE_NW_SE A diagonal arrow pointing northwest and southeast.
SIZE_W_E A vertical arrow pointing west and east.
UP_ARROW The straight up arrow.
HOURGLASS The hourglass waiting shape.
NO_DROP The "No" circle with a line through it.

Stop and Type: Listing 23.2 contains the code found in this book's MOUSECH.MAK application. The program demonstrates how the mouse cursor can change depending on the value of an option button. The program produces the before 5:00 p.m. and after 5:00 p.m. results described earlier in this section.

Review: Most controls support the MousePointer property that determines the shape that the mouse takes on when the user moves the mouse over another control.

Listing 23.2. Code that changes the shape of the mouse depending on the value of option buttons.


1: Sub optAfter_Click ()

2: ' After 5:00 pm so fix mouse over command

3: 
cmdReport.MousePointer = ARROW

4: End Sub

5:

6: Sub optBefore_Click ()

7: ' Before 5:00 pm so negate mouse over command

8: cmdReport.MousePointer = NO_DROP

9: lblReport.Visible = False

10: End Sub

11:

12: Sub cmdExit_Click ()

13: End

14: End Sub


15:

16: Sub cmdReport_Click ()

17: ' Beep if it's before 5:00

18: If optBefore.Value = True Then

19: Beep

20: lblReport.Visible = False

21: Else

22: lblReport.Visible = True

23: End If

24: End Sub

Output: Figure 23.4 shows what happens to the shape of the mouse cursor when the user moves the mouse cursor over the command button before 5:00 p.m.

Figure 23.4. The mouse cursor tells the user that the command button won't respond.

Analysis: When the program begins, the first option button named optBefore is set to True. Therefore, the application assumes that the time is before 5:00 p.m. and the command button's MousePointer value is set to NO_DROP as, shown in Figure 23.4.



Note: The program could use the Time$() function to check the actual time to see whether the command button can be active or not. However, the option buttons offer you an easier method for practicing with the two mouse cursors; you don't have to wait until 5:00 p.m. to see the difference.

As soon as the user clicks the After 5:00 p.m. option button named optAfter, the optAfter_Click() event procedure changes the command button's MousePointer property to ARROW in line 3.

When the user clicks the Report command button, the command button's cmdReport_Click() event procedure executes (line 16). If the Before 5:00 pm option button is set, line 19 beeps. If the After 5:00 pm option button is set, a small label appears beneath the command button to inform you that the report can now be printed. (The label, named lblReport, is set to False when the program first begins.)

Capturing Mouse Clicks and Movements


Concept: The mouse supports three events that return movement and clicking information to the user through the form of event procedures. Unlike most event procedures, the mouse event procedures use arguments that return information such as the location of the mouse and the button that was clicked.

Table 23.6 lists the events most often supported by applications that deal with the mouse. These events return events for which you can write event procedures to respond to the user's mouse commands.

Table 23.6. The events most often associated with the mouse.

Event Description
DblClick Generated when the user double-clicks a mouse button
MouseDown Generated when the user presses a mouse button
MouseMove Generated when the user moves the mouse
MouseUp Generated when the user lets up on a mouse button

The MouseDown, MouseMove, and MouseUp event procedures open with argument lists when you select them inside the Code window. Suppose that you wanted to respond to user clicks of the mouse when the user clicked the mouse over a form named frmApp. Here is the three event procedures' wrapper code that Visual Basic would open for you when you selected MouseDown, MouseMove, or MouseUp from the Code window's dropdown Proc listbox:


Sub frmApp_MouseDown (Button As Integer, Shift As 
Integer, X As Single, Y As Single)

End Sub

Sub frmApp_MouseMove (Button As Integer, Shift As Integer, X As Single, Y As Single)

End Sub

Sub frmApp_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)

End Sub

The argument lists for these procedures are so long that you may have to horizontally scroll the Code window to see the entire argument list.

Table 23.7 lists the descriptions for these four argument values. The values returned in the arguments describe something about the user's mouse press or mouse release. Your code can check the argument values to determine what needs to be known. For example, if you want to know the location of the mouse when the user clicked the mouse on the form, the X and Y arguments describe the number of twips from the left and top edge of the form that the user clicked.

Table 23.7. The MouseDown() and MouseUp() arguments.

Argument Description
Button Holds a number that represents the mouse button pressed. The value is 1 for the left button press, 2 for the right button press, and 4 for both or for the center button on a three-button mouse.
Shift Describes the shifting key (if any) pressed at the same time that the user clicked or moved the mouse button. Shift holds 1 if the user pressed Shift during the mouse click, 2 for the Ctrl key, and 4 for the Alt key. If Shift contains a number other than 1, 2, or 4, that number is the sum of two or more key presses just described. For example, if the user presses the Alt keys at the same time that the user pressed the mouse button, the value of Shift will be 5.
X Contains the horizontal twip form measurement where the user clicked or moved the mouse button.
Y Contains the vertical twip form measurement where the user clicked or moved the mouse button.

If the user double-clicks the mouse, Visual Basic generates both a MouseUp and a DblClick event. The MouseUp event procedure returns location and button information about the double-click (see Table 23.6), and the DblClick event should contain the code that you want executed upon the double-clicking of the mouse.

The MouseUp event occurs every time that the user lets up on a mouse button, whether the user lets up from a single click or from a double-click. Therefore, if you want to respond to a single click's MouseUp event but not to a double-click's MouseUp event, you'll have to set a module variable inside the DblClick event procedure that you can test to see which kind of release, a click or double-click, generated the MouseUp event.



Note: Depending on your computer, Visual Basic might generate a mouse movement event procedure every time that the user moves the mouse 10 to 15 twips across the form. Visual Basic doesn't generate a mouse movement event for every twip movement; most computers couldn't keep up with the events that would execute that fast.

Stop and Type: Listing 23.3 contains a sample MouseUp() event procedure that demonstrates how the code can use the arguments to the procedure to learn more about the mouse movement. The code prints mouse information. You could write a similar set of mouse event procedures for the other mouse events and get a printed log of the mouse actions as you moved, clicked, and double-clicked the mouse.

Review: The mouse movement and clicking event procedures need additional information when a mouse event occurs. Through the argument list to the mouse event procedures, you can determine which button the user pressed, which key the user was pressing at the time of the click or mouse movement, and exactly where on the form the mouse was when the user generated the event.

Listing 23.3. Code that checks the mouse after a button release.


1: Sub frmApp_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)

2: ' Display text on the printer 
that describes the

3: ' mouse button press. The semicolon at the end of

4: ' the Printer.Print statements forces Visual

5: ' to keep the printer cursor on the same Print

6: ' line.

7:

8: ' Tell the user about the button pressed

9: Printer.Print 
"Up: The button you released was the ";

10: Select Case Button

11: Case 1: Printer.Print "Left ";

12: Case 2: Printer.Print "Right ";

13: Case 4: Printer.Print "Middle ";

14: End Select

15: Printer.Print 
"button"

16:

17: Printer.Print "The mouse was at X position: ";

18: Printer.Print X;

19: Printer.Print "and Y position: ";

20: Printer.Print Y

21: ' Print a blank line to separate for subsequent output

22: Printer.Print


23:

24: End Sub

Analysis: Line 1 collects the mouse button's release arguments. The argument tell the event procedure which button the user just released, which key, if any, was pressed at the time, and the exact twip coordinates of the mouse when the user released the mouse.

The rest of the event procedure proceeds to print the mouse information gathered from the arguments. Keep in mind that this event procedure will execute every time the user releases either mouse button, so the printer's output will continue as the user continues clicking the mouse.

Homework



General Knowledge


  1. How many different scroll bar controls are there?

  2. How do the scroll bars eliminate the typing of specific entries?

  3. What do the LargeChange and SmallChange properties indicate?

  4. How does a user trigger a LargeChange change of a scroll bar's value?

  5. What do the Min and Max properties indicate?

  6. What are the preferred naming prefixes for scroll bar controls?

  7. True or false: You can initialize a scroll bar using a variable defined as an Integer data type.

  8. What must you do to add the grid control to the Toolbox window?

  9. What is the name of the custom control file that holds the grid control?

  10. True or false: The user can read, change, and enter new values into the grid.

  11. What is a cell?

  12. True or false: When you adjust the grid's physical size on the form, the grid's size must be large enough to display all of the grid's rows and columns.

  13. When can scroll bars appear on a grid?

  14. What two property values, available only at runtime, specify an individual cell that you want to change or initialize with code?

  15. What are the four property values that define selected cells?

  16. How many different shapes can the mouse appear as over a control?

  17. Which property describes the mouse cursor's appearance?

  18. Technically, what is the text cursor called?

  19. Technically, what is the mouse cursor called?

  20. When would the 0-Default and 1-Arrow MousePointer property values differ?

  21. Which mouse cursor might be useful for reminding the user to wait a few seconds until a calculation completes?

  22. How do the mouse movement events generate extra information that describe the buttons and location of the mouse?

  23. True or false: When the user double-clicks a mouse button, two events actually take place.


Find the Bug


  1. Kelly wants to use a grid control to hold a 5-by-5 table of weight readings that he needs for a chemistry experiment. After Kelly initializes the table, through code, and then presses a command button, he wants the code to fix the rows and columns for the entire table so that the user can't select additional cells. Sadly, Visual Basic imposes a limit that doesn't allow Kelly the freedom to fix all rows and columns. What is the maximum number of rows and columns that can be fixed in a table?


Write Code That...


  1. Suppose that you were writing a program that needed the user to enter a temperature value from a range of 32 to 212 degrees. You want the user to enter the temperature reading using the relative position on a scroll bar. When the user clicks one of the scroll bar arrows, the scroll bar should adjust by 3 degrees. When the user clicks in the shaft on either side of the scroll bar, the scroll bar should adjust by 8 degrees. What would be appropriate Min, Max, SmallChange, and LargeChange properties?


Extra Credit


Write a program that contains one command button labeled Change Mouse that displays a different mouse cursor every time the user clicks the button. Use a module variable to keep track of the current mouse pointer's value.

Previous Page Page Top TOC Next Page