VB.net the ListBox control

Source: Internet
Author: User
Tags contains count integer sort

A ListBox control can display a list of a set of items from which a user can select one or more options. A list box can provide the user with a list of all options. Although you can also set the list box to be a multiple-column list, the list box displays all of the options vertically, and the scroll bar automatically appears on the control if the number of items exceeds the number that the list box can display. The user can then scroll up, down, left, and right in the list. The icon for the ListBox control in the Toolbox is as shown in the figure:

1.click and double-click Events
For events in a list box, especially when a list box participates in a part of a function that triggers an application, you can add a button control and use it with a list box. The Click event procedure for a button should use the options for the list box to perform an application-appropriate operation.
Double-click an item in the list with the selected item and then clicking the button should have the same effect. To do this, you should call the button's click procedure during the double-click procedure for the ListBox control:

Public Sub Listbox1_doubleclick (ByVal sender as Object,_
                   ByVal e as System.EventArgs) Handles listbox1.doubleclick< C3/>button1_click (sender,e) End
Sub

This provides shortcuts to users who use the mouse, and does not prevent users who use the keyboard from performing the same action.
Note: There is no keyboard command equivalent to the DoubleClick event, so the DoubleClick function of the ListBox control cannot be reached through a shortcut key.
2. Add an item to a list box
To add an item to a list box, use the InsertItem method, which has the following syntax:
Listboxname.insertitem (Index as Integer,item as Object)
or
ListboxName.Items.Insert (index as integer,item as Object)
Name of listboxname list box Item to a string expression that is added to the list. If "item" is a literal constant, enclose it in parentheses: "index" specifies where to insert the new item in the list. "Index" for 0 represents the first position. If you add an item to the first position in the list box, you can also use:
ListboxName.Items.Add (item as Object)
You typically add a list item to a form design or new procedure or InitializeComponent (the procedure exists in Windows Form Designer generated code), but you can also add a project at any time by using the InsertItem method. The project can then be added dynamically (in response to the user's action). The following code adds "Germany", "India", "France", and "USA" to a list box named ListBox1:

Public Sub New ()
   mybase.new Form1=me ' This call is required by the
   Win Form Designer
   intializecomponent< C5/>listbox1.insertitem (0, "Germany")
   Listbox1.insertitem (1, "India")
   Listbox1.insertitem (2, "France")
   Listbox1.insertitem (3, "USA")
   ' Todo:add any initialization after the InitializeComponent () called End
Sub

Note: The code in "InitializeComponent" is automatically generated every time the form is designed, so try to design the form as often as possible, because if you repeatedly design the form, the previously added code will be overwritten, so you can create a procedure for adding code to the listbox. AddItem () ' procedure ', which calls this procedure in ' New () ' or ' InitializeComponent () ', as follows:

Public Sub New ()
   mybase.new Form1=me ' This call is required by the
   Win Form Designer
   IntializeComponent call
   AddItem (0
   ' Todo:add, any initialization after the InitializeComponent ()
End Sub
Private Sub AddItem ()
   listbox1.insertitem (0, "Germany")
   Listbox1.insertitem (1, "India")
   Listbox1.insertitem (2, "France")
   Listbox1.insertitem (3, "USA") End
Sub

3. Add Project at design time
You can also add items to the list at design time by setting the Items property of the ListBox Control Properties window, when the Items property option is selected and clicked, the String Collection Editor pops up to enter the list item and press ENTER to wrap the line. Items can only be added at the end of the list. So, if you want to sort the list alphabetically, you should set the sorted property to True.

4. Sorting list
You can specify items to be added to the list alphabetically, setting the sorted property to True and omitting the index. The sort is case-insensitive, so the word "Japan" and "Japan" will be treated equally. When the sorted property is set to True, the InsertItem method may cause unpredictable and unordered results.

5. Remove items from the list
The following syntax is available for removing items from the list:
ListboxName.Items.Remove (Index as Integer)
"Remove" has one parameter, "index", which specifies the item to be deleted; it has an overloaded function:
ListboxName.Items.Remove (value as Object)
To meet the needs of different input parameters. To delete all items in a list box, use the Clear method:
Listbox1.Items.Clear ().
6. Get the list content through the Text property
Generally, the easiest way to get the currently selected item value is to use the Text property. The Text property always corresponds to the list item that the user selected at run time. For example, the following code displays information about the Canadian population when the user selects Canada from the list box:

Public Sub Listbox1_doubleclick (ByVal sender as Object,byval e as System.EventArgs)
    If listbox1.text= "Canada" then< C6/>textbox1.text= "Canada has million people."
    End If End
Sub

The Text property contains the item currently selected in the Listbox1 list box. The code checks whether "Canada" is selected, and displays the information in the text box if selected.
7. Access list items with the Items property
Use the Items property to access all items in the list. This property contains an array, and each item in the list is an element of the array. Each item is represented as a string. You should use the following syntax when referencing a list of items:
Listboxname.items (Index)
The "listboxname" argument is the name of the list box, and "Index" is the location of the item. The index for the top item is 0, and the next item is indexed by 1, and so on. For example, the following statement displays the third item (index=2) of a list in a text box:
Textbox1.text=ctr (Listbox1.items (2)).
8. Use the SelectedIndex attribute to judge the position
If you want to know the location of the selected item in the list, use the SelectedIndex property. This property is available only at run time, and it sets or returns the index of the currently selected item in the control. Setting the SelectedIndex property of a list box also triggers the control's Click event.
If the first (Xiangduan) item is selected, the value of the property is 0, and if the next item is selected, the value of the property is 1, and so on. If no item is selected, the SelectedIndex value is-1.
9. Use the Count property to return the number of items
To return the number of items in the list box, use the Count property. For example, the following statement uses the Count property to determine the number of items in a list box:
Textbox1.text= "You have" &Listbox1.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.