📄 vel23.htm
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel23.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel24.htm"><LINK REL="Previous" HREF="velp11.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel23.htm</H2><P ALIGN=LEFT><A HREF="velp11.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel24.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E172" >What You'll Learn</A><LI><A HREF="#E68E173" >Scrolling the Scroll Bars</A><LI><A HREF="#E68E174" >Prepare for the Grid Control</A><LI><A HREF="#E68E175" >Using the Grid Control</A><LI><A HREF="#E68E176" >Monitoring the Mouse Cursor</A><LI><A HREF="#E68E177" >Capturing Mouse Clicks and Movements</A><LI><A HREF="#E68E178" >Homework</A><UL><LI><A HREF="#E69E163" >General Knowledge</A><LI><A HREF="#E69E164" >Find the Bug</A><LI><A HREF="#E69E165" >Write Code That...</A><LI><A HREF="#E69E166" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E34"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 12, Unit 23</B></FONT></CENTER></H1><BR><A NAME="E67E37"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>The Scroll Bars, Grid, and Mouse</B></FONT></CENTER></H2><BR><BR><A NAME="E68E172"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>Scrolling the scroll bars<BR><BR><LI>Preparing for the grid control<BR><BR><LI>Using the grid control<BR><BR><LI>Monitoring the mouse cursor<BR><BR><LI>Capturing mouse clicks and movements<BR><BR></UL><P>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.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>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.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E173"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Scrolling the Scroll Bars</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>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.<BR><P>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.<BR><P><B><A HREF="23vel01a.gif">Figure 23.1a. The scroll bars and their descriptions.</A></B><BR><P><B> <A HREF="23vel01b.gif">Figure 23.1b. The scroll bars and their descriptions.</A></B><BR><P>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.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 23.1. The scroll bar properties.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Property</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>DragIcon</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>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.)</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>DragMode</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>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.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Enabled</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>If set to True (the default), the scroll bar can respond to events. Otherwise, Visual Basic halts event processing for that particular control.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Height</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains the height, in twips, of the scroll bar.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>HelpContextID</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>If you add advanced, context-sensitive help to your application, the HelpContextID provides the identifying number for the help text.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Index</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>If the scroll bar is part of a control array, the Index property provides the numeric subscript for each particular scroll bar.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>LargeChange</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Specifies the amount that the scroll bar changes when the user clicks within the scroll bar's shaft area.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Left</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds the number of twips from the left edge of the Form window to the left edge of the scroll bar.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Max</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>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).</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Min</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates the minimum number of units the scroll bar value represents at its lowest setting. The range is from 1 (the default) to 32767.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>MousePointer</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>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.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Name</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>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.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>SmallChange</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Specifies the amount that the scroll bar changes when the user clicks an arrow at either end of the scroll bar.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>TabIndex</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>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.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>TabStop</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>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.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Tag</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Unused by Visual Basic. This is for the programmer's use for an identifying comment applied to the scroll bar.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Top</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds the number of twips from the top edge of a scrollbar to the top of the form.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Value</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains the unit of measurement currently represented by the position of the scroll bar.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Visible</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains either True or False, indicating whether the user can see (and, therefore, use) the scroll bar.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Width</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds the number of twips wide that the scroll bar consumes.</FONT></TABLE><P><B>Tip: </B>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.<P>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.<BR><P>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.<BR><P>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:<BR><UL><UL><P>Min: 5</UL></UL><UL><UL><P>Max: 100</UL></UL><UL><UL><P>SmallChange: 1</UL></UL><UL><UL><P>LargeChange: 5<BR></UL></UL><P>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.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>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.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>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.<BR><P><FONT COLOR="#000080"><B>Listing 23.1. The code for the SCROLL.MAK application.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Option Explicit2:3: Sub Form_Load ()4: ' Set initial scroll bar values5: hsbBar.Value = 1800 ' Circle's default width6: vsbBar.Value = 1800 ' Bar's default height7: End Sub8:9: Sub hsbBar_Change ()10: ' As user clicks the scroll bar,11: ' the width of the circle adjusts12: shpCircle.Width = hsbBar.Value13: End Sub14:15: Sub vsbBar_Change ()16: ' As user clicks the scroll bar,17: ' the height of the bar adjusts18: shpBar.Height = vsbBar.Value19: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Here are the vital horizontal scroll bar properties that were set during the design of the form:<BR><UL><UL><P>Min: 50</UL></UL><UL><UL><P>Max: 2100</UL></UL><UL><UL><P>SmallChange: 50</UL></UL><UL><UL><P>LargeChange: 100<BR></UL></UL><P>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.<BR><P>Here are the vital vertical scroll bar properties that were set during the design of the form:<BR><UL><UL><P>Min: 50</UL></UL><UL><UL><P>Max: 2300</UL></UL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -