Create a global variable in Delphi

7

I need to run the following code, but the form in question is not always UniForm1 . So I'm thinking of storing the name of form in a variable and replacing UniForm1 with it.

Instead of using:

UniForm1.Parent := UniPanel1;
UniForm1.Show;
UniForm1.SetFocus;

UniForm1.Width := UniPanel1.Width;
UniForm1.Height := UniPanel1.Height;

I need something like:

VariavelForm.Parent := UniPanel1;
VariavelForm.Show;
VariavelForm.SetFocus;

VariavelForm.Width := UniPanel1.Width;
VariavelForm.Height := UniPanel1.Height;

How to do this?

According to @Tmc's response, I made some changes.

But the error occurs:

  

[dcc32 Error] Main.pas (74): E2010 Incompatible types: 'TUniForm' and   'string'

procedure TMainForm.UniTreeView1Change(Sender: TObject; Node: TUniTreeNode);
var nome : string;
begin
   nome := Node.Text;

   Vforms := nome;

   Vforms.Parent := UniPanel1;
   Vforms.Show;
   Vforms.SetFocus;

   Vforms.Width := UniPanel1.Width;
   Vforms.Height := UniPanel1.Height;
end;
    
asked by anonymous 19.09.2017 / 14:23

1 answer

9

You can do this as follows:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure ChangeSetForms;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
    //declarar variavel global
    Vforms: TComponent;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text := 'Form1';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // atribui valor de form a variavel
  Vforms := Application.FindComponent(Edit1.Text);

  //chama a procedure
  ChangeSetForms;
end;

procedure TForm1.ChangeSetForms;
Begin
  //faz as alterãções no form escolhido
  TForm(VForms).Parent := UniPanel1;
  TForm(VForms).Show;
  TForm(VForms).SetFocus;

  TForm(VForms).Width := UniPanel1.Width;
  TForm(VForms).Height := UniPanel1.Height;
End;

end.

I tried to detail all the steps but any questions I am available.

Another hypothesis is not to create a variable by calling procedure and passing the variable by it, I also leave the example:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text := 'Form1';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //chama a procedure
  ChangeSetForms(Application.FindComponent(Edit1.Text));
end;

procedure TForm1.ChangeSetForms(VForm: TComponent);
Begin
  TForm(VForm).Width := 500;
End;
    
19.09.2017 / 15:01