C # WinForm ListBox Add and remove items

Source: Internet
Author: User

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

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.