Making ENTER work as TAB for the application - HOW???

  • Thread starter Thread starter Ali Baba
  • Start date Start date
A

Ali Baba

Hi,

I need the enter key to work as a tab so that the focus goes to the next
control. I don't want to write a KEY_PRESS handler for all controls. Is
there a better way? IMPORTANT: I only want this for Edit controls that
do NOT allow multiple lines. No other control besides Edit Control
should have this behavior


Regards,
AB
 
Ali,

Why not set a KEY_PRESS handler up for the form and intercept the
message before it is dispatched to the controls? You can make a
determination based on the handle of the control that the message is
destined for. Also, you will want to set the KeyPreview property of the
form to true.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,

I need the enter key to work as a tab so that the focus goes to the next
control. I don't want to write a KEY_PRESS handler for all controls. Is
there a better way? IMPORTANT: I only want this for Edit controls that do
NOT allow multiple lines. No other control besides Edit Control should have
this behavior


Regards,
AB
 
Why not use inheritance..

Extend the edit control to handle the enter key, and
replace all the edit controls on your form...

HTH
 
I use the enter key as a tab on almost all of my controls (textbox,
combobox,...)...
Suppose I have a textbox:

class MyTextBox : TextBox
{
private Control nextItem;
public Control NextItem
{
get, set...
}

....

protected override OnKeyPressed(...)
{
// if key is enter(13) and nextItem != null then call
nextItem.Focus()
}
}

hope it helps :)
saso
 
Hi,

I need the enter key to work as a tab so that the focus goes to the next
control. I don't want to write a KEY_PRESS handler for all controls. Is
there a better way? IMPORTANT: I only want this for Edit controls that
do NOT allow multiple lines. No other control besides Edit Control
should have this behavior

Very easy:
private void General_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}

Now, add this eventhandler to the KeyDown event of every control
that should have this enter==tab behaviour. You can add more than 1 event
handler to an event. You can add the same handler (the one I mentioned
above) to more than one control.

FB
 
Back
Top