Reputation: 155
I have some forms that help me search for a product or a customer. When i open these forms i want the TEdit control that i type into for searching to be focused. I've been using a Timer for that but i've been searching for a more legit way to do this as this causes errors sometimes if the control is told to be focused when the form is not visible yet.
I've tried to use a windows message AfterShow that is called on the end of OnShow event of my Form. It doesn't work as the other simpler solutions of ActiveControl or SetFocus. The window message code is this.
const WM_AFTER_SHOW = WM_USER + 444;
private
procedure WmAfterShow(var Msg: TMessage); message WM_AFTER_SHOW;
procedure Tproducts_edit_form.WmAfterShow(var Msg: TMessage);
begin
self.ActiveControl:= search_txt;
//showmessage(Screen.ActiveControl.Name);
//PostMessage(search_txt.Handle, WM_SETFOCUS, 0, 0);
end;
Strange thing is that if uncomment both the showmessage and the postmessage, the TEdit gets the focus correctly. If i don't, the form opens but the TEdit is not focused even if the Screen.ActiveControl.Name tells me that the control i want has the focus.
Any ideas?
Upvotes: 0
Views: 3526
Reputation: 408
It is correct to use Form.ActiveControl (not Screen.ActiveControl) property to set focus on control, but use it in OnShow, not in OnCreate etc..:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
ActiveControl = Edit1;
}
If it doesn't work, maybe because of manual interfering with window message handler, message queue.
Upvotes: 2
Reputation: 155
The windowsmessage technique is working and the TabOrder of the control to be focused indeed has to be zero.
The problem i had was lying in the DevExpress Bar that my control is docked. The way these bars work makes it impossible to focus a non-DevExpress control that is docked into a DevExpress Bar.
Upvotes: -1