Recent

Author Topic: Unit containing its own form  (Read 1675 times)

torbente

  • Sr. Member
  • ****
  • Posts: 298
    • Noso Main Page
Unit containing its own form
« on: January 26, 2021, 09:42:22 am »
I coded a simple file explorer (image included)
Then i try to make an unit from it.

Code: Pascal  [Select][+][-]
  1. unit FormExplore;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, forms, controls, stdctrls,Graphics, buttons, grids, fileutil;
  9.  
  10. function OnlyName(conpath:string):string;
  11. procedure FGridFilesDblClick(Sender: TObject);
  12. Procedure LoadDirectory(directory:string);
  13. procedure FSBUpPathClick(Sender: TObject);
  14. Procedure ShowExplorer(Directory:string);
  15.  
  16. var
  17.   FExplorer : TForm;
  18.   EditPath : Tedit;
  19.   FSBUpPath : TSpeedButton;
  20.   FGridFiles : TStringGrid;
  21.   FEditFilename : Tedit;
  22.   FSBOkFile : TSpeedButton;
  23.  
  24.   FActiveDir : String = '';
  25.   FilesDir : TStringList;
  26.   FoldersDir : TStringList;
  27.   Result : String = '';
  28.  
  29. implementation
  30.  
  31. Procedure ShowExplorer(Directory:String);
  32. Begin
  33. LoadDirectory(Directory);
  34. FExplorer.Visible:=true;
  35. End;
  36.  
  37. function OnlyName(conpath:string):string;
  38. var
  39.   cont : integer;
  40. Begin
  41. for cont := length(conpath) downto 1 do
  42.    if conpath[cont] = '\' then
  43.       begin
  44.       result := copy(conpath,cont+1,length(conpath));
  45.       break;
  46.       end;
  47. end;
  48.  
  49. procedure FSBUpPathClick(Sender: TObject);
  50. var
  51.   contador : integer;
  52. begin
  53. for contador := length(FActiveDir) downto 1 do
  54.    begin
  55.    if FActiveDir[contador] = '\' then
  56.       begin
  57.       FActiveDir := copy(FActiveDir,1,contador-1);
  58.       LoadDirectory(FActiveDir);
  59.       break
  60.       end;
  61.    end;
  62. end;
  63.  
  64. procedure FGridFilesDblClick(Sender: TObject);
  65. begin
  66. if FGridFiles.Row>0 then
  67.    begin
  68.    if copy(FGridFiles.Cells[0,FGridFiles.Row],1,3) = '   ' then
  69.       begin
  70.       FActiveDir := FActiveDir+'\'+ copy(FGridFiles.Cells[0,FGridFiles.Row],4,length(FGridFiles.Cells[0,FGridFiles.Row]));
  71.       LoadDirectory(FActiveDir);
  72.       end;
  73.    end;
  74. end;
  75.  
  76. procedure FGridFilesSelection(Sender: TObject; aCol, aRow: Integer);
  77. begin
  78. FEditFilename.Text:=FGridFiles.Cells[0,FGridFiles.Row];
  79. if copy(FEditFilename.Text,1,3)='   ' then FEditFilename.Text:='';
  80. end;
  81.  
  82. Procedure LoadDirectory(directory:string);
  83. var
  84.   cont : integer;
  85. Begin
  86. FilesDir.Clear;
  87. FoldersDir.clear;
  88. FGridFiles.RowCount:=1;
  89. FindAllFiles(FilesDir, directory, '*.*', false);
  90. FoldersDir := FindAllDirectories(directory,false);
  91. if FoldersDir.Count>0 then
  92.    Begin
  93.    for cont := 0 to FoldersDir.Count-1 do
  94.       begin
  95.       FGridFiles.RowCount:=FGridFiles.RowCount+1;
  96.       FGridFiles.Cells[0,cont+1]:='   '+OnlyName(FoldersDir[cont]);
  97.       end;
  98.    end;
  99. if FilesDir.Count>0 then
  100.    begin
  101.    for cont := 0 to FilesDir.Count-1 do
  102.       begin
  103.       FGridFiles.RowCount:=FGridFiles.RowCount+1;
  104.       FGridFiles.Cells[0,FGridFiles.RowCount-1]:=OnlyName(FilesDir[cont]);
  105.       end;
  106.    end;
  107. EditPath.Text:=directory;
  108. End;
  109.  
  110. begin
  111. FExplorer := TForm.Create(nil);
  112. FExplorer.caption := 'File Explorer';
  113. FExplorer.SetBounds(0, 0, 450, 350);
  114. FExplorer.BorderStyle := bssingle;
  115. FExplorer.Position:=poOwnerFormCenter;
  116. FExplorer.BorderIcons:=FExplorer.BorderIcons-[biminimize];
  117.  
  118. EditPath := TEdit.Create(FExplorer);EditPath.Parent:=FExplorer;EditPath.AutoSize:=false;
  119. EditPath.Left:=1;EditPath.Top:=1;EditPath.Height:=24;EditPath.Width:=424;
  120. EditPath.Font.Name:='consolas'; EditPath.Font.Size:=12;
  121. EditPath.Color:=clBlack;EditPath.Font.color:=clwhite;
  122. EditPath.Alignment:=taLeftJustify;EditPath.Visible:=true;
  123. EditPath.Enabled:=true;EditPath.ReadOnly:=true;
  124.  
  125. FSBUpPath := TSpeedButton.Create(FExplorer);FSBUpPath.Parent:=FExplorer;
  126. FSBUpPath.Left:=426;FSBUpPath.Top:=2;FSBUpPath.Height:=22;FSBUpPath.Width:=22;
  127. FSBUpPath.Visible:=true;
  128. FSBUpPath.hint:='Go up';FSBUpPath.ShowHint:=true;
  129. //FSBUpPath.OnClick:=SBUpPathClick;
  130.  
  131. FGridFiles := TStringGrid.Create(nil);FGridFiles.Parent:=FExplorer;
  132. FGridFiles.Font.Name:='consolas'; FGridFiles.Font.Size:=12;
  133. FGridFiles.Left:=2;FGridFiles.Top:=24;FGridFiles.Height:=298;FGridFiles.width:=446;
  134. FGridFiles.FixedCols:=0;FGridFiles.FixedRows:=1;
  135. FGridFiles.rowcount := 1;FGridFiles.ColCount:=4;
  136. FGridFiles.ScrollBars:=ssAutoVertical;
  137. FGridFiles.Options:= FGridFiles.Options-[goRangeSelect];
  138. FGridFiles.ColWidths[0]:= 444;
  139. FGridFiles.Cells[0,0]:= 'Name';
  140. FGridFiles.FocusRectVisible:=false;
  141. //FGridFiles.OnDblClick:=FGridFilesDblClick;
  142. //FGridFiles.OnSelection:=FGridFilesSelection;
  143.  
  144. FEditFilename := TEdit.Create(FExplorer);FEditFilename.Parent:=FExplorer;FEditFilename.AutoSize:=false;
  145. FEditFilename.Left:=1;FEditFilename.Top:=324;FEditFilename.Height:=24;FEditFilename.Width:=384;
  146. FEditFilename.Font.Name:='consolas'; FEditFilename.Font.Size:=12;
  147. FEditFilename.Color:=clBlack;FEditFilename.Font.color:=clwhite;
  148. FEditFilename.Alignment:=taLeftJustify;FEditFilename.Visible:=true;
  149. FEditFilename.Enabled:=true;FEditFilename.ReadOnly:=true;
  150.  
  151. FSBOkFile := TSpeedButton.Create(FExplorer);FSBOkFile.Parent:=FExplorer;
  152. FSBOkFile.Left:=388;FSBOkFile.Top:=324;FSBOkFile.Height:=22;FSBOkFile.Width:=60;
  153. FSBOkFile.Visible:=true;
  154. FSBOkFile.hint:='Accept';FSBOkFile.ShowHint:=true;
  155. //FSBOkFile.OnClick:=@form1.FSBOkFileOnClick;
  156.  
  157. FilesDir := TStringList.Create;
  158. FoldersDir := TStringList.Create;
  159. END.  

