My app has four group boxes with four text boxes and one button in each group box what i would like to do is use the keyboard enter button to tab instead of the tab button and when it gets to my control button instead of left clicking the mouse to activate my control button I would like to press the enter key on the keyboard to activate it has anyone got any ideas how to do this
barry t 0 Newbie Poster
Dani AI
Generated
asked for Enter-to-tab plus Enter-to-activate-button. ’s per-control KeyDown approach works, but it’s brittle and easy to forget for new controls or special cases (multiline textboxes, RichTextBox, combo drops, etc.). Centralizing the logic at the form level is simpler, more reliable, and easier to maintain.
A robust pattern is to intercept Enter centrally and either move focus with SelectNextControl or let the control handle Enter (multiline textboxes or a focused Button). Overriding ProcessCmdKey avoids wiring every control and runs early in the message chain:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
Control ctl = this.ActiveControl;
// let multiline controls accept Return
if (ctl is TextBoxBase tb && tb.AcceptsReturn)
return base.ProcessCmdKey(ref msg, keyData);
// let a focused Button behave normally
if (ctl is Button)
return base.ProcessCmdKey(ref msg, keyData);
// move to next control like Tab
SelectNextControl(ctl, true, true, true, true);
return true; // handled
}
return base.ProcessCmdKey(ref msg, keyData);
} If ProcessCmdKey feels heavy, use Form.KeyPreview = true and handle KeyDown on the form, calling SelectNextControl and setting e.SuppressKeyPress = true to prevent the system “ding” (see KeyEventArgs.SuppressKeyPress). Also consider using the form’s AcceptButton property when a specific button should be invoked by Enter without changing navigation.
Notes: explicitly exclude controls that accept Return (TextBoxBase.AcceptsReturn) and controls where Enter has a semantic meaning. Changing Enter behavior can surprise keyboard users and assistive technologies, so keep conventional behavior where it matters.
References: Form.KeyPreview property, , Control.ProcessCmdKey, , Form.AcceptButton property.
r0ckbaer 3 Junior Poster in Training
Don't know if i understood exactly what you mean, but i guess you want to simulate the TAB button behaviour with the ENTER button, to do this you can do the following (might be buggy and inefficient because quickly tested):
First you have to add keydown events to every control in your form (i assume your Form is called Form1):
private void Form1_Load(object sender, System.EventArgs e)
{
AddEnterEvent((Control)sender);
}
private void AddEnterEvent(Control c)
{
c.KeyDown += new KeyEventHandler(c_KeyDown);
foreach (Control ctlChildren in c.Controls)
{
AddEnterEvent(ctlChildren);
}
} The keydown event code:
private void c_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Control ctlNext = ActiveForm.GetNextControl((Control)sender, true);
ctlNext.Focus();
}
} Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.