vel11.htm

来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 2,549 行 · 第 1/4 页

HTM
2,549
字号
<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>AddItem</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Adds a single item to the list box</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Clear</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Clears all items from the list</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>List</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>A string array that holds each item within the list box</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>ListCount</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The total number of items in a list box</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>RemoveItem</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Removes a single item from a list box</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Selected</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Determines whether the user has selected a particular item in the list box</FONT></TABLE><P>Use the AddItem method to add properties to a list box control. Suppose that you want to add a few state names to a list box named lstStates. The following code adds the state names:<BR><PRE><FONT COLOR="#000080">' Add several states to a list box controllstStates.AddItem &quot;Arizona&quot;lstStates.AddItem &quot;Alabama&quot;lstStates.AddItem &quot;Oklahoma&quot;lstStates.AddItem &quot;New York&quot;lstStates.AddItem &quot;California&quot;lstStates.AddItem &quot;Nebraska&quot;lstStates.AddItem &quot;Ohio&quot;lstStates.AddItem &quot;Florida&quot;lstStates.AddItem &quot;Texas&quot;lstStates.AddItem &quot;South Dakota&quot;lstStates.AddItem &quot;Nevada&quot;lstStates.AddItem &quot;Illinois&quot;lstStates.AddItem &quot;New Mexico&quot;</FONT></PRE><P>This code would most likely appear in the Form_Load() event procedure so that the list boxes are initialized with their values before the form appears and before the list boxes are seen on the form.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The list box acts a little like an array.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Each item in a list box has a subscript just as each element in an array has a subscript. The first item that you add to a list box has a subscript of 0, the second has a subscript of 1, and so on. To remove the third item from the list box, therefore, your code can apply the RemoveItem method, as follows:<BR><BR><PRE><FONT COLOR="#000080">lstStates.RemoveItem(2) ' 3rd item has a subscript of 2</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Caution:</B> Keep in mind that, as you remove items from a list box, the remaining item subscripts adjust accordingly. Therefore, if a list box contains seven items, each item has a subscript that ranges from 0 to 6. If you remove the fourth item, the list box items will then range from 0 to 5; the subscript of 5 will indicate the same item that the subscript of 6 indicated before the RemoveItem method removed the fourth item.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>If you want to remove all items from the list box, use the Clear method. The following simple method removes all the state names from the state list box:<BR><BR><PRE><FONT COLOR="#000080">lstStates.Clear ' Remove all items</FONT></PRE><P>You can assign individual items from a list box control that contains data by using the List method. You must save list box values in string or variant variables unless you convert list box items to a numeric data type using Val() first. The following assignment statements store the first and fourth list box item in two string variables:<BR><PRE><FONT COLOR="#000080">FirstStringVar = lstStates.List(0)SecondStringVar = lstStates.List(3)</FONT></PRE><P>The ListCount method always provides the total number of items in the list box control. For example, the following statement stores the number of list box items in a numeric variable named Num:<BR><BR><PRE><FONT COLOR="#000080">Num = lstStates.ListCount</FONT></PRE><P>The Selected method returns either a true or false value that determines whether a user has selected a list box item. The Selected method returns true for possibly more than one list box item if the MultiSelect property is set to either 1-Simple or 2-Extended. Those properties indicate that the user can select more than one item at once. Figure 11.5 shows a list box with several items selected at the same time.<BR><P><B> <A HREF="11vel05.gif">Figure 11.5. A list box with a </B><B>MultiSelect</B><B> property set to </B><B>1</B><B> or </B><B>2</B><B>.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>The code in Listing 11.2 stores every selected item of a list box named lstStates in a string array. The string array is defined with enough elements to hold all the items if the user happens to have selected all fifty items that were first added to the list box.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>A list box control holds one or more values through which the user can scroll. Unlike many other controls, the user can only look at and select items in the list box, but not add items to the list box. Through methods, you can, however, add items, delete items, and check for selected items in the list box by using the methods inside your code procedures.<BR><P><FONT COLOR="#000080"><B>Listing 11.2. Storing all selected values in a string array.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Dim SelectStates(50) As String2: Dim StSub As Integer ' State array subscript3: StSub = 14: For Ctr = 0 To 49 ' 50 states in all5: If (lstStates.Selected(Ctr) = True) Then6: SelectStates(StSub) = lstStates.List(Ctr)7: StSub = StSub + 18: End If10: Next Ctr</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 2 must define a subscript that will keep track of the state's string array. Line 3 initializes the starting subscript to 1 because the zero subscript is ignored throughout this book's programs; this is the standard followed by most Visual Basic programmers.<BR><P>Lines 4 through 10 contain a loop that steps through all fifty states that were stored in the list box earlier (perhaps in the Form_Load() event procedure). If a state has been selected by the user (line 5 checks for the selection), the Selected method returns a true result and line 6 stores the selected list box item in the string array. Line 7 increments the string array's subscript to prepare the state array subscript for further possible assignments in subsequent iterations through the loop.<BR><BR><A NAME="E68E89"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Combo Boxes</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Combo boxes work a little like list boxes except that the user can add items to a combo box at runtime. There are three kinds of combo boxes determined by the Style property. The AddItem and RemoveItem methods are popular for combo boxes, although all of the list box methods that you learned in the previous lesson apply to combo boxes as well.<BR><P>Figure 11.6 shows the location of the combo box control on the Toolbox window. No matter what kind of combo box you want to place on the form, you'll use the same combo box control on the toolbox to add the combo box to the form.<BR><P><B> <A HREF="11vel06.gif">Figure 11.6. The location of the combo box control.</A></B><BR><P>There are three kinds of combo boxes, as follows:<BR><UL><LI>A <I>dropdown combo box</I> takes up only a single line on the form unless the user opens the combo box (by pressing the combo box's down arrow) to see additional values. The user can enter additional items at the top of the dropdown combo box and select items from the combo box.<BR><BR><LI>A <I>simple combo box</I> always displays items as if they were in a list box. The user can add items to the combo box list as well.<BR><BR><LI>A <I>dropdown list box</I> is a special list box that the user can't enter new items into, but that normally appears closed to a single line until the user clicks the down arrow button to open the list box to its full size. Technically, dropdown list boxes are not combo box controls but work more like list boxes. The reason dropdown list boxes fall inside the combo box control family is that you place dropdown list boxes on forms by clicking the combo box control and setting the appropriate combo box property (Style).<BR><BR></UL><P>Figure 11.7 shows the three kinds of combo boxes. Each combo box contains the names of states that you saw earlier in list boxes. The first combo box, the dropdown combo box, is normally closed; when the user clicks the combo box's down arrow, the combo box opens. The third combo box, the dropdown list box, is left unopened. If the user opens the dropdown list box, the user will see a list of state names but will not be able to add to the state names because no data entry is possible in dropdown list boxes.<BR><P><B> <A HREF="11vel07.gif">Figure 11.7. The three combo box styles.</A></B><BR><P>Table 11.4 contains a description of every combo list property value that you can set in the Property window.<BR><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 11.4. The combo box 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>BackColor</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The background color of the combo box. This is a hexadecimal number representing one of thousands of possible Windows color values. You can select from a palette of colors displayed by Visual Basic when you're ready to set the BackColor property. The default background color is the same as the form's default background color.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>DragIcon</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The icon that appears when the user drags the combo box control around on the form. (You will only rarely allow the user to move a combo box control, 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>Either contains 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 combo box control 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 combo box control can respond to events. Otherwise, Visual Basic halts event processing for that particular control.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontBold</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>True (the default) if the combo values are to display in boldfaced characters; False otherwise.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontItalic</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>True (the default) if the combo values are to display in italicized characters; False otherwise.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontName</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The name of the combo box's text style. Typically, you'll use the name of a Windows TrueType font.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontSize</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The size, in points, of the font used for the combo box values.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontStrikethru</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>True (the default) if the combo values are to display in strikethru letters (characters with a dash through each one); False otherwise.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>FontUnderline</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>True (the default) if the combo box values are to display in underlined letters; False otherwise.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>ForeColor</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The color of the values inside the combo box.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Height</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The height, in twips, of the combo box control.</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 combo box control is part of a control array, the Index property provides the numeric subscript for each particular combo box control. (See the next unit).</FONT><TR><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 combo box control.</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 combo box control. 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 Combo1, Combo2, and so on as you add subsequent combo box controls to the form.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Sorted</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>If True, Visual Basic doesn't display the combo box values sorted numerically or alphabetical. If False (the default), the values appear in the same order in which the program added them to the list.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>Style</FONT><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>The default, 0-Dropdown Combo, produces a dropdown combo box control. 1-Simple Combo turns the combo box into a simple combo box control. 2-Dropdown list turns the combo box into a dropdown list box control.</FONT><TR><TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>TabIndex</FONT>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?