Create a global variable in Delphi

Asked

Viewed 1,962 times

7

I need to execute the following code, but the form in question is not always Uniform1. So I’m thinking of storing the name of form into a variable and replace the Uniform1 for her sake.

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?

As per @Tmc’s reply, 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;

1 answer

8


You can do it this way:

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 some doubt I’m available.

Another hypothesis is not to create a variable by calling the procedure and passing the variable through 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.