P
per9000
Hejsan!
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.
<code>
this.checkedListBox1.SelectionMode =
System.Windows.Forms.SelectionMode.None;
this.checkedListBox1.Click += new System.EventHandler
(this.checkedListBox1_Click1);
</code>
My event handler does a raw computation of the index clicked and
toggles it if it was Unchecked or Checked.
<code>
void checkedListBox1_Click1(object sender, System.EventArgs e)
{
MouseEventArgs mev = e as MouseEventArgs;
if (mev == null)
return;
int n = (mev.Y / checkedListBox1.ItemHeight);
if (n >= checkedListBox1.Items.Count)
return;
CheckState state = checkedListBox1.GetItemCheckState(n);
if (state == CheckState.Unchecked || state ==
CheckState.Checked) // TODO: optimize this row
checkedListBox1.SetItemChecked(n, state ==
CheckState.Unchecked);
}
</code>
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
for example.
See blog entry with fancy screen shot here:
http://www.pererikstrandberg.se/blog/index.cgi?page=CsharpCheckedListBoxTest,
complere sample code here http://www.pererikstrandberg.se/blog/code/clbTestForm.cs
and executable here http://www.pererikstrandberg.se/blog/code/clbTestForm.exe
Thanks
Per
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.
<code>
this.checkedListBox1.SelectionMode =
System.Windows.Forms.SelectionMode.None;
this.checkedListBox1.Click += new System.EventHandler
(this.checkedListBox1_Click1);
</code>
My event handler does a raw computation of the index clicked and
toggles it if it was Unchecked or Checked.
<code>
void checkedListBox1_Click1(object sender, System.EventArgs e)
{
MouseEventArgs mev = e as MouseEventArgs;
if (mev == null)
return;
int n = (mev.Y / checkedListBox1.ItemHeight);
if (n >= checkedListBox1.Items.Count)
return;
CheckState state = checkedListBox1.GetItemCheckState(n);
if (state == CheckState.Unchecked || state ==
CheckState.Checked) // TODO: optimize this row
checkedListBox1.SetItemChecked(n, state ==
CheckState.Unchecked);
}
</code>
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
for example.
See blog entry with fancy screen shot here:
http://www.pererikstrandberg.se/blog/index.cgi?page=CsharpCheckedListBoxTest,
complere sample code here http://www.pererikstrandberg.se/blog/code/clbTestForm.cs
and executable here http://www.pererikstrandberg.se/blog/code/clbTestForm.exe
Thanks
Per