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:
moveListboxItem的代码如下:
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;
}
1 个解决方案
#1
You'll need to post the code for moveListBoxItem in order for us to be able to help out.
您需要发布moveListBoxItem的代码才能让我们能够提供帮助。
My suspicion is that moveListBoxItem looks something like this.
我怀疑moveListBoxItem看起来像这样。
void moveListBoxItem(CheckedListBox list, int oldIndex, int newIndex ) {
object old = list.Items[oldIndex];
list.Items.RemoveAt(oldIndex);
list.Items.Insert(newIndex, old);
}
If that's the case, the reason it's not working is because once you remove the object, the CheckedListBox no longer tracks the checked state of the particular index. You'll need to re-add this later.
如果是这种情况,那么它不起作用的原因是因为一旦删除了对象,CheckedListBox就不再跟踪特定索引的已检查状态。您需要稍后重新添加。
void moveListBoxItem(CheckedListBox list, int oldIndex, int newIndex ) {
var state = list.GetItemCheckedState(oldIndex);
object old = list.Items[oldIndex];
list.Items.RemoveAt(oldIndex);
list.Items.Insert(newIndex, old);
list.SetItemCheckedState(newIndex, state);
}
EDIT: Update for the actual moveListBoxItem code. You need to propagate the CheckState to the new index as well. Removing it from the collection essentially clears the stored state.
编辑:更新实际的moveListBoxItem代码。您还需要将CheckState传播到新索引。从集合中删除它基本上清除了存储状态。
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];
CheckState state = ctl.GetItemCheckState(fromIndex);
ctl.Items.RemoveAt(fromIndex);
ctl.Items.Insert(toIndex, data);
ctl.SetItemCheckState(toIndex, state);
ctl.SelectedIndex = toIndex;
}
#1
You'll need to post the code for moveListBoxItem in order for us to be able to help out.
您需要发布moveListBoxItem的代码才能让我们能够提供帮助。
My suspicion is that moveListBoxItem looks something like this.
我怀疑moveListBoxItem看起来像这样。
void moveListBoxItem(CheckedListBox list, int oldIndex, int newIndex ) {
object old = list.Items[oldIndex];
list.Items.RemoveAt(oldIndex);
list.Items.Insert(newIndex, old);
}
If that's the case, the reason it's not working is because once you remove the object, the CheckedListBox no longer tracks the checked state of the particular index. You'll need to re-add this later.
如果是这种情况,那么它不起作用的原因是因为一旦删除了对象,CheckedListBox就不再跟踪特定索引的已检查状态。您需要稍后重新添加。
void moveListBoxItem(CheckedListBox list, int oldIndex, int newIndex ) {
var state = list.GetItemCheckedState(oldIndex);
object old = list.Items[oldIndex];
list.Items.RemoveAt(oldIndex);
list.Items.Insert(newIndex, old);
list.SetItemCheckedState(newIndex, state);
}
EDIT: Update for the actual moveListBoxItem code. You need to propagate the CheckState to the new index as well. Removing it from the collection essentially clears the stored state.
编辑:更新实际的moveListBoxItem代码。您还需要将CheckState传播到新索引。从集合中删除它基本上清除了存储状态。
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];
CheckState state = ctl.GetItemCheckState(fromIndex);
ctl.Items.RemoveAt(fromIndex);
ctl.Items.Insert(toIndex, data);
ctl.SetItemCheckState(toIndex, state);
ctl.SelectedIndex = toIndex;
}