vel12.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 2,319 行 · 第 1/4 页
HTM
2,319 行
7: ' 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 optGreen_Click ()23: lblPoem.BackColor = GREEN24: End Sub25:26: Sub optItal_Click ()27: lblPoem.FontItalic = True28: End Sub29:30: Sub optNoItal_Click ()31: lblPoem.FontItalic = False32: End Sub33:34: Sub optNoUnd_Click ()35: lblPoem.FontUnderline = False36: End Sub37:38: Sub optRed_Click ()39: lblPoem.BackColor = RED40: End Sub41:42: Sub optUnd_Click ()43: lblPoem.FontUnderline = True44: End Sub45:46: Sub optWhite_Click ()47: lblPoem.BackColor = WHITE48: End Sub49:50: Sub cmdExit_Click ()51: End52: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Figure 12.7 contains the OPTIONS.MAK application after the user selects an option in each of the three frames.<BR><P><B> <A HREF="12vel07.gif">Figure 12.7. The framed option buttons allow for multiple settings.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>The difficult part of framed option controls is placing the option buttons inside the frames, and even that's not extremely difficult. After you draw the option buttons and the frames, the usual event procedures that you've been writing work for the controls.<BR><P>Lines 22 through 48 contain the Click event procedures that set the poem's label formatting properties.<BR><BR><A NAME="E68E95"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Control Arrays Simplify</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>By putting one or more similar controls inside a control array, you gain the advantage of writing a single event procedure that can handle more than one control on the form.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>control array</I> is a list of controls with the same name.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>When you place more than one control of the same control type on a form and assign the same Name property to those controls, you're creating a control array. As with variable arrays, a subscript that begins at 0 differentiates one control from another. The Index property of each control in the array contains that control's subscript location in the array. If you want to renumber the controls in the array to begin at 1, you can do so by changing the first zero-based control's Index value to 1.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Each control in a control array must be the same data type.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>If there were five text boxes in a control array named txtBoxes, each individual control would be named txtBoxes(0), txtBoxes(1), txtBoxes(2), txtBoxes(3), and txtBoxes(4).<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>[ic: Note]As soon as you place a control on the form that has the same name as an existing control, Visual Basic makes sure that you want to begin a control array by issuing the warning message box shown in Figure 12.8. The designers of Visual Basic knew that you may accidentally attempt to place two controls on the same form with the same name, and the message box makes sure of your intent to create a control array. If you select the No command button to the warning message box, Visual Basic returns the second control to a its default name.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><B> <A HREF="12vel08.gif">Figure 12.8. Visual Basic ensures that you want a control array.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Load and run this book's project named CHECK2.MAK. Listing 12.4 contains the code for the project's chkAll_Click() event procedure. The program is similar to the CHECK.MAK application that you saw in the previous section, except that, instead of four separate check box event procedures, CHECK2.MAK contains a single check box control array named chkAll.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>By placing several controls in a control array, you can reference each control by its subscript rather than by a different name for each control. Rather than write several event procedures, you need to write only a single event procedure.<BR><P><FONT COLOR="#000080"><B>Listing 12.4. The code for control array's event procedure.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub chkAll_Click (Index As Integer)2: ' A single Select Case3: ' handles all check box formats4: Select Case Index5: Case 0:6: ' Set the poem label's background to Red or white7: If (lblPoem.BackColor = RED) Then8: lblPoem.BackColor = WHITE ' Default9: Else10: lblPoem.BackColor = RED11: End If12: Case 1:13: ' Set the poem label's foreground to Green or Black14: If (lblPoem.ForeColor = GREEN) Then15: lblPoem.ForeColor = BLACK ' Default16: Else17: lblPoem.ForeColor = GREEN18: End If19: Case 2:20: ' Set the poem label's caption to italicize or not21: If (lblPoem.FontItalic = True) Then22: lblPoem.FontItalic = False ' Default23: Else24: lblPoem.FontItalic = True25: End If26: Case 3:27: ' Set the poem label's caption to underline or not28: If (lblPoem.FontUnderline = True) Then29: lblPoem.FontUnderline = False ' Default30: Else31: lblPoem.FontUnderline = True32: End If33: End Select34: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>The single Select Case statement handles all four check boxes. There is only one single check box Click() procedure in the entire application even though there are four check boxes. Each check box is named chkAll, so they all a control array of check boxes and there will be only one event procedure for each event that the programmer wants to program.<BR><P>Therefore, whenever the user clicks <I>any</I> of the check boxes, the chkAll_Click() event procedure executes. How does the procedure know <I>which</I> check box triggered the event? Line 1, the event procedure's first line, gives you a good clue. Visual Basic sends the Index value of the check box that the user clicked to the event procedure. The parentheses is a mechanism that Visual Basic uses to collect values such as the index to the control array that triggered the event. You'll understand such parenthetical values in Lesson 8, but until then, be assured that after the chkAll_Click() procedure executes, an Index variable will contain the subscript of the check box in the control array that the user selected.<BR><P>The Select Case is a consolidation of Listing 12.2's multiple check box Click() event procedures. As you can see, the control array saves time and memory because a single event procedure can now handle all the events in the control array.<BR><BR><A NAME="E68E96"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E81"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>Which control offers a single choice from a multiple set of choices?<BR><BR><LI>Which control is best for allowing the user to select from one or more options?<BR><BR><LI>Why are option buttons sometimes called <I>radio buttons</I>?<BR><BR><LI>What happens if the user selects an option button that's different from the option button currently selected?<BR><BR><LI>True or false: You can write code ensuring that no option button is selected when the program first executes.<BR><BR><LI>What does the Alignment property do for option buttons and check boxes?<BR><BR><LI>What property determines whether an option button is selected?<BR><BR><LI>What property determines whether a check box is selected?<BR><BR><LI>True or false: One or more Value properties can be set for option buttons on a form (assume that the option buttons aren't placed in frames).<BR><BR><LI>True or false: One or more Value properties can be set for option buttons on a form (assume that the option buttons are placed in frames).<BR><BR><LI>What is a frame?<BR><BR><LI>True or false: You can place one frame at the most on a form at any one time.<BR><BR><LI>What is a <I>control character?</I><BR><BR><LI>Describe how to initialize a label control with multiline text.<BR><BR><LI>What does the Chr$() function do?<BR><BR><LI>What is a <I>control array</I>?<BR><BR><LI>If a control array contains seven controls, how many names do the controls have?<BR><BR><LI>How do you distinguish between values in a control array?<BR><BR><LI>What is the default starting subscript for control arrays?<BR><BR><LI>How many event procedures do you have to write for a control array that contains eight controls?<BR><BR><LI>What property holds a control array's subscript number?<BR><BR><LI>How does Visual Basic make sure that you want to create a control array?<BR><BR></OL><BR><A NAME="E69E82"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Write the opening line of an DblClick event procedure for a command button control array named cmdAll.<BR><BR></OL><P><B>Find the Bug</B><OL><LI>Opal is trying her hand at frames and option buttons. First, Opal places three frames on her form. Then, Opal double-clicks the option button control on the Toolbox window nine times and moves each control to its appropriate frame. Opal doesn't seem to be able to select more than one option button at a time on the entire form, even though the three sets of option buttons appear on different frames. What is Opal doing wrong?<BR><BR></OL><BR><A NAME="E69E83"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><OL><LI>What character does the following assignment statement place in the string variable named MyGrade?<BR>MyGrade = Chr$(65)<BR><BR></OL><P>Create a form that contains ten option buttons, two sets of five in two frames. Put the ten option buttons inside two control arrays. Label each option button One, Two, Three, Four, and Five. Write two Click event procedures, one for each option button control array, that beep the PC's speaker once for whatever option buttons are selected at that time. In other words, if the user clicks the left frame's Three option button, and the right frame already has the value Two selected, you'll click the speaker five times.<P ALIGN=LEFT><A HREF="vel11.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="velp06.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?