Bibhas Debnath
Bibhas Debnath

Reputation: 14939

Using enter key to check CheckBoxes in C#.NET

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??

Upvotes: 1

Views: 4826

Answers (2)

Keith Adler
Keith Adler

Reputation: 21178

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

Upvotes: 1

SLaks
SLaks

Reputation: 887489

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.

Upvotes: 3

Related Questions