treat return key as tab key?

  • Thread starter Thread starter Wanjun Yu
  • Start date Start date
W

Wanjun Yu

Hi All,
On my windows form, when the user hits the return key, I'll need to treat it
as a tab key. i.e. when the user hits a return, the focus will move to the
next item in the tab-order on the windows form.

I guess I can add a KeyDown event handler for each componet controls on the
windows form, but that does not seem to be efficient.

Can I do it at the forms level? What i'm thinking is on FORMS if I can get
this return event, I can send another tab key-down event. I know how to do
this in MFC, but not in C#.

Thanks.

WJ
 
Sure, first set the Form's KeyPreview property to true:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.keypreview(VS.80).aspx

This causes the Form to intercept KeyDown events for any Controls in the
Form. It can then pass on the event, or handle it, or both. Then handle the
KeyDown event for the Form. If you've done MFC before, the rest should be
easy.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

I just flew in from Chicago with
a man with a wooden leg named Smith
who shot an elephant in my pajamas.
So I bit him.
 
Thansk, Kevin.

One more question to ask though, how to send a tab key message to the form?

WJ
 
Hi Wanjun,

You can select the next control in the tab order using the following method:

"Control.SelectNextControl Method"
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.selectnextcontrol(VS.80).aspx

If you want to execute some code that uses the next control in the tab order
before it's focused then use the following methods instead:

"Control.GetNextControl Method"
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.getnextcontrol(VS.80).aspx

"Control.Select Method"
http://msdn2.microsoft.com/en-us/library/7wt11hea(VS.80).aspx
 
Press the TAB key?

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

I just flew in from Chicago with
a man with a wooden leg named Smith
who shot an elephant in my pajamas.
So I bit him.
 
Dave, Thanks. It works.

Wanjun

Dave Sexton said:
Hi Wanjun,

You can select the next control in the tab order using the following
method:

"Control.SelectNextControl Method"
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.selectnextcontrol(VS.80).aspx

If you want to execute some code that uses the next control in the tab
order before it's focused then use the following methods instead:

"Control.GetNextControl Method"
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.getnextcontrol(VS.80).aspx

"Control.Select Method"
http://msdn2.microsoft.com/en-us/library/7wt11hea(VS.80).aspx
 
I was about to post the same question when I found this thread. We have a
custom app written in C# which does lots of strange things when our users
hit the Enter key out of habit instead of the Tab key. The programmer claims
it takes a lot of time and effort to modify the Windows forms to interpret
the Return key to be a Tab key. I suspect he's not correct. Is the above
suggestion the simplest way to cause the Enter key to always act like a Tab
key? Is this a lot of work to pull this off for a form?
- John Loewen
 
Hi John,
I was about to post the same question when I found this thread. We have a
custom app written in C# which does lots of strange things when our users hit
the Enter key out of habit instead of the Tab key. The programmer claims it
takes a lot of time and effort to modify the Windows forms to interpret the
Return key to be a Tab key. I suspect he's not correct.

If there is already code in place to process complex key event logic it's
quite possible that it might be relatively complex to change the meaning of
the "enter" key in certain contexts.
Is the above suggestion the simplest way to cause the Enter key to always
act like a Tab key? Is this a lot of work to pull this off for a form?

In general, it's quite easy and the description in this thread is all that
most applications need to convert "enter" into "tab", so to speak.

One thing to consider is if the Form.AcceptButton property is assigned. It
must be unassigned in order to change the behavior of the enter key unless the
key is handled before the Form gets a hold of it (I assume that handling the
Form.KeyPress event and setting KeyPreview to true should be enough, but I
haven't tested this myself).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top