tutorial on the use of ComboBox controls _vbs

Source: Internet
Author: User

We learned about the use of ListBox controls, as well as a control--combobox control, also known as a combo box, that is very similar to a ListBox control in vb.net. A combo box control consists of two parts, a text box that can enter a list item in the upper part, and a list box at the bottom of the text box to display a list of items that the user can select from

The ComboBox control and the ListBox control are functionally similar, and in many cases the two controls are interchangeable, but there is a situation where only one control is appropriate in a particular environment.

Typically, the ComboBox control is appropriate for recommending that the user select the options enumerated by the control, while allowing the user to enter an option in the text box that does not exist in the list, and the ListBox control is appropriate to restrict the user from selecting only the options in the list.

In the user interface, because the ComboBox control is by default a drop-down list box, less form space is used than the ListBox control, which is more appropriate for use in cases where there are a large number of list items. As shown in figure three below is the QQ to modify the personal settings of the interface, it is a lot of reasonable use of ComboBox control, so that the user interface is simple and can accommodate more options information.

 This shows that the ComboBox control is more flexible and versatile than the ListBox control.

  Common properties for a ComboBox control:

1. BackColor property: Gets or sets the background color of the ComboBox control.

2. DropDownStyle property: Gets or sets the value of the specified combo box style to determine whether the user can enter a new value in the text section and the list part is always displayed. It contains three values, the default value is DropDown, as shown in the following table:

Description of Member name

The DropDown text section can be edited. The user must click the arrow button to display the list section.

DropDownList users cannot edit the text section directly. The user must click the arrow button to display the list section.

The simple text section can be edited. The list section is always visible.

3. DropDownWidth property: Used to get or set the width of the drop-down portion of the combo box (in pixels), some list items are too long, you need to change the property to display all the text of the class table entry, and if you do not set the DropDownWidth value, this property returns the width of the combo box. Note that the Drop-down section width cannot be less than the width of the ComboBox, so we set the value of the dropdownwidth if it is less than the width of the ComboBox, the width of the Drop-down list box is the same as the width of the text box.

4. Droppeddown property: Gets or sets a value that indicates whether the combo box is displaying its drop-down section. True if the Drop-down section is displayed, otherwise false. The default value is False.

5. IntegralHeight Property: Specifies whether the height of the edit box or list box control is automatically adjusted so that the last item in the control can be displayed correctly. Specifies whether the height of the text box control is automatically resized to display a single line of text. Available at design time, read-only at run time. The default is False. If the height of the list box control is not appropriate, the last line of text in the control will display only a portion of it, set Intergralheight to True, and the height of the control can be automatically adjusted so that the last item in the control is displayed correctly. Note: When the IntegralHeight property is set to True, the value of the Height property may not match the true height of the control.

6. Items property: Gets an object that represents the collection of items contained in the ComboBox. Detailed usage is described below.

7. MaxDropDownItems property: The maximum number of items that can be displayed in the Drop-down section. The minimum value for this property is 1, and the maximum value is 100.

8. Text property: The text displayed in the Text entry box in the ComboBox control.

9, SelectedIndex Properties and SelectedItem properties: The SelectedIndex property returns an integer value representing the index of the currently selected list item, which can be programmatically changed, and the corresponding item in the list appears in the text box of the combo box. If no item is selected, the SelectedIndex is-1, and if an item is selected, SelectedIndex is an integer value starting at 0. The SelectedItem property is similar to the SelectedIndex property, but the SelectedItem property returns an item.

10. SelectedText property: A String representing the currently selected text in a combo box. If DropDownStyle is set to ComboBoxStyle.DropDownList, the return value is an empty string (""). You can assign text to this property to change the text currently selected in the combo box. If no text is currently selected in the combo box, this property returns a 0-length string. The following code, we can verify the property in the mouse event of the ComboBox control:

Private Sub Combobox1_mouseup (ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) Handles combobox1.m Ouseup
MessageBox.Show (combobox1.selectedtext) End
Sub

The returned value is the text that we selected with the mouse.

The corresponding SelectionLength property returns the number of characters for the text, and the SelectionStart property returns the starting position of the selected text in the combo box.

Second, the important method of ComboBox control:

1, BeginUpdate method and EndUpdate method: When you add an item one at a time using the Add method, you can use the BeginUpdate method to prevent the control from redrawing ComboBox each time you add an item to the list. After completing the task to add items to the list, call the EndUpdate method to enable ComboBox for redrawing. When you add a large number of items to a list, adding items using this method prevents flicker when you draw ComboBox. As shown in the following code:

Private Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles mybase.load
Dim i as inte GER = 0
combobox1.beginupdate () for
i = 0 to 999
ComboBox1.Items.Add ("Item" &combobox1.items.count. ToString)
Next
combobox1.endupdate () End
Sub

2, Add method: Items Properties of one of the methods, generally used to:

COMBOBOX1.ITEMS.ADD ("Item 0")

You can also use the clear method of the Items property to purge all list items.

3, FindString method and FindStringExact method:

The FindString method is used to find the first item in the ComboBox that starts with the specified string, which is a fuzzy query, but the found string must be at the beginning of the match.

The FindStringExact method is used to find an item that exactly matches the specified string.

4, GetItemText Method: Returns the text representation of the specified item. The use form is as follows:

GetItemText (item)

  Third, the example:

Here we understand the use of the ComboBox control in code, add a ComboBox control to the form, four button controls, a TextBox control, a GroupBox control, and the layout is shown in Figure four below:

1, in the "Add 1000" button click event to add the following code:

Dim i as Integer = 0
combobox1.beginupdate () for
i = 0 to 999
ComboBox1.Items.Add ("Item" & (ComboBox 1.items.count + 1). ToString)
Next
combobox1.endupdate ()

2. Add the following code to the "Add One Item" button click event:

COMBOBOX1.ITEMS.ADD (TextBox1.Text)

The text that is entered in the text box as the item.

3, add the following code in the Click event of the Find Blur match button:

Combobox1.selectedindex = combobox1.findstring (TextBox1.Text)

The first item that starts with the text entered in the text box is returned as the selected item for the ComboBox control.

4. Add the following code to the click event of the Find exact match button:

Combobox1.selectedindex = Combobox1.findstringexact (TextBox1.Text)

The item that exactly matches the text entered in the text box is returned as the selected item for the ComboBox control.

5, add the following code in the ComboBox1 SelectedIndexChanged event:

Private Sub combobox1_selectedindexchanged (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles ComboBox1. SelectedIndexChanged
MessageBox.Show (Combobox1.getitemtext (Combobox1.selecteditem)) End
Sub

Returns the text of the currently selected item through the GetItemText method when the current selection of the ComboBox1 changes.

The above is a small set to introduce the ComboBox control of the use of the tutorial, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.