| View previous topic :: View next topic |
| Author |
Message |
Analian Guest
|
Posted: Tue Mar 29, 2005 1:52 pm Post subject: Pressing keys and getting focus |
|
|
I've got a form with 20 Edit boxes in it and two buttons - OK and Cancel.
I want when the user presses Enter while in this form OR in any of its child
Edit boxes to set the Focus to the OK button and if the key's Escape, to set
the focus to the Cancel button. So I wrote an event handler for the form's
OnKeyDown event
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (Key == VK_RETURN)
Button1->SetFocus();
else if (Key == VK_ESCAPE)
Button2->SetFocus();
}
But this doesn't help me much cause the focus is on some of the edit boxes.
How can I solve this matter?
What comes to my mind is writing 20 handlers for each Edit box's OnKeyDown
event or stupid stuff like that.
What's the elegant exit of the situation? Thank you.
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Mar 29, 2005 2:06 pm Post subject: Re: Pressing keys and getting focus |
|
|
Analian wrote:
| Quote: | But this doesn't help me much cause the focus is on some of the edit
boxes. How can I solve this matter?
|
Set the form's KeyPreview property to true. That way the form gets to
see the keypress before any components.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
Analian Guest
|
Posted: Tue Mar 29, 2005 2:11 pm Post subject: Re: Pressing keys and getting focus |
|
|
Thank you. I've obiously overlooked this property.
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Mar 29, 2005 2:18 pm Post subject: Re: Pressing keys and getting focus |
|
|
Analian wrote:
| Quote: | Thank you. I've obiously overlooked this property.
|
Well I hope it provides the solution you need. I've used it in a couple
of projects and one of them turned out to be more involved. I forget
why but I remember that setting KeyPreview to true was only part of the
solution. Then again that form had a bitmap and needed to 'pretend'
that the bitmap had the focus.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
Analian Guest
|
Posted: Tue Mar 29, 2005 2:54 pm Post subject: Re: Pressing keys and getting focus |
|
|
Well, it works fine for me now for the purpose of catching "enter" and
"escape" buttons. But help says that KeyPreview doesn't affect positioning
keys. I also want the user to be able to move trough the Edit boxes. Using
Tab and setting TabOrder in the desired order works fine. But how am I
supposed to handle the arrow up and arrow down keypresses? I can't use
KeyPreview so what am I to do?
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Mar 29, 2005 3:06 pm Post subject: Re: Pressing keys and getting focus |
|
|
Analian wrote:
| Quote: | But how am I supposed to handle the arrow up and arrow down
keypresses?
|
Create a single event then have all the edit handlers share that. I
found the following code snippet on the web. It's Delphi but easily
translatable.
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key=VK_RETURN then
Form1.Perform(WM_NEXTDLGCTL, 0, 0);
end;
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
Damon Chandler (TeamB) Guest
|
Posted: Tue Mar 29, 2005 4:51 pm Post subject: Re: Pressing keys and getting focus |
|
|
Hi,
In addition to Andrue's suggestion, have a look at the following
article...
http://bcbjournal.org/articles/vol4/0007/Customized_form_navigation.htm
HTH,
--
Damon (TeamB)
C++Builder Developer's Journal
http://bcbjournal.com
BCB Commonly Asked Questions
http://bcbjournal.com/bcbcaq
Analian wrote:
| Quote: | Well, it works fine for me now for the purpose of catching "enter" and
"escape" buttons. But help says that KeyPreview doesn't affect positioning
keys. I also want the user to be able to move trough the Edit boxes. Using
Tab and setting TabOrder in the desired order works fine. But how am I
supposed to handle the arrow up and arrow down keypresses? I can't use
KeyPreview so what am I to do?
|
|
|
| Back to top |
|
 |
Analian Guest
|
Posted: Tue Mar 29, 2005 6:14 pm Post subject: Re: Pressing keys and getting focus |
|
|
Thank you, Andrue. I got it.
|
|
| Back to top |
|
 |
Analian Guest
|
Posted: Tue Mar 29, 2005 6:14 pm Post subject: Re: Pressing keys and getting focus |
|
|
Thank you, Damon. The article's great.
|
|
| Back to top |
|
 |
|