But im missing something since im unable to assign the event functions to its controls.
(lines 129,141,142,155)

PS: The unit is still not finished, but im stuck here. It actually works but without control events, it is impossible to navigate.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

dseligo

  • Jr. Member
  • **
  • Posts: 81
Re: Unit containing its own form
« Reply #1 on: January 26, 2021, 10:23:01 am »
I think that event procedures should be part of class (method). So, try to create a class and put this procedures in that class.

howardpc

  • Hero Member
  • *****
  • Posts: 3644
Re: Unit containing its own form
« Reply #2 on: January 26, 2021, 11:05:32 am »
something like this:
Code: Pascal  [Select][+][-]
  1. unit FormExplore;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil,
  9.   Forms, Controls, StdCtrls, Graphics, Buttons, Grids;
  10.  
  11. type
  12.   TExplorerForm = class(TForm)
  13.   private
  14.     EditPath: TEdit;
  15.     FActiveDir: String;
  16.     FEditFilename: TEdit;
  17.     FGridFiles: TStringGrid;
  18.     FilesDir: TStringList;
  19.     FoldersDir: TStringList;
  20.     FSBOkFile: TSpeedButton;
  21.     FSBUpPath: TSpeedButton;
  22.  
  23.  
  24.     procedure FSBOkFileOnClick(Sender: TObject);
  25.     function OnlyName(const conpath: String): String;
  26.     procedure FGridFilesDblClick(Sender: TObject);
  27.     procedure FGridFilesSelection(Sender: TObject; aCol, aRow: Integer);
  28.     procedure SBUpPathClick(Sender: TObject);
  29.     procedure LoadDirectory(const Directory: String);
  30.   public
  31.     constructor Create(TheOwner: TComponent); override;
  32.     destructor Destroy; override;
  33.   end;
  34.  
  35.   procedure ShowExplorer(const Directory: String);
  36.  
  37. implementation
  38.  
  39. var
  40.   explorer: TExplorerForm = Nil;
  41.  
  42. procedure ShowExplorer(const Directory: String);
  43.  
  44. begin
  45.   if not Assigned(explorer) then
  46.     explorer := TExplorerForm.Create(Nil);
  47.   explorer.LoadDirectory(Directory);
  48.   explorer.Show;
  49. end;
  50.  
  51. { TExplorerForm }
  52.  
  53. constructor TExplorerForm.Create(TheOwner: TComponent);
  54. begin
  55.   inherited CreateNew(TheOwner);
  56.   Caption := 'File Explorer';
  57.   SetBounds(0, 0, 450, 350);
  58.   BorderStyle := bssingle;
  59.   Position := poOwnerFormCenter;
  60.   BorderIcons := BorderIcons - [biminimize];
  61.  
  62.   FilesDir := TStringList.Create;
  63.   FoldersDir := TStringList.Create;
  64.  
  65.   EditPath := TEdit.Create(Self);
  66.   with EditPath do
  67.     begin
  68.       SetBounds(1, 1, 424, 24);
  69.       Font.Name := 'consolas';
  70.       Font.Size := 12;
  71.       Font.Color := clWhite;
  72.       Color := clBlack;
  73.       Alignment := taLeftJustify;
  74.       ReadOnly := True;
  75.       Parent := Self;
  76.     end;
  77.  
  78.   FSBUpPath := TSpeedButton.Create(Self);
  79.   with FSBUpPath do
  80.     begin
  81.       SetBounds(426, 2, 22, 22);
  82.       Hint := 'Go up';
  83.       ShowHint := True;
  84.       OnClick := @SBUpPathClick;
  85.       Parent := Self;
  86.     end;
  87.  
  88.   FGridFiles := TStringGrid.Create(Self);
  89.   with FGridFiles do
  90.     begin
  91.       SetBounds(2, 24, 446, 298);
  92.       Font.Name := 'consolas';
  93.       Font.Size := 12;
  94.       FixedCols := 0;
  95.       FixedRows := 1;
  96.       RowCount := 1;
  97.       ColCount := 4;
  98.       ScrollBars := ssAutoVertical;
  99.       Options := Options - [goRangeSelect];
  100.       ColWidths[0] := 444;
  101.       Cells[0,0] := 'Name';
  102.       FocusRectVisible := False;
  103.       OnDblClick := @FGridFilesDblClick;
  104.       OnSelection := @FGridFilesSelection;
  105.       Parent := Self;
  106.     end;
  107.  
  108.   FEditFilename := TEdit.Create(Self);
  109.   with FEditFilename do
  110.     begin
  111.       SetBounds(1, 324, 384, 24);
  112.       AutoSize := False;
  113.       Font.Name := 'consolas';
  114.       Font.Size := 12;
  115.       Font.Color := clWhite;
  116.       Color := clBlack;
  117.       Alignment := taLeftJustify;
  118.       ReadOnly := True;
  119.       Parent := Self;
  120.     end;
  121.  
  122.   FSBOkFile := TSpeedButton.Create(Self);
  123.   with FSBOkFile do
  124.     begin
  125.       SetBounds(388, 324, 60, 22);
  126.       ShowHint := True;
  127.       Hint := 'Accept';
  128.       OnClick := @FSBOkFileOnClick;
  129.       Parent := Self;
  130.     end;
  131.  
  132. end;
  133.  
  134. destructor TExplorerForm.Destroy;
  135. begin
  136.   FilesDir.Free;
  137.   FoldersDir.Free;
  138.   inherited Destroy;
  139. end;
  140.  
  141. procedure TExplorerForm.FGridFilesDblClick(Sender: TObject);
  142. begin
  143.   if (FGridFiles.Row > 0) and (Copy(FGridFiles.Cells[0,FGridFiles.Row],1,3) = '   ' ) then
  144.     begin
  145.       FActiveDir := FActiveDir+'\'+ Copy(FGridFiles.Cells[0,FGridFiles.Row],4,Length(FGridFiles.Cells[0,FGridFiles.Row]));
  146.       LoadDirectory(FActiveDir);
  147.    end;
  148. end;
  149.  
  150. procedure TExplorerForm.FGridFilesSelection(Sender: TObject; aCol, aRow: Integer);
  151. begin
  152.   FEditFilename.Text := FGridFiles.Cells[0,FGridFiles.Row];
  153.   if Copy(FEditFilename.Text,1,3) = '   ' then
  154.     FEditFilename.Text := '';
  155. end;
  156.  
  157. procedure TExplorerForm.FSBOkFileOnClick(Sender: TObject);
  158. begin
  159.   // your code here
  160. end;
  161.  
  162. procedure TExplorerForm.LoadDirectory(const Directory: String);
  163. var
  164.   cont: Integer;
  165. begin
  166.   FilesDir.Clear;
  167.   FoldersDir.Clear;
  168.   FGridFiles.RowCount := 1;
  169.   FindAllFiles(FilesDir, Directory, '*.*', False);
  170.   FindAllDirectories(FoldersDir, Directory, False);
  171.   if FoldersDir.Count > 0 then
  172.     for cont := 0 to FoldersDir.Count-1 do
  173.       begin
  174.         FGridFiles.RowCount := FGridFiles.RowCount+1;
  175.         FGridFiles.Cells[0,cont+1] := '   ' + OnlyName(FoldersDir[cont]);
  176.       end;
  177.   if FilesDir.Count > 0 then
  178.     for cont := 0 to FilesDir.Count-1 do
  179.       begin
  180.         FGridFiles.RowCount := FGridFiles.RowCount+1;
  181.         FGridFiles.Cells[0,FGridFiles.RowCount-1] := OnlyName(FilesDir[cont]);
  182.       end;
  183.   EditPath.Text := Directory;
  184. end;
  185.  
  186. function TExplorerForm.OnlyName(const conpath: String): String;
  187. var
  188.   cont: Integer;
  189. begin
  190.   for cont := Length(conpath) downto 1 do
  191.    if conpath[cont] = '\' then
  192.      begin
  193.        Result := Copy(conpath, cont+1, Length(conpath));
  194.        Break;
  195.      end;
  196. end;
  197.  
  198. procedure TExplorerForm.SBUpPathClick(Sender: TObject);
  199. var
  200.   contador : integer;
  201. begin
  202. for contador := length(FActiveDir) downto 1 do
  203.    begin
  204.    if FActiveDir[contador] = '\' then
  205.       begin
  206.         FActiveDir := copy(FActiveDir,1,contador-1);
  207.         LoadDirectory(FActiveDir);
  208.         Break;
  209.       end;
  210.    end;
  211. end;
  212.  
  213. finalization
  214.  
  215.   explorer.Free;
  216.   explorer := Nil;
  217.  
  218. end.

