I want to make a System.Windows.Forms.CheckedListBox where only some
of the values are clickable.
I googled a bit and the best way I seemed to find was to disable
clicking, setting the SelectionMode to None and making a custom event
handler upon click.
this.checkedListBox1.Click += new System.EventHandler
My event handler does a raw computation of the index clicked and
toggles it if it was Unchecked or Checked.
void checkedListBox1_Click1(object sender, System.EventArgs e)
MouseEventArgs mev = e as MouseEventArgs;
if (mev == null)
int n = (mev.Y / checkedListBox1.ItemHeight);
if (n >= checkedListBox1.Items.Count)
CheckState state = checkedListBox1.GetItemCheckState(n);
if (state == CheckState.Unchecked || state ==
CheckState.Checked) // TODO: optimize this row
checkedListBox1.SetItemChecked(n, state ==
My question is more or less: is there a better way of doing this? This
method is hopelessly flawed: I can not toggle items with the keyboard
See blog entry with fancy screen shot here:
complere sample code here http://www.pererikstrandberg.se/blog/code/clbTestForm.cs
and executable here http://www.pererikstrandberg.se/blog/code/clbTestForm.exe
"per9000" <per...@gmail.com> wrote in message
> I want to make a System.Windows.Forms.CheckedListBox where only some
> of the values are clickable.
> I googled a bit and the best way I seemed to find was to disable
> clicking, setting the SelectionMode to None and making a custom event
> handler upon click.
Why not handle ItemCheck and set the NewValue property back to CurrentValue
for items which are supposed to be read-only?
Here is the description of the NewValue property:
This property enables you to determine the new check state for the specified
item before the check state is changed by the CheckedListBox control. In
addition to determining the new check state, you can use this property in an
event handler for the ItemCheck event to change the state to a different
check state than the one specified. For example, if the user placed a check
mark next to an item in the CheckedListBox that you have determined should
not be checked based on the state of your application, you can override the
change in the check mark state by setting this property to its previous
setting or to a different check state.
this.checkedListBox1.CheckOnClick = true;
this.checkedListBox1.ItemCheck += new
void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
if (e.CurrentValue == CheckState.Indeterminate)
e.NewValue = e.CurrentValue;