ListBox control operations

Source: Internet
Author: User

1.Attribute list:

Selectionmode Select multiple types of items in the component. (Multiple) , Single choice (Single)
Rows Total number of rows displayed in the list box
Selected Check whether entries are selected
Selecteditem The returned type is Listitem To obtain the selected entries in the list box.
Count Total number of entries in the list box
Selectedindex Index value of the selected item in the list box
Items It refers to all items in the list box. each item is of the type Listitem

2.Select the selected value from the list box.

ListBox. selectedvalue

3.Dynamically add items in the list box:

ListBox. Items. Add ("Item to be added");

4.Remove a specified item:

//First, determine whether the items in the list box are greater0
If (ListBox. Items. Count> 0)
{
//Remove selected items
ListBox. Items. Remove (ListBox. selecteditem );
}

5.Clear all items:

// first, determine whether the items in the list box are greater than 0
If (ListBox. items. count> 0)
{< br> // 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 properties of the list box.Selectionmode = "multiple ",PressCTRLYou can select multiple

7.Two list boxes are linked, that is, two levels of linked menus

// Judge the selected value in the first list box
Switch (listbox1.selectvalue)
{
// If yes "" , Add these in the second list box:
Case ""
Listbox2.items. Clear ();
Listbox2.items. Add ("A1 ");
Listbox2.items. Add ("A2 ");
Listbox2.items. Add ("A3 ");
// If yes "B" , Add these in the second list box:
Case "B"
Listbox2.items. Clear ();
Listbox2.items. Add ("B1 ");
Listbox2.items. Add ("B2 ");
Listbox2.items. Add ("B3 ");
}

8. Shift items in the list box
That is, shift up and down
The specific idea is: Create ListBox Object.
If it is an upward shift, the previous value of the selected item is assigned to the selected item, and then
Attaches the value of the newly added object to the first item of the selected item.
DetailsCodeIs:
// 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 value of the previous or next one of the selected items with a temporary variable.
ListBox. items [ListBox. selectedindex]. test = lt. test;
// Replace the value of the previous or next one of the selected items 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;

9.Move the pointer to the specified position:

(1 ). Move to first
// Set the index of the selected item 0 Just OK Now
ListBox. selectindex = 0;
(2 ). Move to end
// Set the index of the selected item ListBox. Items. Count-1 Just OK Now
ListBox. selectindex = ListBox. Items. Count-1;
(3 ). Previous
// Subtract from the selected index 1
ListBox. selectindex = ListBox. selectindex-1;
(4 ). Next
// Use the selected index to add 1
ListBox. selectindex = ListBox. selectindex + 1;

This. listbox1.items. insertat (3, new listitem ("Insert3Item after line",""));

This. listbox1.items. insertat (index, listitem)

Listbox1.items. insert (0, new listitem ("text", "value "));

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.