チェックリストボックスがあり、2つのボタンのコードを記述しています。1つは選択したすべてのアイテムを上に移動し、もう1つは各アイテムを下に移動します。上に移動するための1つは機能しますが、もう1つを機能させることができません。
//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);
}
}
}