Advancing On Pressing Enter
-
I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
-
I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
-
I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
I do not know how to avoid sound. But you can achieve the behavior by tracking keypress event
private void radioButton1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
Yourcontrol.Focus();
}
}HTH
Jinal Desai - LIVE Experience is mother of sage....
-
I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
Hy, Use the KeyDown event and compare:
if(e.KeyCode == Keys.Enter){
e.Handled = true;//stops the beep/bonk noise
e.SupressKeyPress = true;//it bypasses the KeyPress eventtxtYouNameIt.Focus();//focuses on the control you specify
}Something like that should work
Just an irritated, ranting son of ... an IT guy. At your trolling services
-
I do not know how to avoid sound. But you can achieve the behavior by tracking keypress event
private void radioButton1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
Yourcontrol.Focus();
}
}HTH
Jinal Desai - LIVE Experience is mother of sage....
Thanks, Jinal! It works great! :-D
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
-
Hy, Use the KeyDown event and compare:
if(e.KeyCode == Keys.Enter){
e.Handled = true;//stops the beep/bonk noise
e.SupressKeyPress = true;//it bypasses the KeyPress eventtxtYouNameIt.Focus();//focuses on the control you specify
}Something like that should work
Just an irritated, ranting son of ... an IT guy. At your trolling services
Thanks! This is a little more general solution, and good for a User Control intended for re-use.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
-
Thanks! This is a little more general solution, and good for a User Control intended for re-use.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
-
I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
Hi Roger, The answers give already, work perfectly well in circumstances where you know the Name of the control that you want to go to and are prepared to write an individual
KeyDown
/Keypress
(select your poison) handler for each control. However, for circumstances where you want to simply follow theTabOrder
you have already set and, where applicable, use one handler for several controls, theSystem.Windows.Forms.Control
class has a built in method to do exactly what you want. Look up theSystem.Windows.Forms.Control.GetNextControl()
method. An example:private void Comunal\_KeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { e.Handled = true; // true means go forwards this.GetNextControl((Control)sender, true).Focus(); } }
This way you can assign the handler to all the controls in your User Control, provided that you are not already using their
KeyDown
event for something else.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
-
I have a User control that contains three radio buttons - rbCirc, rbRect, and rbTrap - representing 3 possible shapes, and three textboxes for user values. I'm used to programs that automatically advance to the next item in the tab order when I press Enter, but I haven't been able to duplicate that behavior. Tab works as expected, but Enter just makes a "Bonk" sound, at least in the debugger. What can I do to make the focus advance when a user presses Enter?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
Everything already said will work, but you might also want to consider the "KeyPreview" property of the Form. This lets the Form see the key-press before it goes to any controls, so it allows you to create a handler at the Form level instead of a handler for each individual control. Another advantage is that all your code will be inside a single handler instead of spread out among all the various controls.