When a form is shown on TTDINotebook tabsheet, ActiveControl property of the form is not assigned.
This create a difficulty when detecting the next active control when changing focus from one control to another control.
Attached is a small demo:
When form2 is shown in a tab by clicking menu item "Create Form2 in Page" ActiveControl property is always NIL
When form2 is shown normally by clicking menu item "Create floating form2", ActiveControl property is correctly detected.
project1.lpr:
program project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, Unit1, Unit2
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource:=True;
Application.Scaled:=True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
unit1.pas
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, TDIClass;
type
{ TForm1 }
TForm1 = class(TForm)
MainMenu1: TMainMenu;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
TDINoteBook1: TTDINoteBook;
procedure MenuItem1Click(Sender: TObject);
procedure MenuItem2Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.lfm}
{ TForm1 }
procedure TForm1.MenuItem1Click(Sender: TObject);
begin
Self.TDINoteBook1.CreateFormInNewPage(TForm2);
end;
procedure TForm1.MenuItem2Click(Sender: TObject);
begin
TForm2.Create(Self).Show;
end;
end.
unit1.lfm
object Form1: TForm1
Left = 463
Height = 308
Top = 233
Width = 482
Caption = 'Form1'
ClientHeight = 286
ClientWidth = 482
Menu = MainMenu1
LCLVersion = '2.0.2.0'
object TDINoteBook1: TTDINoteBook
Left = 0
Height = 286
Top = 0
Width = 482
Align = alClient
TabOrder = 0
MainMenu = MainMenu1
TDIActions.TabsMenu.Caption = 'Tabs'
TDIActions.TabsMenu.ImageIndex = -1
TDIActions.TabsMenu.Visible = True
TDIActions.CloseTab.Caption = 'Close Tab'
TDIActions.CloseTab.ImageIndex = -1
TDIActions.CloseTab.Visible = True
TDIActions.CloseAllTabs.Caption = 'Close All Tabs'
TDIActions.CloseAllTabs.ImageIndex = -1
TDIActions.CloseAllTabs.Visible = True
TDIActions.NextTab.Caption = 'Next Tab'
TDIActions.NextTab.ImageIndex = -1
TDIActions.NextTab.Visible = False
TDIActions.PreviousTab.Caption = 'Previous Tab'
TDIActions.PreviousTab.ImageIndex = -1
TDIActions.PreviousTab.Visible = False
end
object MainMenu1: TMainMenu
left = 239
top = 112
object MenuItem1: TMenuItem
Caption = 'Create Form2 in Page'
OnClick = MenuItem1Click
end
object MenuItem2: TMenuItem
Caption = 'Create floating Form2 '
OnClick = MenuItem2Click
end
end
end
unit2.pas
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm2 }
TForm2 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
procedure Button1Exit(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
procedure Edit2Exit(Sender: TObject);
procedure FormShow(Sender: TObject);
private
procedure ShowActiveControl;
public
end;
var
Form2: TForm2;
implementation
{$R *.lfm}
{ TForm2 }
procedure TForm2.Button1Exit(Sender: TObject);
begin
ShowActiveControl;
end;
procedure TForm2.Edit1Exit(Sender: TObject);
begin
ShowActiveControl;
end;
procedure TForm2.Edit2Exit(Sender: TObject);
begin
ShowActiveControl;
end;
procedure TForm2.FormShow(Sender: TObject);
begin
Self.Edit1.SetFocus;
ShowActiveControl;
end;
procedure TForm2.ShowActiveControl;
begin
if Assigned( Self.ActiveControl) then begin
Self.Label1.Caption:='Active Control is ' + Self.ActiveControl.Name;
end
else begin
Self.Label1.Caption:='Active Control is NIL';
end;
end;
end.
unit2.lfm
object Form2: TForm2
Left = 669
Height = 269
Top = 289
Width = 379
Caption = 'Form2'
ClientHeight = 269
ClientWidth = 379
OnShow = FormShow
LCLVersion = '2.0.2.0'
object Edit1: TEdit
Left = 32
Height = 25
Top = 40
Width = 80
OnExit = Edit1Exit
TabOrder = 0
Text = 'Edit1'
end
object Edit2: TEdit
Left = 32
Height = 25
Top = 72
Width = 80
OnExit = Edit2Exit
TabOrder = 1
Text = 'Edit2'
end
object Button1: TButton
Left = 32
Height = 25
Top = 104
Width = 75
Caption = 'Button1'
OnExit = Button1Exit
TabOrder = 2
end
object Label1: TLabel
Left = 32
Height = 17
Top = 0
Width = 97
Caption = 'Active Control'
ParentColor = False
end
end