Winform Control Learning Notes [day 3] -- ListBox

Source: Internet
Author: User

1. Attribute event 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 check whether the entry is 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.

SelectedIndexChanged: triggers an event when the selected item changes

 

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 selected items:

// First, judge whether the Items in the list box are greater than 0If (ListBox. Items. Count> 0) {// Remove the selected item ListBox. Items. Remove (ListBox. SelectedItem );}

 

5. Clear all items:

// First, judge whether the Items in the list box are greater than 0If (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. Shift the items in the list box

That is, shift up and down

The specific idea is to create a ListBox object and put the items to be moved in this object for the moment.

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.

The Code is as follows:

// 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;

 

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

 

9. insert mode

This. listBox1.Items. insertat (3, new ListItem ("inserted after 3rd rows", ""); 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.