2015-04-18 61 views
1

我有一个保存按钮的窗体(模式)。请记住,此按钮在按下后没有关闭表格,并且在保存数据后变为禁用状态。德尔福:按下保存按钮后返回焦点

我想要做的是将焦点返回到保存按钮被按下后使用的最新对象组件(编辑,vst,组合框等)。

+0

http://stackoverflow.com/questions/15316831/how-to-remove-focus-rectangle-from-一键控制 –

回答

4

您可以使用一个专门的按钮,将保存以前集中控制,同时它获得焦点:

type 
    TSaveButton = class(TButton) 
    private 
    FPrevWinControl: TWinControl; 
    protected 
    procedure Click; override; 
    procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS; 
    end; 

procedure TSaveButton.WMSetFocus(var Message: TWMSetFocus); 
begin 
    FPrevWinControl := FindControl(Message.FocusedWnd); 
    inherited; 
end; 

procedure TSaveButton.Click; 
begin 
    inherited; 
    if Assigned(FPrevWinControl) then 
    FPrevWinControl.SetFocus; 
end; 

然而,这需要一些控制有多于一个窗口一些特殊的处理。例如。一个组合框,如果编辑窗口被聚焦,FindControl将不会找到组合框,您需要传递编辑的父项。


还可通过覆盖SetFocusedControl处理它在形式层面,例如:

type 
    TForm1 = class(TForm) 
    ... 
    procedure ButtonSaveClick(Sender: TObject); 
    private 
    FPrevWinControl: TWinControl; 
    public 
    function SetFocusedControl(Control: TWinControl): Boolean; override; 
    ... 

function TForm1.SetFocusedControl(Control: TWinControl): Boolean; 
begin 
    if Control = ButtonSave then 
    FPrevWinControl := ActiveControl; 
    Result := inherited SetFocusedControl(Control); 
end; 

procedure TForm1.ButtonSaveClick(Sender: TObject); 
begin 
    // save ... 
    if Assigned(FPrevWinControl) then 
    FPrevWinControl.SetFocus; 
end; 
+0

第二个变体看起来更好。谢谢你的回答Sertac Akyuz – REALSOFO