Wednesday, April 13, 2011

SetItemChecked(index,boolean) VS SetItemCheckState(index,Checkstate)

I just stumbled upon the SetItemChecked and SetItemCheckState when working on a checked listbox.

The SetItemChecked takes an listindex and True/false to either set the listitem to checked or unchecked.

The SetItemCheckState takes an listindex and a CheckState-enum to set the state to either checked, unchecked or Indeterminate.

Why does SetItemChecked exists, when SetItemCheckState does cover all states?

Is it just to make it easy to use a boolean because its the most common use to check/uncheck or has I missed something?

And if it just to make it easier to use a boolean, why not have that as an overload to the function?

Thanks.

From stackoverflow
  • I vote for

    Is it just to make it easy to use a boolean because its the most common use to check/uncheck or has I missed something?

    But the extra function will normally wont be used (only the Indeterminate case is added)

    EDIT: CheckedListBox.SetItemCheckState(1, CheckState.Checked) is horrible to read (but clear) CheckedListBox.SetItemChecked(1, true) is easier to read

  • There is no real difference. SetItemCheck calls SetItemCheckedState like so:

    public void SetItemChecked(int index, bool value)
    {
        this.SetItemCheckState(index, value ? CheckState.Checked : CheckState.Unchecked);
    }
    

    Like you've already assumed, I guess it's just a helper-method to make things simpler for the developer.

  • It is because it is much more common to want to check/uncheck depending on a boolean value, and to avoid having to do this:

    listbox.SetItemCheckState(i, value ? CheckState.Checked : CheckState.UnChecked)

    all the time. Also, it is not an overload because is mirrors the Checked and CheckState properties of the CheckBox.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.