views:

111

answers:

2

On forms created with pre dotNET VB and C++ (MFC), a checkbox control responded to the plus/minus key without custom programming. When focus was on the checbox control, pressing PLUS would check the box, no matter what the previous state (checked/unchecked), while pressing MINUS would uncheck it, no matter the previous state.

C# winform checkboxes do not seem to exhibit this behavior.

Said behavior was very, very handy for automation, whereby the automating program would set focus to a checkbox control and issue a PLUS or MINUS to check or uncheck it. Without this capability, that cannot be done, as the automation program (at least the one I am using) is unable to query the current state of the checkbox (so it can decide whether to issue a SPACE key to toggle the state to the desired one).

I've gone over the properties of a checkbox in the Visual Studio 2008 IDE and could not find anything that would restore/enable response to PLUS/MINUS.

Since I am in control of the sourcecode for the WinForms in question, I could replace all checkbox controls with a custom checkbox control, but blech, I'd like to avoid that - heck, I don't think I could even consider that given the amount of refactoring that would need to be done.

So the bottom line is: does anyone know of a way to get this behavior back more easily than a coding change?

+5  A: 

I don't see an easy way to get this enabled. However, replacing the existing checkbox shouldn't be terribly daunting:

1- Create new Class Library and create new checkbox (derive from checkbox, override OnKeyPress.)
2- Reference new Library to existing projects.
3- Search and Replace System.Windows.Forms.Checkbox with YourNamespace.NewCheckbox

Jacob G
+1 because this is the only way to do it that or catch the on key down event on every form for every check box. Now that would suck. :)
Tony
Yeah, that was the conclusion I drew. It's just that I'd have to change a very large quantity of projects and upset quite a few apple carts in the process. I needed to make sure no one knew of any magic bullet--they do exist sometimes--before resorting to such an action, so I thought I'd take a shot and ask here.
Scott
But, if checkbox had exposed a property like "EnablePlusMinusInteraction", would't you still have to change that line in your dozen of projects as well ?With this solution, you must only create a dll with the overriden checkbox, add a reference in your projects, and change the instantiation line.(Anyway, it could be annoying in one point: Add Reference sometimes is very looong :P )
digEmAll
+2  A: 

As answered by Jacob G you can easily override CheckBox Control in this way:

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


    }
digEmAll
Yes, but not so easily change dozens of projects to use it.
Scott