torbente

  • Sr. Member
  • ****
  • Posts: 298
    • Noso Main Page
Re: Unit containing its own form
« Reply #3 on: January 26, 2021, 02:26:08 pm »
Your code works perfect! Thanks a lot. I did not have idea i need declare a new class for the form.

Actually, when ok button is pressed, it returns the full path showed in the upper Tedit+'\'+Name of the file specified.
The string is exactly the same i got from the windows file explorer.
But if i apply a Fileexists to this string, i get a false  %).
I guest it is something with the application dir, but it not supposed that with the complete path it should work?
I try changing the directory separator from '\' to '/' but it did not work  neither

something like this:
Code: Pascal  [Select][+][-]
  1. unit FormExplore;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil,
  9.   Forms, Controls, StdCtrls, Graphics, Buttons, Grids;
  10.  
  11. type
  12.   TExplorerForm = class(TForm)
  13.   private
  14.     EditPath: TEdit;
  15.     FActiveDir: String;
  16.     FEditFilename: TEdit;
  17.     FGridFiles: TStringGrid;
  18.     FilesDir: TStringList;
  19.     FoldersDir: TStringList;
  20.     FSBOkFile: TSpeedButton;
  21.     FSBUpPath: TSpeedButton;
  22.  
  23.  
  24.     procedure FSBOkFileOnClick(Sender: TObject);
  25.     function OnlyName(const conpath: String): String;
  26.     procedure FGridFilesDblClick(Sender: TObject);
  27.     procedure FGridFilesSelection(Sender: TObject; aCol, aRow: Integer);
  28.     procedure SBUpPathClick(Sender: TObject);
  29.     procedure LoadDirectory(const Directory: String);
  30.   public
  31.     constructor Create(TheOwner: TComponent); override;
  32.     destructor Destroy; override;
  33.   end;
  34.  
  35.   procedure ShowExplorer(const Directory: String);
  36.  
  37. implementation
  38.  
  39. var
  40.   explorer: TExplorerForm = Nil;
  41.  
  42. procedure ShowExplorer(const Directory: String);
  43.  
  44. begin
  45.   if not Assigned(explorer) then
  46.     explorer := TExplorerForm.Create(Nil);
  47.   explorer.LoadDirectory(Directory);
  48.   explorer.Show;
  49. end;
  50.  
  51. { TExplorerForm }
  52.  
  53. constructor TExplorerForm.Create(TheOwner: TComponent);
  54. begin
  55.   inherited CreateNew(TheOwner);
  56.   Caption := 'File Explorer';
  57.   SetBounds(0, 0, 450, 350);
  58.   BorderStyle := bssingle;
  59.   Position := poOwnerFormCenter;
  60.   BorderIcons := BorderIcons - [biminimize];
  61.  
  62.   FilesDir := TStringList.Create;
  63.   FoldersDir := TStringList.Create;
  64.  
  65.   EditPath := TEdit.Create(Self);
  66.   with EditPath do
  67.     begin
  68.       SetBounds(1, 1, 424, 24);
  69.       Font.Name := 'consolas';
  70.       Font.Size := 12;
  71.       Font.Color := clWhite;
  72.       Color := clBlack;
  73.       Alignment := taLeftJustify;
  74.       ReadOnly := True;
  75.       Parent := Self;
  76.     end;
  77.  
  78.   FSBUpPath := TSpeedButton.Create(Self);
  79.   with FSBUpPath do
  80.     begin
  81.       SetBounds(426, 2, 22, 22);
  82.       Hint := 'Go up';
  83.       ShowHint := True;
  84.       OnClick := @SBUpPathClick;
  85.       Parent := Self;
  86.     end;
  87.  
  88.   FGridFiles := TStringGrid.Create(Self);
  89.   with FGridFiles do
  90.     begin
  91.       SetBounds(2, 24, 446, 298);
  92.       Font.Name := 'consolas';
  93.       Font.Size := 12;
  94.       FixedCols := 0;
  95.       FixedRows := 1;
  96.       RowCount := 1;
  97.       ColCount := 4;
  98.       ScrollBars := ssAutoVertical;
  99.       Options := Options - [goRangeSelect];
  100.       ColWidths[0] := 444;
  101.       Cells[0,0] := 'Name';
  102.       FocusRectVisible := False;
  103.       OnDblClick := @FGridFilesDblClick;
  104.       OnSelection := @FGridFilesSelection;
  105.       Parent := Self;
  106.     end;
  107.  
  108.   FEditFilename := TEdit.Create(Self);
  109.   with FEditFilename do
  110.     begin
  111.       SetBounds(1, 324, 384, 24);
  112.       AutoSize := False;
  113.       Font.Name := 'consolas';
  114.       Font.Size := 12;
  115.       Font.Color := clWhite;
  116.       Color := clBlack;
  117.       Alignment := taLeftJustify;
  118.       ReadOnly := True;
  119.       Parent := Self;
  120.     end;
  121.  
  122.   FSBOkFile := TSpeedButton.Create(Self);
  123.   with FSBOkFile do
  124.     begin
  125.       SetBounds(388, 324, 60, 22);
  126.       ShowHint := True;
  127.       Hint := 'Accept';
  128.       OnClick := @FSBOkFileOnClick;
  129.       Parent := Self;
  130.     end;
  131.  
  132. end;
  133.  
  134. destructor TExplorerForm.Destroy;
  135. begin
  136.   FilesDir.Free;
  137.   FoldersDir.Free;
  138.   inherited Destroy;
  139. end;
  140.  
  141. procedure TExplorerForm.FGridFilesDblClick(Sender: TObject);
  142. begin
  143.   if (FGridFiles.Row > 0) and (Copy(FGridFiles.Cells[0,FGridFiles.Row],1,3) = '   ' ) then
  144.     begin
  145.       FActiveDir := FActiveDir+'\'+ Copy(FGridFiles.Cells[0,FGridFiles.Row],4,Length(FGridFiles.Cells[0,FGridFiles.Row]));
  146.       LoadDirectory(FActiveDir);
  147.    end;
  148. end;
  149.  
  150. procedure TExplorerForm.FGridFilesSelection(Sender: TObject; aCol, aRow: Integer);
  151. begin
  152.   FEditFilename.Text := FGridFiles.Cells[0,FGridFiles.Row];
  153.   if Copy(FEditFilename.Text,1,3) = '   ' then
  154.     FEditFilename.Text := '';
  155. end;
  156.  
  157. procedure TExplorerForm.FSBOkFileOnClick(Sender: TObject);
  158. begin
  159.   // your code here
  160. end;
  161.  
  162. procedure TExplorerForm.LoadDirectory(const Directory: String);
  163. var
  164.   cont: Integer;
  165. begin
  166.   FilesDir.Clear;
  167.   FoldersDir.Clear;
  168.   FGridFiles.RowCount := 1;
  169.   FindAllFiles(FilesDir, Directory, '*.*', False);
  170.   FindAllDirectories(FoldersDir, Directory, False);
  171.   if FoldersDir.Count > 0 then
  172.     for cont := 0 to FoldersDir.Count-1 do
  173.       begin
  174.         FGridFiles.RowCount := FGridFiles.RowCount+1;
  175.         FGridFiles.Cells[0,cont+1] := '   ' + OnlyName(FoldersDir[cont]);
  176.       end;
  177.   if FilesDir.Count > 0 then
  178.     for cont := 0 to FilesDir.Count-1 do
  179.       begin
  180.         FGridFiles.RowCount := FGridFiles.RowCount+1;
  181.         FGridFiles.Cells[0,FGridFiles.RowCount-1] := OnlyName(FilesDir[cont]);
  182.       end;
  183.   EditPath.Text := Directory;
  184. end;
  185.  
  186. function TExplorerForm.OnlyName(const conpath: String): String;
  187. var
  188.   cont: Integer;
  189. begin
  190.   for cont := Length(conpath) downto 1 do
  191.    if conpath[cont] = '\' then
  192.      begin
  193.        Result := Copy(conpath, cont+1, Length(conpath));
  194.        Break;
  195.      end;
  196. end;
  197.  
  198. procedure TExplorerForm.SBUpPathClick(Sender: TObject);
  199. var
  200.   contador : integer;
  201. begin
  202. for contador := length(FActiveDir) downto 1 do
  203.    begin
  204.    if FActiveDir[contador] = '\' then
  205.       begin
  206.         FActiveDir := copy(FActiveDir,1,contador-1);
  207.         LoadDirectory(FActiveDir);
  208.         Break;
  209.       end;
  210.    end;
  211. end;
  212.  
  213. finalization
  214.  
  215.   explorer.Free;
  216.   explorer := Nil;
  217.  
  218. end.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

