Recent

Author Topic: [SOLVED] TTDINotebook: Form's ActiveControl is not assigned  (Read 1122 times)

bpranoto

  • Full Member
  • ***
  • Posts: 183
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:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, Unit1, Unit2
  11.   { you can add units after this };
  12.  
  13. {$R *.res}
  14.  
  15. begin
  16.   RequireDerivedFormResource:=True;
  17.   Application.Scaled:=True;
  18.   Application.Initialize;
  19.   Application.CreateForm(TForm1, Form1);
  20.   Application.Run;
  21. end.
  22.  

unit1.pas
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, TDIClass;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     MainMenu1: TMainMenu;
  16.     MenuItem1: TMenuItem;
  17.     MenuItem2: TMenuItem;
  18.     TDINoteBook1: TTDINoteBook;
  19.     procedure MenuItem1Click(Sender: TObject);
  20.     procedure MenuItem2Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. uses Unit2;
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.MenuItem1Click(Sender: TObject);
  39. begin
  40.   Self.TDINoteBook1.CreateFormInNewPage(TForm2);
  41. end;
  42.  
  43. procedure TForm1.MenuItem2Click(Sender: TObject);
  44. begin
  45.   TForm2.Create(Self).Show;
  46. end;
  47.  
  48. end.
  49.  

unit1.lfm
Code: Pascal  [Select][+][-]
  1. object Form1: TForm1
  2.   Left = 463
  3.   Height = 308
  4.   Top = 233
  5.   Width = 482
  6.   Caption = 'Form1'
  7.   ClientHeight = 286
  8.   ClientWidth = 482
  9.   Menu = MainMenu1
  10.   LCLVersion = '2.0.2.0'
  11.   object TDINoteBook1: TTDINoteBook
  12.     Left = 0
  13.     Height = 286
  14.     Top = 0
  15.     Width = 482
  16.     Align = alClient
  17.     TabOrder = 0
  18.     MainMenu = MainMenu1
  19.     TDIActions.TabsMenu.Caption = 'Tabs'
  20.     TDIActions.TabsMenu.ImageIndex = -1
  21.     TDIActions.TabsMenu.Visible = True
  22.     TDIActions.CloseTab.Caption = 'Close Tab'
  23.     TDIActions.CloseTab.ImageIndex = -1
  24.     TDIActions.CloseTab.Visible = True
  25.     TDIActions.CloseAllTabs.Caption = 'Close All Tabs'
  26.     TDIActions.CloseAllTabs.ImageIndex = -1
  27.     TDIActions.CloseAllTabs.Visible = True
  28.     TDIActions.NextTab.Caption = 'Next Tab'
  29.     TDIActions.NextTab.ImageIndex = -1
  30.     TDIActions.NextTab.Visible = False
  31.     TDIActions.PreviousTab.Caption = 'Previous Tab'
  32.     TDIActions.PreviousTab.ImageIndex = -1
  33.     TDIActions.PreviousTab.Visible = False
  34.   end
  35.   object MainMenu1: TMainMenu
  36.     left = 239
  37.     top = 112
  38.     object MenuItem1: TMenuItem
  39.       Caption = 'Create Form2 in Page'
  40.       OnClick = MenuItem1Click
  41.     end
  42.     object MenuItem2: TMenuItem
  43.       Caption = 'Create floating Form2 '
  44.       OnClick = MenuItem2Click
  45.     end
  46.   end
  47. end
  48.  

unit2.pas
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm2 }
  13.  
  14.   TForm2 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     Label1: TLabel;
  19.     procedure Button1Exit(Sender: TObject);
  20.     procedure Edit1Exit(Sender: TObject);
  21.     procedure Edit2Exit(Sender: TObject);
  22.     procedure FormShow(Sender: TObject);
  23.   private
  24.     procedure ShowActiveControl;
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form2: TForm2;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm2 }
  37.  
  38. procedure TForm2.Button1Exit(Sender: TObject);
  39. begin
  40.   ShowActiveControl;
  41. end;
  42.  
  43. procedure TForm2.Edit1Exit(Sender: TObject);
  44. begin
  45.   ShowActiveControl;
  46. end;
  47.  
  48. procedure TForm2.Edit2Exit(Sender: TObject);
  49. begin
  50.   ShowActiveControl;
  51. end;
  52.  
  53. procedure TForm2.FormShow(Sender: TObject);
  54. begin
  55.   Self.Edit1.SetFocus;
  56.   ShowActiveControl;
  57. end;
  58.  
  59. procedure TForm2.ShowActiveControl;
  60. begin
  61.   if Assigned( Self.ActiveControl) then begin
  62.     Self.Label1.Caption:='Active Control is ' + Self.ActiveControl.Name;
  63.   end
  64.   else begin
  65.     Self.Label1.Caption:='Active Control is NIL';
  66.   end;
  67. end;
  68.  
  69. end.
  70.  
  71.  

unit2.lfm
Code: Pascal  [Select][+][-]
  1. object Form2: TForm2
  2.   Left = 669
  3.   Height = 269
  4.   Top = 289
  5.   Width = 379
  6.   Caption = 'Form2'
  7.   ClientHeight = 269
  8.   ClientWidth = 379
  9.   OnShow = FormShow
  10.   LCLVersion = '2.0.2.0'
  11.   object Edit1: TEdit
  12.     Left = 32
  13.     Height = 25
  14.     Top = 40
  15.     Width = 80
  16.     OnExit = Edit1Exit
  17.     TabOrder = 0
  18.     Text = 'Edit1'
  19.   end
  20.   object Edit2: TEdit
  21.     Left = 32
  22.     Height = 25
  23.     Top = 72
  24.     Width = 80
  25.     OnExit = Edit2Exit
  26.     TabOrder = 1
  27.     Text = 'Edit2'
  28.   end
  29.   object Button1: TButton
  30.     Left = 32
  31.     Height = 25
  32.     Top = 104
  33.     Width = 75
  34.     Caption = 'Button1'
  35.     OnExit = Button1Exit
  36.     TabOrder = 2
  37.   end
  38.   object Label1: TLabel
  39.     Left = 32
  40.     Height = 17
  41.     Top = 0
  42.     Width = 97
  43.     Caption = 'Active Control'
  44.     ParentColor = False
  45.   end
  46. end
  47.  
« Last Edit: June 30, 2019, 06:09:09 pm by bpranoto »

bpranoto

  • Full Member
  • ***
  • Posts: 183
Re: [SOLVED] TTDINotebook: Form's ActiveControl is not assigned
« Reply #1 on: June 30, 2019, 06:10:00 pm »
instead of Form's ActiveControl you should use screen.ActiveControl

 

TinyPortal © 2005-2018