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

 


In C #, there are several listbox problems ,,,

Add one row of listbox1 to another
Add a textbox to set the number of rows and convert the data type.
Then extract it using lines [line n ].
The following is the code that the landlord previously asked for. If the code is correct, I will reference it (saving the keyboard)

Read the txt file row by row and add it to listbox1:
Private void button3_Click (object sender, EventArgs e)
{
StreamReader file = new StreamReader (@ "c: \ IP.txt", System. Text. Encoding. Default );
String s = "";
While (s! = Null)
{
S = file. ReadLine ();
If (s! = Null &&! S. Equals (""))
ListBox1.Items. Add (s );
}
File. Close ();
}

Add the value in listbox1 to listbox2:
Private void button4_Click (object sender, EventArgs e)
{
If (listBox1.Items. Count> 0 & listBox1.SelectedItem! = Null)
{
ListBox2.Items. Add (listBox1.SelectedItem );
}
}

C # listBox Problems

Private void button3_Click (object sender, EventArgs e)
{
If (listBox1.SelectedIndex <listBox1.Items. Count-1) // <=== = this sentence is compared. The error is found here.
ListBox1.SelectedIndex = (listBox1.SelectedIndex + 1 );
Else if (listBox1.Items. Count> 0)
ListBox1.SelectedIndex = 0;
PlaySong (listBox1.SelectedIndex + 1 );
}

//-----------------------
// When the form size is changed, the listbox size is also changed.
Private void Form1_Resize (object sender, EventArgs e)
{
ListBox1.Top = 0;
ListBox1.Left = 0;
ListBox1.Width = this. Width-5; // <= this value is changed as required.
ListBox1.Height = this. Height-10; // <= this value is changed as required.
}

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.