Lazarus: How to Focus on TPanel lazarus / 31-LCL ---------------------------------------------------------------------- staratel20 · 2016-03-11 19:04 Hi. After read some forums create that simple code, that not work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  b :boolean;begin  Panel1.TabStop:=True;  b:=Panel1.CanFocus;  // b=true in debugger  Panel1.SetFocus;        // here raising errorend; Is this behaviour correct? ---------------------------------------------------------------------- howardpc · 2016-03-11 22:54 You can set a panel's TabStop property in its containing form's OnCreate handler, but OnCreate is is called too early for focusing a control - the form itself is not active yet, so its controls certainly cannot be active. The form's OnActivate (or OnShow) event are the earliest available moments for setting focus for a contained control. There's no point calling CanFocus - setting the focus already checks this for you. ---------------------------------------------------------------------- FTurtle · 2016-03-12 03:02 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin  ActiveControl := Panel1;end; ---------------------------------------------------------------------- staratel20 · 2016-03-12 10:47 howardpc: of cause! My fault - create bad isolated example. But when meet the problem, I was thinking that this code will not work too: procedure TForm1.FormCreate(Sender: TObject); begin   Panel1.TabStop:=true; end; procedure TForm1.Button1Click(Sender: TObject); begin   Panel1.SetFocus; end;  but it works. Thanks for all answers. FTurtle: your code was helpful. ---------------------------------------------------------------------- FTurtle · 2016-03-12 11:19 Quote from: staratel20 on March 12, 2016, 11:47:29 amprocedure TForm1.Button1Click(Sender: TObject); begin   Panel1.SetFocus; end;  By the way, when experimenting with focus, instead of TButton is better to use TSpeedButton because it does not take focus and therefore does not change the situation. ---------------------------------------------------------------------- staratel20 · 2016-03-12 11:39 In form where I need this - all(thanks God) on TSpeedButtons))) ---------------------------------------------------------------------- Windsurfer · 2016-03-12 16:53 Thanks for the TSpeedButton tip. ---------------------------------------------------------------------- SymbolicFrank · 2016-03-17 12:52 The OnActivate and OnShow events are useful, but they can be fired multiple times. For a workaround you can use a TTimer: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMainForm.StartTimerTimer(Sender: TObject);begin  if not Visible then Exit;  StartTimer.Enabled := False;  // now do the final initializationend; ---------------------------------------------------------------------- staratel20 · 2016-03-18 19:40 My problem totally solved. But I'm feel like it's wrong - that CanFocus return True when actually can't focus on control. If Panel1<>nil - CanFocus by default must return False. And just created form must somehow notify childs , that they can take focus. Maybe it's difficult or I'm wrong, but think it's more correct.