lucamar

  • Hero Member
  • *****
  • Posts: 3601
Re: Unit containing its own form
« Reply #4 on: January 26, 2021, 03:48:32 pm »
FileExist() should return True for an existing full-qualified file name ... except when the name is that of a directory; in that case it will return False and you'll have to use DirectoryExist().
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

torbente

  • Sr. Member
  • ****
  • Posts: 298
    • Noso Main Page
Re: Unit containing its own form
« Reply #5 on: January 26, 2021, 05:14:23 pm »
Fixed!

@Howadpc: how is the best way to handle the explorer form wjen the user finish with it? close? Destroy? visible :=false?

FileExist() should return True for an existing full-qualified file name ... except when the name is that of a directory; in that case it will return False and you'll have to use DirectoryExist().
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

howardpc

  • Hero Member
  • *****
  • Posts: 3644
Re: Unit containing its own form
« Reply #6 on: January 26, 2021, 05:27:27 pm »
All classes must be freed after use to avoid memory leaks.
There are various ways to do this. The code I gave you relied on a unit finalization routine.

Alternatively you could make the Application the form's Owner (rather than having a Nil Owner), or you could explicitly Create and Free a form instance in your main form's code somewhere if you only need the form for a limited period in the lifetime of your app. It's up to you.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6902
  • Debugger - SynEdit - and more
    • wiki
Re: Unit containing its own form
« Reply #7 on: January 26, 2021, 06:33:44 pm »
Thanks a lot. I did not have idea i need declare a new class for the form.

Note, you do not need to inherit TForm.

The only thing that you need is a class. And you need to instantiate it.
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyClass = class
  3.     procedure FSBOkFileOnClick(Sender: TObject);
  4.   end;
  5.  
  6. ...
  7.   FSBOkFile := TSpeedButton.Create(Self);
  8.   with FSBOkFile do
  9.     begin
  10. ...
  11.       OnClick := @FSBOkFileOnClick;
  12.  

Thus the event is a method.

Of course it makes sense to make your global vars, fields in that class. Just to keep some order (though order is a subjective concept).
If you don't inherit TForm, then you could have a field "FMyForm: TForm".

 

TinyPortal © 2005-2018