ComboBox of VB. NET Control

Source: Internet
Author: User

The ComboBox control combines the functions of the text box and the list box. This control can be used to select projects by text input or by selecting projects from the list. If the number of items exceeds the number of items that can be displayed in the combo box, the control automatically displays a scroll bar. You can scroll up or down the list. The ComboBox control icon in the toolbox:

1. Use a combo box and a list box
In general, the combo box applies to the Option List of the created items. When you want to restrict the input to the list, you should use the list box. The combo box contains the editing area. Therefore, it is not included in the option input column area in the list. In addition, the combo box saves the form space. Only when you click the downward arrow of the combo box (except for the one with the style attribute value), it is always in the drop-down status) to display all the lists. Therefore, it is easy to hold the list box.
2. combox Style
There are three combox styles. Each style can be set at design or runtime, and each style uses a value or a corresponding Visual Basic constant to set the style of the combo box.
Style value constant: the drop-down combo box value is 0, the constant value in VB.net is dropdown, the simple combo box value is 1, the constant value in VB.net is simple, and the drop-down list box value is 2, the constant value in VB.net is dropdownlist.
3. drop-down box
In the default setting (style = 0), the combo box is drop-down. You can directly enter the text as in the text box, or click the accompanying arrow on the right of the combo box to open the option list. Select an option and insert it into the text section at the top of the combo box. When the control obtains focus, you can press Alt + keys to open the list.
4. Simple combo box
Set the style attribute of the combo box to 1. A simple combo box is specified and displayed in the list at any time. To display all items in the list, the list box must be large enough. When the number of items exceeds the display limit, a vertical scroll bar is automatically inserted. You can enter text directly or select from the list. Like a drop-down box, a simple box allows users to enter options that are not in the list.
5. drop-down box
The drop-down box (style = 2) is similar to the regular list box-it displays the list of items, which must be selected by the user. However, the drop-down list box differs from the list box unless you click the arrow on the right of the box, otherwise, the list is not displayed.
The main difference between a list box and a drop-down box is that you cannot enter options in the list box, but can only select options in the list. This type of list box can be used when there is less space on the form.
6. Add a project
To add a project to the combo box, the insert method should be used. Its syntax is as follows:
Comboboxname. Items. insert (index as integer, item as object)
"Comboboxname" is the name of the list box or combo box, and item is the string expression added to the List, enclosed in quotation marks. Index is used to specify the insert position of a new project in the list. The value of index 0 indicates the first position. When at the first position, you can also use the Syntax:
ComboBox. Items. Add (item as object)
The list project is usually added at design or during the new process, but the insert method can also be used at any time. In this way, you can dynamically add projects to the list. The following code places "Chardonnay", "Fum blanc", "gewztraminer", and "Zinfandel" in the combobox1 box with the style attribute 0 (dropdown:

Public sub new ()... combobox1.items. Insert "Chardonnay" combobox1.items. Insert "Fum BLC" combobox1.items. Insert "begin" combobox1.items. Insert "Zinfandel" End Sub"

7. Add a project during design
During design, you can also set the items attribute of the "properties" Window of the combox control to add a project to the list. After you select the items attribute and click the button, you can enter the list item and press enter to change to a new line.
Only projects can be added to the end of the list. Therefore, if you want to sort the list alphabetically, set the sorted attribute to true.
8. Add a project at a specified location
To add a project to a specified position in the list, specify the index value after the new project. For example, the downstream code inserts "piont noir" into the first position and adjusts the position of other projects downward:
Combobox1.items. insert (0, "pinot noir ")
Note: The first position in the list is 0 instead of 1.
9. Sorting list
If the sorted attribute is set to true and the index is omitted, you can specify projects that are added alphabetically in the list. Sorting is case insensitive. When the sorted attribute is set to true, the items. insert method will lead to unexpected unordered results.
10. delete a project
You can use the items. Remove Method in the combo box to delete a project. Items. Remove has an index parameter that specifies the project to be deleted: combobox1.items. Remove (INDEX) and the index parameters are the same as those in items. insert. For example, to delete the first project in the list, add the following line of code:
Combobox1.items. Remove (0)
To delete all list items, use the clear method:
Combobox1.clear
11. Use the text attribute to retrieve the list content
The simplest common method to obtain the selected project value is to use the text attribute. At runtime, No matter what text is entered into the text box of the control, the text attribute corresponds to this text. It can be a selected list option or a string that you enter in the text box. For example, if you select "Chardonnay" in the list box, the following code displays information about "Chardonnay:

Private sub combobox#click (byval sender as object, byval e as system. eventargs) If combobox1.text = "Chardonnay" then textbox1.text = "Chardonnay is a Midium-bodied white wine." End if end sub

The text attribute contains the currently selected items in the combox1 list box. Code to check whether "Chardonnay" is selected. If so, the information is displayed in the text box.
12. access list options using the items attribute
With the items attribute, you can access all items in the list. This attribute contains an array, and each item in the list is an array element. Each item is represented as a string. To reference projects in the list, use the following syntax:
Comboboxname. Items (INDEX)
Comboboxname is the name of the combo box, and index is the position of the project. The index of the top project is 0, the index of the next project is 1, and so on. For example, in the text box, the following statement shows the third project (Index = 2) in the list ):
Text1.text = CSTR (combobox1.items (2 ))
13. Use the selectindex attribute to determine the position
To know the position of the selected project in the combo box list, you can get it from the selectindex attribute. This property sets or returns the index value of the currently selected project in the control, and is only valid at runtime. When you set the selectindex attribute of the combo box, the Click Event of the control is also triggered. If the first (item end) project is selected, the property value is 0. The property value of the next project is 1, and so on. If no project is selected, Or you enter an option (Style 0 or 1) in the combo box and do not select an existing project in the list, selectindex is-1.
14. Number of items returned by the items. Count attribute
To return the number of items in the combo box, use the items. Count attribute. For example, the following statement uses the items. Count attribute to determine the number of items in the combo box:
Textbox1.text = "you have" & combobox1. "items. Count" & "_ entries listed"

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.