Operation and Implementation of The ListBox control
Source: Internet
Author: User
. NET Framework> test> class library> system. Windows. Forms> ListBox class attributes
1. attribute list:Select the type of items in the selectionmode component: None-do not agree with any choice at all; One-default value, only select a single option; multisimple-simple multiple choices, click one mouse to select or deselect one of the items in the list. multiextended-multiple options are similar to those in windows. selecteditem is in a single-choice list box. selecteditem returns an object whose text is represented by the text attribute. the function is to obtain the selected entries in the list box. if the control agrees to multiple selections, the selected entries are represented by the selecteditems attribute, which is a collection of item objects.
Total number of entries in the count list box
Selectedindex/selectedindices/selecteditem/selecteditemslistbox. the selectedindex attribute is used to obtain the position of the currently selected item in The ListBox. the selectedindices attribute gets a set that includes the index from scratch for all currently selected items in The ListBox; ListBox. the selecteditem attribute gets the currently selected items in ListBox; ListBox. the selecteditems attribute is used to obtain all selected items in the Multi-choice ListBox. It is a set. Public readonly property selectedindices as ListBox. selectedindexcollection ListBox. selectedindexcollection, including the index of the currently selected item in the control. If no selected item exists, null is returned.
ListBox. selectedindexcollectionAll items in the list box
2. Select the selected value from the list box.ListBox. selectedvalue
3. dynamically add items in the list box:ListBox. items. add ("items to be added"); the following code adds content to the list box through the input box: private sub bttnadd1_click (byval sender as system. object, byval e as system. eventargs) handles bttnadd1.click
Dim listitem as string
Listitem = inputbox ("Enter new item's name ")
If listitem. Trim <> "then
Sourcelist. Items. Add (listitem)
End if
End sub ListBox. Items. insert (index, item) item is the object to be added to the list, and index is the index of this new item.
4. Remove a specified item:// First, infer whether the items in the list box are greater than 0.
If (ListBox. Items. Count> 0)
{
// Remove the selected item
ListBox. Items. Remove (ListBox. selecteditem );
} The following code deletes the selected entry from the list box selected by a single item: private sub bttnremoveseldest_click (byval sender as system. Object, byval e as system. eventargs) handles bttnremoveseldest. Click
ListBox. Items. Remove (ListBox. selecteditem)
The code below end sub deletes multiple entries from the Multi-choice list box: private sub bttnremoveselsrc_click (byval sender as system. Object, byval e as system. eventargs) handles bttnremoveselsrc. Click
Dim I as integer
For I = 0 to ListBox. selectedindices. Count-1
Lisboxt. Items. removeat (ListBox. selectedindices (0 ))
Next
End sub remarks: Use the ListBox. Items. Remove Method to delete entries from the list by using the number of objects to be deleted as the limit. The removeat method can be used to delete the list items of the specified position (INDEX). It uses the index as the number of objects: ListBox. Items. removeat (INDEX)
5. Clear all items:// First, infer whether the items in the list box are greater than 0.
If (ListBox. Items. Count> 0)
{
// Clear all items
ListBox. Items. Clear ();
}
6. You can select multiple items in the list box at a time: You only need to set the attribute selectionmode = "multiple" of the list box, and press Ctrl to select 7 more
. SEARCH strings in multiple lists:The findstring and findstringexact methods can quickly locate the position (wordindex) of the search word in the list ). They all receive strings as the number of shards for the younger brother, and the second number of shards is optional, used to specify the start position of the search. The findstring finds the entries that match the specified character, while the findstringexact completely matches. Wordindex = ListBox. findstringexact ("search word") wordindex = ListBox. findstring ("search word ")
8. Contains MethodYou can avoid inserting the same object in the list. This method receives an object as the number of bytes, and returns true/false to indicate whether the object is included in the items set. For example, you need to implement the following function: first check whether the inserted string already exists in the list and insert it only when the string is not included in the list. The Code is as follows (VB. NET): dim ITM as string = "Remote Computing" if not ListBox. Items. Contains (ITM) Then listbox1.item. Add (ITM) end if
9. Two lists are linked, that is, two levels of linked menus.// Deduce the selected value in the first list box
Switch (listbox1.selectvalue)
{
// If it is "A", add these to the second list box:
Case ""
Listbox2.items. Clear ();
Listbox2.items. Add ("A1 ");
Listbox2.items. Add ("A2 ");
Listbox2.items. Add ("A3 ");
// If it is "B", add the following to the second list box:
Case "B"
Listbox2.items. Clear ();
Listbox2.items. Add ("B1 ");
Listbox2.items. Add ("B2 ");
Listbox2.items. Add ("B3 ");
}
10. Shift the items in the list box That is, shift up and down
The detailed idea is: Create a ListBox object and put the items to be moved in this object for the moment.
If it is an upward shift, it is to assign the value of the previous item of the selected item to the selected item, and then
Attaches the value of the newly added object to the first item of the selected item.
Detailed code:
// Define a variable for shift
Index =-1;
// Save the text and values of the current entry to a temporary variable.
Listitem lT = new listitem (ListBox. selecteditem. Text, ListBox. selectedvalue );
// The value of the selected item is equal to the value of the previous or next item.
ListBox. items [ListBox. selectedindex]. Text = ListBox. items [ListBox. selectedindex + Index]. text;
// The value of the selected item is equal to the value of the previous or next item.
ListBox. items [ListBox. selectedindex]. value = ListBox. items [ListBox. selectedindex + Index]. value;
// Replace the previous or next value of the selected item with a temporary variable
ListBox. items [ListBox. selectedindex]. test = lt. test;
// Replace the previous or next value of the selected item with a temporary variable
ListBox. items [ListBox. selectedindex]. value = lt. value;
// Place the mouse pointer on the moving item
ListBox. items [ListBox. selectedindex]. value = lt. value;
11. Move the pointer to the specified position:(1) Move to the first entry
// Set the index of the selected item to 0.
ListBox. selectindex = 0;
(2) Move to the end
// Set the index of the selected item to ListBox. Items. Count-1.
ListBox. selectindex = ListBox. Items. Count-1;
(3). Previous
// Subtract 1 from the selected index
ListBox. selectindex = ListBox. selectindex-1;
(4). Next
// Add 1 with the selected index
ListBox. selectindex = ListBox. selectindex + 1;
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.