Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to shift focus between buttons programmatically Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
26-Aug-02
Category
VCL-General
Language
Delphi All Versions
Views
104
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I have 4 buttons on a form, let's say button1 and button2 on the left side on the 
form and button3 and button4 on the right side on the form. When Button1 has the 
focus and I press the RightArrow I want to have Button3 to get the focus instead of 
Button2. When Button3 has the focus and I press the LeftArrow I want to have 
Button1 to get the focus. I've tried the onkeydown event on the button but it 
ignores to trap the arrowkeys.

Answer:

Buttons do not process navigation keys, so they go to the form in the guise of 
CM_DIALOGKEY messages and the form processes them to move to the next/ previous 
control in tab order. You may be able to achieve what you want by simply changing 
the tab order of your buttons (assuming they are all sitting on the same parent 
control).

To handle this all yourself you would add a handler for the CM_DIALOGKEY message to 
the form.

1   {form private section}
2   
3   procedure CMDialogKey(var msg: TCMDialogKey); message CM_DIALOGKEY;
4   
5     procedure TForm1.CMDialogKey(var msg: TCMDialogKey);
6     begin
7       case Msg.Charcode of
8         VK_RIGHT:
9           begin
10            if ActiveControl = Button1 then
11            begin
12              Button3.Setfocus;
13              msg.result := 1; {mark key handled}
14              Exit;
15            end;
16            if ActiveControl = Button2 then
17            begin
18              Button4.Setfocus;
19              msg.result := 1; {mark key handled}
20              Exit;
21            end;
22          end;
23        VK_LEFT:
24          begin
25            if ActiveControl = Button3 then
26            begin
27              Button1.Setfocus;
28              msg.result := 1;
29              Exit;
30            end;
31            if ActiveControl = Button4 then
32            begin
33              Button2.Setfocus;
34              msg.result := 1;
35              Exit;
36            end;
37          end;
38      end;
39      inherited;
40    end;
41  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC