[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Move items between two ListBoxes in C#

[Move items between two ListBoxes in C#]

One way to let the user select items from a list is to use a ListBox or a CheckedListBox. The user can Click, Ctrl+Click, Shift+Click, and user other keyboard and mouse combinations to select some of the items in the list.

Another method is to use buttons to let the user move items between two ListBoxes. One ListBox contains the items that are not selected and the other contains those that are.

Select an item in either list and the program enables the appropriate buttons. For example, if you select an item in the left list, the program enables the > button to let you move the item into the right list. Use the << and >> buttons to move all of the items from one list to the other.

The program uses the MoveSelectedItems method shown below to move items from one ListBox to the other.

// Move selected items from one ListBox to another. private void MoveSelectedItems(ListBox lstFrom, ListBox lstTo) { while (lstFrom.SelectedItems.Count > 0) { string item = (string)lstFrom.SelectedItems[0]; lstTo.Items.Add(item); lstFrom.Items.Remove(item); } SetButtonsEditable(); }

While the source ListBox has items selected, the code adds the first selected item to the destination ListBox and then removes it from the source ListBox. The loop continues until all of the selected items have been removed from the first ListBox.

The following code shows how the program uses this method to move items when the user clicks the < and > buttons.

// Move selected items to lstSelected. private void btnSelect_Click(object sender, EventArgs e) { MoveSelectedItems(lstUnselected, lstSelected); } // Move selected items to lstUnselected. private void btnDeselect_Click(object sender, EventArgs e) { MoveSelectedItems(lstSelected, lstUnselected); }

The MoveAllItems method shown below moves all of the items from one ListBox to another.

// Move all items from one ListBox to another. private void MoveAllItems(ListBox lstFrom, ListBox lstTo) { lstTo.Items.AddRange(lstFrom.Items); lstFrom.Items.Clear(); SetButtonsEditable(); }

This code uses the ListBox control's AddRange method to quickly add all of the source list's items to the destination list. It then clears the source ListBox.

The following code shows how the program moves items when the user clicks the << and >> buttons.

// Move all items to lstSelected. private void btnSelectAll_Click(object sender, EventArgs e) { MoveAllItems(lstUnselected, lstSelected); } // Move all items to lstUnselected. private void btnDeselectAll_Click(object sender, EventArgs e) { MoveAllItems(lstSelected, lstUnselected); }

The last interesting piece of code is the following SetButtonsEditable method.

// Enable and disable buttons. private void SetButtonsEditable() { btnSelect.Enabled = (lstUnselected.SelectedItems.Count > 0); btnSelectAll.Enabled = (lstUnselected.Items.Count > 0); btnDeselect.Enabled = (lstSelected.SelectedItems.Count > 0); btnDeselectAll.Enabled = (lstSelected.Items.Count > 0); }

This method enables or disables the < and > buttons depending on whether items are selected in the corresponding lists. It enables and disables the <<, and >> buttons depending on whether the corresponding lists are empty.

Both of the ListBox controls' SelectedIndexChanged event handlers plus any code that changes the items in a ListBox call this method to make sure only the useful buttons are enabled at any time.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.