我做了一個擴展方法,將兩個物品的位置交換到CheckedListBox中。該方法放在一個靜態的Utilities類中。問題是CheckState不能移動。因此,如果我將列表中選中的項目向上移動,複選框狀態將保留,移動的項目將從它正在替換的項目中接管CheckState。以靜態方式獲取物品CheckState
我的代碼如下所示:
public static System.Windows.Forms.CheckedListBox.ObjectCollection Swap(this System.Windows.Forms.CheckedListBox.ObjectCollection lstBoxItems, int indexA, int indexB)
{
if (indexB > -1 && indexB < lstBoxItems.Count - 1)
{
object tmpItem = lstBoxItems[indexA];
lstBoxItems[indexA] = lstBoxItems[indexB];
lstBoxItems[indexB] = tmpItem;
}
return lstBoxItems;
}
我想是這樣的(不顯着工作)
public static System.Windows.Forms.CheckedListBox.ObjectCollection Swap(this System.Windows.Forms.CheckedListBox.ObjectCollection lstBoxItems, int indexA, int indexB)
{
if (indexB > -1 && indexB < lstBoxItems.Count - 1)
{
object tmpItem = lstBoxItems[indexA];
System.Windows.Forms.CheckState state = tmpItem.CheckState;
lstBoxItems[indexA] = lstBoxItems[indexB];
lstBoxItems[indexB] = tmpItem;
}
return lstBoxItems;
}
的代碼簡稱像這樣
myCheckedListBox.Items.Swap(selectedIndex, targetIndex);
謝謝。我只是在看到你的解決方案之前就解決了它 - 我甚至使用了相同的變量名和語法:-)。 Thx驗證它! Upvote並接受那一個。 – Christoffer