ComboBox Control for VB.net

Source: Internet
Author: User
Tags constant count insert

The Combobox (combo box) control is equivalent to combining the functions of a text box and a list box. This control can implement the input text to select items, or you can implement the options for selecting items from the list. If the number of items exceeds the number of items that the combo box can display, scroll bars appear automatically on the control. Users can scroll the list up or down. The icon for the ComboBox control in the Toolbox is as shown in the figure:

1. Using combo boxes and list boxes
Typically, a combo box applies to a list of suggested options, and when you want to limit your input to a list, you use a list box. The combo box contains the editing area, so you can enter an option that is not in the list into the column area. In addition, the combo box saves space on the form. You can easily fit a combo box where you cannot fit a list box unless you click the combo box's down arrow (except for a combo box with a style property value of 1, which is always in the Drop-down state) to display the full list.
2. Style of combo box
There are three combo box styles here. Each style can be set at design time or runtime, and each style uses a numeric or corresponding Visual Basic constant to set the style of the combo box.
Style value constants: The Drop-down combo box value is the constant value in the 0,vb.net is dropdown; The simple combo box value is simply a constant value in 1,vb.net, and a drop-down list box value of 2,vb.net is a constant value of DropDownList.
3. Drop-down combo box
Under the default setting (Style=0), the combo box is Drop-down. You can enter text directly as you would in a text box, or you can click the accompanying arrow to the right of the combo box to open the list of options. When you select an option, insert this option into the text section at the top of the combo box. You can also press the Alt+↓ key to open the list when the control has the focus
4. Simple combo box
Setting the Style property of the combo box to 1 specifies a simple combo box that displays the list at any time. To display all items in the list, you must draw the list box large enough. A vertical scroll bar is automatically inserted when the number of entries exceeds the limit that can be displayed. Users can enter text directly or select from a list. Like a drop-down combo box, a simple combo box also allows users to enter options that are not in the list.
5. Drop-down combo box
The Drop-down combo box (style=2) is similar to a regular list box-it displays a list of items that the user must select from, but the Drop-down list box differs from the list box in that the list is not displayed unless you click the arrow to the right of the box.
The main difference between this list box and the Drop-down combo box is that the user cannot enter an option in the list box, but only select it in the list. You can use this type of list box when there is less space on the form.
6. Add Items
To add an item to a combo box, you should use the Insert method with the following syntax:
ComboboxName.Items.Insert (index as integer,item as Object)
"Comboboxname" is a list box or combo box name, item is a string expression added in the list, enclosed in quotation marks. Index is used to specify where the new item is inserted in the list. Index 0 represents the first position. When you are in the first position, you can also use syntax:
ComboBox. Items.Add (item as Object)
You typically add list items at design time or in new procedures, but you can also use the Insert method at all times. This allows you to add items to the list dynamically. The following code places "Chardonnay", "Fum Blanc", "Gewztraminer", and "Zinfandel" in a combo box named Combobox1,style Property 0 (DropDown):

Public Sub New () ...
   ...
   Combobox1.Items.Insert "Chardonnay"
   Combobox1.Items.Insert "Fum Blanc"
   Combobox1.Items.Insert " Gewztraminer "
   Combobox1.Items.Insert" Zinfandel "End
Sub

7. Add Project at design time
When you design, you can also set the Items property of the Properties window for the combo box control to add items to the list. Select the Items property and click the button to enter the list item, and then press ENTER to change to a new line. The
can only add items to the end of the list. So, if you want to sort the list alphabetically, you should set the sorted property to True.
8. Add item at specified location
To add an item at the specified location in the list, you specify the index value after the new item. For example, the downlink code inserts "Piont Noir" into the first position and adjusts the position of the other items downward:
Combobox1.Items.Insert (0, Pinot Noir)
Note: The first position in the specified list is 0 instead of 1.
9. The sorted list
Sets the sorted property to True and omits the index, so you can specify items to add in the list in alphabetical order. Sorting is case-insensitive. When the sorted property is set to True, the Items.insert method causes unpredictable unordered results.
10. Delete Items
You can delete items in a combo box by using the Items.remove method. Items.remove has a parameter index that specifies the item to delete: ComboBox1. The Items.remove (index) and index parameters are the same as those in Items.insert. For example, to delete the first item 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. Get list content with the Text property
The easiest and most common way to get the current selection value is to use the Text property. At run time, the Text property corresponds to this literal regardless of what text is entered into the text box portion of the control. It can be a selected list option, or a string that the user enters in a text box. For example, if the user selects "Chardonnay" in the list box, the following code displays information about "Chardonnay":

 Private Sub Combobox1_click (Byval sender as Object,byval e as System.EventArgs) If combobox1.text= "Chardonnay" Th
   En textbox1.text= "Chardonnay is a midium-bodied white wine." End If End Sub 

Where the Text property contains the item currently selected in the Combobox1 list box. The code looks at whether "Chardonnay" is selected, and if so, displays the information in the text box.
12. Access list options with the Items property
With the Items property, you can access all items in the list, which contains an array, and each item in the list is an element of the array. Each item is represented as a string. To refer to items in the list, use the following syntax:
Comboboxname.items (Index)
Comboboxname is the name of the combo box, and index is the location of the item. The index of the top item is 0, the index of the next item is 1, and so on. For example, in a text box, the following statement displays the third item in the list (index=2):
Text1.text=cstr (Combobox1.items (2))
13. Use the Selectindex attribute to judge the position
To know the location of the selected item in the combo box list, you can get it by the Selectindex property. This property sets or returns the index value of the currently selected item in the control and is valid only at run time. Setting the Selectindex property of a combo box also triggers the control's Click event. If the first (Xiangduan) item is selected, the property value is 0; the next Item property value selected is 1, and so on. If the item is not selected, or if the user enters an option (style 0 or 1) in the combo box and does not select an existing item in the list, then Selectindex is-1.
14.items.count property returns the number of items
To return the number of items in the combo box, use the Items.Count property. For example, the following statement uses the Items.Count property to determine the number of items in a 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.