/* _____________________________________________________________________________________ USING LIST BOXES STEP 3 _____________________________________________________________________________________ This shows how to allow multiple selections from one LB to another. Make sure to set the selectionmode property of the first LB to MultiExtended It also shows how to use the foreach loop to cycle through the selectedItems Collection * * Added ability to stop duplicates! Updated 12/28/06 to C# 2005 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Using_ListBoxes_Part_3 { public partial class frmStep3 : Form { public frmStep3() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { lstItems.Items.Add("Spark plugs"); lstItems.Items.Add("Battery"); lstItems.Items.Add("Coolant"); lstItems.Items.Add("Wiper Blades"); lstItems.Items.Add("Hand Cleaner"); lstItems.Items.Add("Alternator Belt"); lstItems.Items.Add("Radiator Hose"); } private void btnAdd_Click(object sender, EventArgs e) { /*---now cycle through the selected items collection and send them to the other list box. SelectedItems is created for us when the program runs by the listbox itself...it keeps track of what they chose. Then all we have to do is to read through that list and do something with them. This SelectedItems Collection is only available to us at run-time. Not design time. */ foreach (string myItem in lstItems.SelectedItems) { if (lstPurchased.Items.Contains(myItem) == false) lstPurchased.Items.Add(myItem); } } private void btnClear_Click(object sender, EventArgs e) { lstPurchased.Items.Clear(); } private void btnQuit_Click(object sender, EventArgs e) { this.Close(); } } }