C # ListBox controls

Source: Internet
Author: User

Use of The ListBox control:

1) control properties

Items

SelectedItems

SelectioModes

2) Data Binding

DataSoure

DisplayMember

ValueMenber

3) instance

The following describes the use of The ListBox control.

First, let's talk about the properties of the control. (1) Items: use this property to get the properties of the List Control item. This attribute can be used to determine the selected items in the list control. When you add items, you can either add it statically at design time or dynamically in code. If you do not want to display the items added during design, you can add this. listBox1.Items. Clear () in the Code. Only the options added in the Code are displayed.

(2) SelectedItems: obtains the set of currently selected items in The ListBox.

(3) SelectioModes: gets or sets the method used to select an item in ListBox. A total of four values are optional. The default value is SelectionMode. one, only One can be selected. If the attribute value is none, it cannot be selected. When the attribute is MultiExtended, by pressing the Shift key and clicking the mouse or one of the Shift key and one of the arrow keys (Up, down, left, and right), the selected content is extended from the previous selected item to the current item. Press Ctrl and click the mouse to select or unselect an item in the list. When this attribute is set to MultiSimple, click or press the Space key to select or unselect an item in the list.

Next we will talk about data binding. Generally, data is variable, so we need to bind data. There are several types of data binding: one is to bind the able or DataSet obtained from the database; the other is to customize a class and bind the data in the Custom class. Data Binding in c # differs from Data Binding in ASP. NET. After binding data in ASP. NET, A DataBind method must be called, which is not required in c. DisplayMember obtains or sets the attribute to be displayed.

Finally, let's make an example: Below we just talk about the implementation of several important methods, and the specific code will not be written here. Place the options in the left border to the right box. Code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxzcGFuIGNsYXNzPQ = "keyword"> for (int I = this. listBox1.SelectedItems. Count-1; I> = 0; I --)
{
Menu menu = (menu) this. listBox1.SelectedItems [I];
This. listBox2.Items. Add (menu );

This. listBox1.Items. Remove (I );

}

Although the above functions are implemented, there is a problem, that is, the items on the left go to the right and turn into reverse order. Here we need to separate add and remove write, code:

For (int I = 0; I <this. listBox1.SelectedItems. Count; I ++)
{
Menu menu = (menu) this. listBox1.SelectedItems [I];
This. listBox2.Items. Add (menu );

}
For (int I = this. listBox1.SelectedItems. Count-1; I> = 0; I --)
{
This. listBox1.Items. Remove (I );
}

Up to now, the problem is that when the first few items in the selection box are selected, there is no problem with right shift. However, when the last few items are selected, there are the following items in the right border, the first few items are removed from the box on the left. The reason for this problem is that we

SelectedItems and Items are confused. This is an easy mistake for beginners. Code:

For (int I = 0; I <this. listBox1.SelectedItems. Count; I ++)
{
Menu menu = (menu) this. listBox1.SelectedItems [I];
This. listBox2.Items. Add (menu );

}
For (int I = this. listBox1.SelectedItems. Count-1; I> = 0; I --)
{

Menu menu = (menu) this. listBox1.SelectedItems [I];
This. listBox1.Items. Remove (menu );
}

That's right.

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.