[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: Let the user arrange ListBox items in C#

[Let the user arrange ListBox items in C#]

This example demonstrates one way that you can let the user arrange ListBox items. To rearrange the items, the user can click on an item and then use the program's buttons to move the item to the top of the list, up one position, down one position, or to the bottom of the list.

When the user selects an item, the following code enables and disables the appropriate buttons.

// Enable and disable the appropriate buttons. private void lstAnimals_SelectedIndexChanged(object sender, EventArgs e) { btnUp.Enabled = (lstAnimals.SelectedIndex > 0); btnToTop.Enabled = btnUp.Enabled; btnDown.Enabled = (lstAnimals.SelectedIndex < lstAnimals.Items.Count - 1); btnToBottom.Enabled = btnDown.Enabled; }

If the selected index is greater than 0 (the first item has index 0), then the item is not at the top of the list. In that case, the code enables the Move Up and Move To Top buttons.

If the selected index is less than the number of items minus one (the last item has index lstAnimals.Items.Count - 1), then the item is not at the end of the list. In that case, the code enables the Move Down and Move To Bottom buttons.

After it moves the item, the button selects it. That is less confusing than leaving no item selected, it lets the user click the buttons multiple times to move the item repeatedly, and it ensures that the item remains visible if it moves off the bottom or top of the visible list.

The basic approach that each button uses to move the selected item is the same: save the item's index if necessary, save the item, remove the item from the list box, and insert the item in its new position.

The following code shows how the buttons work.

// Move the selected item to the top of the list (index 0). private void btnToTop_Click(object sender, EventArgs e) { object item = lstAnimals.SelectedItem; lstAnimals.Items.RemoveAt(lstAnimals.SelectedIndex); lstAnimals.Items.Insert(0, item); lstAnimals.SelectedIndex = 0; } // Move the selected item up one position. private void btnUp_Click(object sender, EventArgs e) { int index = lstAnimals.SelectedIndex; object item = lstAnimals.SelectedItem; lstAnimals.Items.RemoveAt(lstAnimals.SelectedIndex); lstAnimals.Items.Insert(index - 1, item); lstAnimals.SelectedIndex = index - 1; } // Move the selected item down one position. private void btnDown_Click(object sender, EventArgs e) { int index = lstAnimals.SelectedIndex; object item = lstAnimals.SelectedItem; lstAnimals.Items.RemoveAt(lstAnimals.SelectedIndex); lstAnimals.Items.Insert(index + 1, item); lstAnimals.SelectedIndex = index + 1; } // Move the selected item to the end of the list. private void btnToBottom_Click(object sender, EventArgs e) { object item = lstAnimals.SelectedItem; lstAnimals.Items.RemoveAt(lstAnimals.SelectedIndex); lstAnimals.Items.Add(item); lstAnimals.SelectedIndex = lstAnimals.Items.Count - 1; }

That's all there is to it. This method is cumbersome if you want to move ListBox items a long way through the list, but it's easy enough if the list is short or if the user doesn't need to move items very far.

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

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