2004. március 28., vasárnap
How to shift focus between buttons programmatically
Problem/Question/Abstract:
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.
{form private section}
procedure CMDialogKey(var msg: TCMDialogKey); message CM_DIALOGKEY;
procedure TForm1.CMDialogKey(var msg: TCMDialogKey);
begin
case Msg.Charcode of
VK_RIGHT:
begin
if ActiveControl = Button1 then
begin
Button3.Setfocus;
msg.result := 1; {mark key handled}
Exit;
end;
if ActiveControl = Button2 then
begin
Button4.Setfocus;
msg.result := 1; {mark key handled}
Exit;
end;
end;
VK_LEFT:
begin
if ActiveControl = Button3 then
begin
Button1.Setfocus;
msg.result := 1;
Exit;
end;
if ActiveControl = Button4 then
begin
Button2.Setfocus;
msg.result := 1;
Exit;
end;
end;
end;
inherited;
end;
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése