C # ListBox usage

Source: Internet
Author: User

1. attribute list:

Select Type of entries in the selectionmode component, that is, multiple and single)
Total number of rows displayed in the rows list box
Selected
The returned type of selecteditem is listitem.
Total number of entries in the count list box
Index value of the selected item in the selectedindex list box
Items refers to all items in the list box. each item is of the listitem type.

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 ");

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 );
}

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 multiple

7. 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 ");
}

8. 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;

9. 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;

 

This. listbox1.items. insertat (3, new listitem ("inserted after row 3rd ",""));

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

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

 

Related Article

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.