简体   繁体   中英

Get item CheckState in a static method

I made an extension method to swap places of two items in a CheckedListBox. The method is put in a static Utilities class. The problem is that the CheckState doesn't travel. So if I move a checked item up in the list, the checkbox state will stay and the moved item will take over the CheckState from the item it's replacing.

My code looks like this:

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;
}

What I want is something like this (which doesn't work obviously)

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;
}

The code is simply called like this

myCheckedListBox.Items.Swap(selectedIndex, targetIndex);

I haven't used the CheckedListBox before, but if I had to hazard a guess looking at the MSDN docs for it, I would say you'd want to use the GetItemCheckedState and the SetItemCheckedState methods. However, that also means you'd have to pass in the CheckedListBox as well rather than just its .Items ObjectCollection .

public static System.Windows.Forms.CheckedListBox Swap(this System.Windows.Forms.CheckedListBox listBox, int indexA, int indexB)
{
    var lstBoxItems = listBox.Items;
    if (indexB > -1 && indexB < lstBoxItems.Count - 1)
    {
        System.Windows.Forms.CheckState stateA = listBox.GetItemCheckState(indexA);
        System.Windows.Forms.CheckState stateB = listBox.GetItemCheckState(indexB);

        object tmpItem = lstBoxItems[indexA];
        lstBoxItems[indexA] = lstBoxItems[indexB];
        lstBoxItems[indexB] = tmpItem;

        listBox.SetItemCheckState(indexA, stateB);
        listBox.SetItemCheckState(indexB, stateA);
    }
    return listBox;
}

So naturally your calling code would change to something like this:

myCheckedListBox.Swap(selectedIndex, targetIndex);

Also, note that my method returns the input CheckedListBox as well instead of the ObjectCollection ; figured that would be more appropriate now given the change of signature parameters.

Maybe the problem is that you should be first getting the current check state of actual list box item instead of from the copy. You already know that the list box is managing the checks separate from the item list content!

You also should consider getting the current checked states for both items A and B. After you perform the item swap then reapply the checked state to the two items so you maintain that status for both swapped items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM