vel12.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 2,319 行 · 第 1/4 页
HTM
2,319 行
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Left</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The number of twips from the left edge of the Form window to the left edge of the check box.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>MousePointer</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The shape that the mouse cursor changes to if the user moves the mouse cursor over the check box. The possible values are from 0 to 12 and represent a range of different shapes that the mouse cursor can take. (See Lesson 12.)</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Name</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The name of the control. By default, Visual Basic generates the names Check1, Check2, and so on as you add subsequent check boxes to the form.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>TabIndex</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The focus tab order begins at 0 and increments every time that 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 check box. If False, the check box 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 check box.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Top</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The number of twips from the top edge of a check box 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>Either 0-Unchecked (the default), 1-Checked, or 2-Grayed, indicating whether the check box is selected.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Visible</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>True or False, indicating whether the user can see (and, therefore, use) the check box.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Width</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>The number of twips wide that the check box consumes.</FONT></TABLE><P>Table 12.4 contains the events available for check box controls. As with option buttons, the Click event is generally the most commonly coded event procedure.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 12.4. The check box's events.</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>Event</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Click</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Occurs when the user clicks the check box with the mouse</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>DragDrop</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Occurs when a dragging operation over the check box completes</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>DragOver</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Occurs during a drag operation</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>GotFocus</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Occurs when the check box receives the focus</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>KeyDown</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Occurs when the user presses a key as long as the KeyPreview property is set to True for the controls on the check box; otherwise, the control gets the KeyDown event</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>KeyPress</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Occurs when the user presses a key over the check box</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>KeyUp</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Occurs when the user releases a key</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>LostFocus</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Occurs when the check box loses the focus</FONT></TABLE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Load and run this book's project named CHECK.MAK. Listing 12.2 contains the code for the project's event procedures. The program allows the user to choose from one to four special formatting options for the poem inside the boxed label. The poem can accept any or all four formatting options, so the check box controls enable the user to select one or more of the formatting options.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The check box controls allow more than one selection from the user at a time, unlike the option button controls, which allow only a single option to be selected at a time.<BR><P><FONT COLOR="#000080"><B>Listing 12.2. The code for the CHECK.MAK project.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub Form_Load ()2: ' Fill the label with a poem.3: ' The poem will display initially4: ' using the default label properties.5: Dim Newline As String6: ' The newline sends a carriage return and7: ' line feed sequence to the poem so that8: ' the poem can appear as a multiple-line9: ' poem.10: Newline = Chr$(13) + Chr$(10)11: ' Fill the label with the poem12: ' and concatenate the newline characters13: lblPoem = "Visual Basic is the best."14: lblPoem = lblPoem & Newline15: lblPoem = lblPoem & "It's much better than the rest."16: lblPoem = lblPoem & Newline17: lblPoem = lblPoem & "If you ever hear differently,"18: lblPoem = lblPoem & Newline19: lblPoem = lblPoem & "Give them major misery!"20: End Sub21:22: Sub chkBack_Click ()23: ' Set the poem label's background to Red or white24: If (lblPoem.BackColor = RED) Then25: lblPoem.BackColor = WHITE ' Default26: Else27: lblPoem.BackColor = RED28: End If29: End Sub30:31: Sub chkFore_Click ()32: ' Set the poem label's foreground to Green or Black33: If (lblPoem.ForeColor = GREEN) Then34: lblPoem.ForeColor = BLACK ' Default35: Else36: lblPoem.ForeColor = GREEN37: End If38: End Sub39:40: Sub chkItal_Click ()41: ' Set the poem label's caption to italicize or not42: If (lblPoem.FontItalic = True) Then43: lblPoem.FontItalic = False ' Default44: Else45: lblPoem.FontItalic = True46: End If47: End Sub48:49: Sub chkUnd_Click ()50: ' Set the poem label's caption to underline or not51: If (lblPoem.FontUnderline = True) Then52: lblPoem.FontUnderline = False ' Default53: Else54: lblPoem.FontUnderline = True55: End If56: End Sub57:58: Sub cmdExit_Click ()59: End60: End Sub</FONT></PRE><P>[ic:output]Figure 12.5 contains a figure that shows the poem when the user selects two of the check box options.<BR><P><B> <A HREF="12vel05.gif">Figure 12.5. Selecting two check boxes at the same time.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Lines 1 through 20 contain the longest Form_Load() event procedure that you've seen in this book. The purpose of the procedure is to initialize the label control with the multiline poem. There is no MultiLine property for labels, so you have to trick Visual Basic into displaying multiple lines inside the label.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>control character</I> produces an action, not a text character.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The Chr$() function (called the <I>character string function</I> or, sometimes, the <I>character function</I>) accepts a number inside its parentheses. The number must come from the ASCII table (<A HREF="velxa.htm">Appendix A</A>). Visual Basic converts that number to its ASCII character equivalent. ASCII number 13 is the <I>carriage return</I> character and is a special control character that sends the cursor to the beginning of the line, which, in the case of a label, is the beginning column in the label. ASCII number 10 is the <I>line feed </I>character that sends the cursor to the next line on the screen. The effect of the concatenated ASCII value of 13 and 10 (line 10) is that a special double control character is created that, when displayed in a label, sends the cursor to the beginning of the label's next line.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The poem label's Alignment property is set to 2-Center, so the lines inside the label always display as centered within the label's border.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Lines 13 through 19 then build the multiline poem by concatenating one line and the newline combination string variable to the label's Caption property.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>But wait! Lines 13 through 19 don't even mention the Caption property! It appears that the poem's text is being sent to the label itself and not to any property. It turns out that each control has a default property that, unless you specify a different property name, acts as the default property. Therefore, Visual Basic assigns all of the poem's lines to the Caption property in lines 13 through 19 because the Caption property is the default property for labels.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Lines 22 through 56 contain the four check box Click event procedures that set or reset each of the four poem properties described by the check box. The If-Else checks first to see whether the poem's property is set to the checked value. If so, the true part of the If sets the property back to its default state. If the property already contains the default value, the Else sets the property to the new state.<BR><P>As always, the application contains an Exit command button event procedure in lines 58 through 60 that terminate the program at the user's request.<BR><BR><A NAME="E68E94"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Multiple Sets of Option Buttons</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Although the user can select only one option button from all the option buttons on the form, there is a way to place multiple sets of option buttons on a form inside frames. The user can select only one option button within any frame at a time.<BR><P>Figure 12.6 shows where the frame control resides on the Toolbox window. The frame control is a holder of other controls. By placing more than one frame on a form, you can group more than one set of option buttons on the form together.<BR><P><B> <A HREF="12vel06.gif">Figure 12.6. The location of the frame control.</A></B><BR><P>When you want to place several sets of options buttons on a form, be sure to place more than one frame on the form first. The frames must be large enough to hold as many option buttons as each group requires.<BR><P>Be careful about placing option buttons when you want to frame them within frame controls. You must draw the option buttons <I>inside the frame</I>. You can't create the option buttons elsewhere and move them into the frame. For most applications, you can double-click controls to place them in the middle of the form, and then drag the controls to their final location and resize them. When placing option buttons inside frames, you must click (not double-click) the option button control on the Toolbox window and then draw the option button by clicking the mouse inside the frame and dragging the mouse until you've approximated the option button's size. When you release the mouse button, Visual Basic will draw the option button in the size you drew it.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Load and run this book's project named OPTIONS.MAK. Listing 12.3 contains the code for the project's event procedures. The program is similar to the CHECK.MAK application that you saw in the previous section. Framed groups of option buttons turn selected poem formatting properties on or off.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The frame enables you to group option button controls together. Be sure to place the frame before drawing option button controls inside the frame.<BR><P><FONT COLOR="#000080"><B>Listing 12.3. The code for the framed option buttons.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub Form_Load ()2: ' Fill the label with a poem.3: ' The poem will display initially4: ' using the default label properties.5: Dim Newline As String6: ' The newline sends a carriage return and
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?