trap "enter" key on button to send "tab" to next control instead of fired OnClick(...)
-
Wednesday, November 01, 2006 12:56 AMI have a window's form with many buttons, what I like to do is move arround all edit controls using "enter key" so every time you finish fill your data in an edit control hit enter and move to next control, I know how to do it usind
SendKeys.Send( "{TAB}" );
But what about buttons, when I hit enter , button.onclick is executed.
How can move to next control instead of OnClick(), but when you actually click with the mouse over this button you get OnClick as usual.
Thanks in advance for your help and time.
Edward Mac
Answers
-
Wednesday, November 01, 2006 1:29 PM
You need to create new button control inherited from Button control. Then you need to override ProcessDialogKey and do some changes like:
protected override bool ProcessDialogKey(Keys keyData)
{
switch (keyData)
{
case Keys.Enter:
case Keys.Space:
return base.ProcessDialogKey(Keys.Tab);
}
return base.ProcessDialogKey(keyData);
}You can use the same way of solving Tab on Enter for text box also, bacause if you use KeyPress, it is not the right way. You can also replace + with Tab or - with Shift + Tab, because it's also used for fast entry forms.
All Replies
-
Wednesday, November 01, 2006 3:41 AMModerator
hi,
i guess button was designed for that it has not job but to click it and fire on click event, so may be you choose a wrong control to use event though you can use the on click to move to next control lets say you have button1 and button2 in your form, so in your form constractor you add this
public constractor()
{
intializeComponent();
button1.Click += new eventHandler(Button_Click);
button2.Click += new eventHandler(Button_Click);
}
void Button_Click(object sender, EventArgs args)
{
//send your tab here
}
hope this helps
-
Wednesday, November 01, 2006 5:40 AMOK, let me put in this way,
I would like to have an endless loop using Enter-key all around the data entry form, no matter what kind of controls are in.
more deep about it:
1) Why all keyboard is trapped from a button control except Enter-key.
2) Where C# or windows defined: enter-key in a button fire OnClick event.
3) This kind of situations makes me feel that I don´t have total control over C#'s controls.
4)Which event trap all button's keyboard keys, I mean all.
5) Do I need to get back to MFC Visual C++ to accomplish this deep programming?
thanks again for your answer.
Edward -
Wednesday, November 01, 2006 1:29 PM
You need to create new button control inherited from Button control. Then you need to override ProcessDialogKey and do some changes like:
protected override bool ProcessDialogKey(Keys keyData)
{
switch (keyData)
{
case Keys.Enter:
case Keys.Space:
return base.ProcessDialogKey(Keys.Tab);
}
return base.ProcessDialogKey(keyData);
}You can use the same way of solving Tab on Enter for text box also, bacause if you use KeyPress, it is not the right way. You can also replace + with Tab or - with Shift + Tab, because it's also used for fast entry forms.
-
Thursday, September 29, 2011 8:31 AMI just neded to do the same thing and this one worked just fine.Thank you

