ListBox and ListBox

Source: Internet
Author: User

ListBox and ListBox
The Winfrom ListBox data source binding list interface is not updated. The problem is that the bound data source cannot be CRUD.


Scenario: Obtain the selected items of a listbox and add them to another listbox solution-1: do not directly bind DataSource first bind ListBox to BindingSource bs and bind bs to code case: 1-1: updatable add all

 private void btnAdd_Click(object sender, EventArgs e)     {     ListBox.SelectedObjectCollection selectObj = this.listLeft.SelectedItems;foreach (DataRowView item in selectObj){Sta sta = new Sta();sta.STCD = int.Parse(item.Row.ItemArray[0].ToString());sta.Name = item.Row.ItemArray[1].ToString();list.Add(sta);}BindingSource bs = new BindingSource();bs.DataSource = list;this.listRight.DataSource = bs;this.listRight.DisplayMember = "NAME";this.listRight.ValueMember = "STCD";     }



1-2: Remove duplicate items after modification


Private void btnAdd_Click (object sender, EventArgs e) {ListBox. selectedObjectCollection selectObj = this. listLeft. selectedItems; foreach (DataRowView item in selectObj) {bool B = true; // to check whether the listright item already exists, do not add foreach (Model. sta rightItem in this. listRight. items) {if (rightItem. name = item. row. itemArray [1]. toString () {B = false ;}} if (B) {Sta sta = new Sta (); sta. STCD = int. parse (item. row. itemArray [0]. toString (); sta. name = item. row. itemArray [1]. toString (); list. add (sta) ;}} BindingSource bs = new BindingSource (); bs. dataSource = list; this. listRight. dataSource = bs; this. listRight. displayMember = "NAME"; this. listRight. valueMember = "STCD ";}
1-3: After setting the DataSource attribute, the item set cannot be modified and can only be bound to the list set. I don't know if there is a good solution, the root cause is that data sources cannot be used to bind data. Only the final version 0.0 of item 1-4 can be directly added.

# Add listboxitemsListBox to the right of region. selectedObjectCollection selectObj = this. listLeft. selectedItems; foreach (Model. sta item in selectObj) {bool B = true; // if you want to check whether this item already exists in listright, no foreach (Model is added. sta rightItem in this. listRight. items) {if (rightItem. name = item. name) {B = false ;}}if (B) {Sta sta = new Sta (); sta. STCD = item. STCD; sta. name = item. name; this. listRight. items. add (sta) ;}} this. listRight. displayMember = "NAME"; this. listRight. valueMember = "STCD"; # endregion // Delete the added item on the left // 1-2: remove the selected item int icount = listLeft. selectedItems. count; for (int I = 0; I <icount; I ++) {this. listLeft. items. remove (listLeft. selectedItems [icount-1-I]);}


Urgent questions about C # listbox

List <string> items = new List <string> (); // defines the content in a container's combox.
Items. Add ("B ");
Items. Add ("");
Items. Add ("C ");

Items. Sort (); // sorts the content in it. The default value is the alphabetic order.

ComboBox. Items. AddRange (items. ToArray (); // Add it to combox.

// In the SelectedValueChanged event of Combox, add the selected items of combox to listBox
Private void comboBox_SelectedValueChanged (object sender, EventArgs e)
ComboBox cbx = sender as ComboBox;
If (cbx! = Null)
List <object> items = new List <object> ();
Items. Add (cbx. SelectedItem );

Foreach (object o in listBox. Items)
Items. Add (o );

Items. Sort ();
ListBox. Items. Clear ();
ListBox. Items. AddRange (items. ToArray ());

Listbox insertion Problems

ListBox1.SelectedIndex is the index number of the Option item. If it is set to <0, no option is selected. Use listBox1.Items. add is directly added to the end. If the value is not less than 0, use listBox1.Items. insert to the option (that is, the selected item) before the index number is listBox1.SelectedIndex.

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: 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.