I have a checked list box and I’m writing code for two buttons, 1 that moves all selected items upward, and one that moves each item downward. The one for moving up works, but I cant get the other one to work:
//Move up
private void button2_Click(object sender, EventArgs e)
{
for (int i = 1; i < checkedListBox1.Items.Count; i++ ) {
if (checkedListBox1.GetItemChecked(i)) {
checkedListBox1.Items.Insert(i - 1, checkedListBox1.Items[i]);
checkedListBox1.SetItemChecked(i - 1, true);
checkedListBox1.Items.RemoveAt(i + 1);
}
}
}
//Move Down
private void button3_Click(object sender, EventArgs e)
{
for (int i = checkedListBox1.Items.Count - 2; i >= 0; i--)
{
if (checkedListBox1.GetItemChecked(i))
{
checkedListBox1.Items.Insert(i + 1, checkedListBox1.Items[i]);
checkedListBox1.SetItemChecked(i + 1, true);
checkedListBox1.Items.RemoveAt(i);
}
}
}
I think you need this in the second method:
You’re current method is inserting a copy of the current element before the following element, which bascially just puts it in the same location.