WinForms checkboxes not responding to plus / minus keys - easy workaround?
In forms built with pre dotNET VB and C ++ (MFC), the checkbox control responded to the plus / minus key without any special programming. When the focus was on a checkbox control, clicking +will check the checkbox regardless of the previous state (checked or unchecked), and clicking -will cancel it regardless of the previous state.
CheckboxesC # winforms don't show this behavior.
This behavior was very, very handy for automation, causing the automation program to set focus on the checkbox control and issue +either -for checking or unchecking. Without this capability, this cannot be done, since the automation program (at least the one I use) cannot query the current state of that checkbox (so it can decide whether to release the key Spaceto switch the state to the desired one).
I looked at the checkbox properties in the Visual Studio 2008 IDE and couldn't find anything that could restore / enable the answer to +/ -.
Since I control the source code for the WinForms in question, I could replace all the checkbox controls with a custom checkbox control, but blech, I would like to avoid that - heck, I don't think I could even consider that given the scope of the refactoring to be done.
So bottom line: Does anyone know a way to get this behavior back more easily than changing the encoding?
a source to share
I don't see an easy way to enable this. However, replacing an existing checkbox shouldn't be terribly difficult:
1- Create a new class library and create a new checkbox (uncheck the checkbox, override OnKeyPress.)
2- Link the new library to existing projects.
3- Find and replace System.Windows.Forms.Checkbox
withYourNamespace.NewCheckbox
a source to share
As Jacob G answered, you can easily override CheckBox Control like this:
public class MyCheckBoxOverride:CheckBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Oemplus)
{
this.Checked = true;
}
else if(e.KeyCode == Keys.OemMinus)
{
this.Checked = false;
}
base.OnKeyDown(e);
}
}
a source to share