views:

283

answers:

2

I want to use Enter key instead of Space key to check the checkboxes..

private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            CheckBox c1 = this.ActiveControl as CheckBox;
            if (e.KeyData == Keys.Enter && this.ActiveControl.Equals(c1))
                c1.Checked = true;
        }

I could do it if i write this code in the KyeUp of the checkbox, but the thing is, I have several Checkboxes in the form and I cant write this under each of their KeyUp, so I need to use it under the KeyUp of the form.. What do I need to change??

+3  A: 

Set the form's KeyPreview property to true.

Alternatively, you could loop through the checkboxes (using the Controls property, perhaps recursively) and add the same handler to every checkbox.

SLaks
Thanx dude, worked. :)
Bibhas
A: 

Simply determine which control has the focus and check/uncheck it as appropriate. This link should help: http://www.webdeveloper.com/forum/archive/index.php/t-36261.html

Nissan Fan