Two listbox add and remove items
1 #regionAdd/Remove2 //Add3 Private voidbtnAdd_Click (Objectsender, EventArgs e)4 {5 if(ListboxA.Items.Count <=0)6 {7 return;8 }9 This. Additems (Listboxb, listboxa.selecteditems);Ten This. Removeitems (Listboxa, listboxa.selecteditems); One //Updatelistviewhandler ();//invoke delegate, update original list refresh MainForm form A } - //removed from - Private voidBtnremove_click (Objectsender, EventArgs e) the { - if(ListboxB.Items.Count <=0) - { - return; + } - This. Additems (Listboxa, listboxb.selecteditems); + This. Removeitems (Listboxb, listboxb.selecteditems); A //Updatelistviewhandler ();//invoke delegate, update original list refresh MainForm form at } - /// <summary> - ///Add items to a ListBox - /// </summary> - /// <param name= "Lstbox" ></param> - /// <param name= "items" ></param> in Private voidAdditems (UI. Listboxex Lstbox, IEnumerable items) - { toDataTable dt =NULL; + foreach(ObjectIteminchitems) - { the if(item isDataRowView) *DT =((DataRowView) item). Row.Table.Clone (); $ if(item isDataRow)Panax NotoginsengDT =(DataRow) item). Table.clone (); - Break; the } + if(Lstbox.datasource! =NULL) ADT =(DataTable) (Lstbox.datasource). Copy (); the foreach(ObjectIteminchitems) + { - if(item isDataRowView) $ dt. Rows.Add ((DataRowView) item). Row.itemarray); $ if(item isDataRow) - dt. Rows.Add ((DataRow) item). ItemArray); - } theLstbox.datasource =DT; - if( This. Text.contains ("Add or Remove Exc"))Wuyi { theLstbox.displaymember ="Excname"; -Lstbox.valuemember ="Excid"; Wu } - Else if( This. Text.contains ("Add or Remove Cs")) About { $Lstbox.displaymember ="CSName"; -Lstbox.valuemember ="CsID"; - } - } A /// <summary> + ///remove items from a ListBox the /// </summary> - /// <param name= "Lstbox" ></param> $ /// <param name= "items" ></param> the Private voidRemoveitems (UI. Listboxex Lstbox, IEnumerable items) the { theDataTable dt =(DataTable) lstbox.datasource); theDataTable NEWDT =dt. Clone (); - BOOLFlag =false; in //because the direct deletion of the DataRow is saved, so it's handled in such an ugly way the foreach(DataRow Drinchdt. Rows) the { About foreach(DataRowView Iteminchitems) the { the if(dr = =item. Row) the { +Flag =true; - Break; the }Bayi Else theFlag =false; the } - if(!flag) - NewDt.Rows.Add (Dr. ItemArray); the Else the Continue; the } theLstbox.datasource =NEWDT; - if( This. Text.contains ("Add or Remove Exc")) the { theLstbox.displaymember ="Excname"; theLstbox.valuemember ="Excid";94 } the Else if( This. Text.contains ("Add or Remove Cs")) the { theLstbox.displaymember ="CSName";98Lstbox.valuemember ="CsID"; About } - }101 #endregion
C # WinForm ListBox Add and remove items