The code below moves the selected item in the listbox downwards to the next item in the listbox, but it unchecks the selected item if it was checked. How can I prevent this?
private void buttonMoveDown_Click(object sender, EventArgs e)
{
int iIndex = checkedListBox1.SelectedIndex;
if (iIndex == -1)
{
return;
}
moveListboxItem(checkedListBox1, iIndex, iIndex + 1);
}
Thanks
The code for moveListboxItem is as follows:
private void moveListboxItem(CheckedListBox ctl, int fromIndex,int toIndex)
{
if(fromIndex == toIndex)
{
return;
}
if(fromIndex < 0 )
{
fromIndex = ctl.SelectedIndex;
}
if(toIndex < 0 || toIndex > ctl.Items.Count - 1)
{
return;
}
object data = ctl.Items[fromIndex];
ctl.Items.RemoveAt(fromIndex);
ctl.Items.Insert(toIndex, data);
ctl.SelectedIndex = toIndex;
}