开发者

WinForms checkboxes do not respond to plus/minus keys - easy workaround?

开发者 https://www.devze.com 2022-12-29 07:02 出处:网络
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 + would check the box,

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 + would check the box, no matter what the previous state (checked/unchecked), while pressing - would uncheck it, no matter the previous st开发者_如何学编程ate.

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 + or - 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 +/-.

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?


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


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


    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号