473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

treat return key as tab key?

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
Nov 6 '06 #1
7 2580
Sure, first set the Form's KeyPreview property to true:

http://msdn2.microsoft.com/en-us/lib...ew(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.
"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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

Nov 6 '06 #2
Thansk, Kevin.

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

WJ

"Kevin Spencer" <sp**@uce.govwrote in message
news:eU**************@TK2MSFTNGP02.phx.gbl...
Sure, first set the Form's KeyPreview property to true:

http://msdn2.microsoft.com/en-us/lib...ew(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.
"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>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


Nov 6 '06 #3
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/lib...ol(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/lib...ol(VS.80).aspx

"Control.Select Method"
http://msdn2.microsoft.com/en-us/lib...ea(VS.80).aspx

--
Dave Sexton

"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:O1**************@TK2MSFTNGP03.phx.gbl...
Thansk, Kevin.

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

WJ

"Kevin Spencer" <sp**@uce.govwrote in message
news:eU**************@TK2MSFTNGP02.phx.gbl...
>Sure, first set the Form's KeyPreview property to true:

http://msdn2.microsoft.com/en-us/lib...ew(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.
"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>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



Nov 7 '06 #4
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.
"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:O1**************@TK2MSFTNGP03.phx.gbl...
Thansk, Kevin.

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

WJ

"Kevin Spencer" <sp**@uce.govwrote in message
news:eU**************@TK2MSFTNGP02.phx.gbl...
>Sure, first set the Form's KeyPreview property to true:

http://msdn2.microsoft.com/en-us/lib...ew(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.
"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>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



Nov 7 '06 #5
Dave, Thanks. It works.

Wanjun

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:uf**************@TK2MSFTNGP04.phx.gbl...
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/lib...ol(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/lib...ol(VS.80).aspx

"Control.Select Method"
http://msdn2.microsoft.com/en-us/lib...ea(VS.80).aspx

--
Dave Sexton

"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:O1**************@TK2MSFTNGP03.phx.gbl...
>Thansk, Kevin.

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

WJ

"Kevin Spencer" <sp**@uce.govwrote in message
news:eU**************@TK2MSFTNGP02.phx.gbl...
>>Sure, first set the Form's KeyPreview property to true:

http://msdn2.microsoft.com/en-us/lib...ew(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.
"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
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



Nov 7 '06 #6
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

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:uf**************@TK2MSFTNGP04.phx.gbl...
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/lib...ol(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/lib...ol(VS.80).aspx

"Control.Select Method"
http://msdn2.microsoft.com/en-us/lib...ea(VS.80).aspx

--
Dave Sexton

"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:O1**************@TK2MSFTNGP03.phx.gbl...
>Thansk, Kevin.

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

WJ

"Kevin Spencer" <sp**@uce.govwrote in message
news:eU**************@TK2MSFTNGP02.phx.gbl...
>>Sure, first set the Form's KeyPreview property to true:

http://msdn2.microsoft.com/en-us/lib...ew(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.
"Wanjun Yu" <wa*******@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
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





Nov 10 '06 #7
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).

--
Dave Sexton
Nov 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gabriel B. | last post by:
This is completely odd to me... # Bind F1...F12 to <<TabKeys>> self.tk.event_add( '<<TabKeys>>', # keys used to switch between the tabs...
2
by: Pablo | last post by:
I have a dilemma. Currently, I may be passing standard text (strings of char) or binary of 1 to 'x' bytes long to a program for comparison with data previously written to a file. The problem...
1
by: chandy | last post by:
Hi, How can I treat multiple, contiguous records as one record? i.e., tblData( a varchar, t datetime )
7
by: Steven T. Hatton | last post by:
"Reduced subclassing. Factory Method (107) often produces a hierarchy of Creator classes that parallels the product class hierarchy. The Prototype pattern lets you clone a prototype instead of...
11
by: Jeevan | last post by:
Hi, I have some data which I am getting from a socket. I am currently storing the data in an array (so that future reading of the data will be fast as it will be in RAM instead of hard disk)....
0
by: Larry Lard | last post by:
Recently we've been on a code quality drive and have eliminated all compilation warnings from our code. To preserve this state, we've turned 'Treat warnings as errors' to 'All' in all our projects...
5
by: helraizer1 | last post by:
Me again :D You're going to be sick of me soon. =P I found the GifEncoder class on phpclasses, which creates an animated gif image from frames and echos it to the browser to show; can also save...
4
by: Ken Snyder | last post by:
hi ng, reading a file line by line is no big match, but i would like to read them and convert them to string as if they where given like command line args so that i can preprocess them and hand...
5
by: Craig Buchanan | last post by:
I would like to monitor a POP3 mailbox with multiple clients. However, I want to ensure that each message is processed by only one client. In essence, I would like to treat a POP3 mailbox like a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.