Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-05

How do I change properties for GUI-components from another unit?

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Anders Eriksson

How do I change properties for GUI-components from another unit?

e.g. hide a button or change a label text from a different unit.

/anders

-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2015.0.5941 / Virus Database: 4342/9751 - Release Date: 05/11/15

Reply by email

Howard Page-Clark
to Anders Eriksson
Anders Eriksson wrote
How do I change properties for GUI-components from another unit? e.g. hide a button or change a label text from a different unit.

Two example units that inter-communicate might be as follows:

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
   Forms, StdCtrls;
type

{ TForm1 }

   TForm1 = class(TForm)
     Button1: TButton;
     Label1: TLabel;
     procedure FormShow(Sender: TObject);
   end;
var
   Form1: TForm1;
implementation
uses unit2;
{$R *.lfm}

{ TForm1 }

procedure TForm1.FormShow(Sender: TObject);
begin
   Form2.Show;
end;
end.

================================================

unit Unit2;
{$mode objfpc}{$H+}
interface
uses
   Forms, StdCtrls;
type
   TForm2 = class(TForm)
     BChange: TButton;
     procedure BChangeClick(Sender: TObject);
   end;
var
   Form2: TForm2;
implementation
uses Unit1;
{$R *.lfm}
procedure TForm2.BChangeClick(Sender: TObject);
begin
   Form1.Button1.Hide;
   Form1.Label1.Caption:='Changed';
end;
end.

==================================================

The uses clauses are the critical feature.

Howard

---
This email has been checked for viruses by Avast antivirus software. http://www.avast.com

Reply by email

Anders Eriksson
to Howard Page-Clark

Thank you very much for your answer.

I will try it out tomorrow.
I have struggled with this for weeks.

/anders

Howard Page-Clark skrev den 2015-05-12 00:43:

Show quoted text 110 lines
On 11/05/2015 22:41, Anders Eriksson wrote: > How do I change properties for GUI-components from > another unit? > > e.g. hide a button or change a label text from a > different unit. Two example units that inter-communicate might be as follows: unit Unit1; {$mode objfpc}{$H+} interface uses Forms, StdCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Label1: TLabel; procedure FormShow(Sender: TObject); end; var Form1: TForm1; implementation uses unit2; {$R *.lfm} { TForm1 } procedure TForm1.FormShow(Sender: TObject); begin Form2.Show; end; end. ================================================ unit Unit2; {$mode objfpc}{$H+} interface uses Forms, StdCtrls; type TForm2 = class(TForm) BChange: TButton; procedure BChangeClick(Sender: TObject); end; var Form2: TForm2; implementation uses Unit1; {$R *.lfm} procedure TForm2.BChangeClick(Sender: TObject); begin Form1.Button1.Hide; Form1.Label1.Caption:='Changed'; end; end. ================================================== The uses clauses are the critical feature. Howard --- This email has been checked for viruses by Avast antivirus software. http://www.avast.com -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus ----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5941 / Virus Database: 4342/9751 - Release Date: 05/11/15

-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2015.0.5941 / Virus Database: 4342/9751 - Release Date: 05/11/15

Reply by email

Giuliano Colla
to Anders Eriksson: How do I change properties for GUI-components from another unit? e.g. hide a button or…

Il 11/05/2015 23:41, Anders Eriksson ha scritto:

How do I change properties for GUI-components from another unit? e.g. hide a button or change a label text from a different unit.

If you have in unit1 a Form1 with a Button1 component, and Form1 is declared in the Interface section of unit1 (i.e. it's made "public"), in unit 2 you must have a clause "uses unit1" and then you can simply code Form1.Button1.Hide
or
Form1.Button1.Caption := 'blah';

Only in case of ambiguity you must prepend also the unit name. e.g. unit2 and unit3 both have a Panel1 component and they both have it declared in the Interface section. Then in unit1 you will have "uses unit2,unit3.
And to operate on them you will code:
unit2.Panel1.hide;
unit3.Panel1.show;

If there's no ambiguity, it's unnecessary.

Giuliano

Giuliano Colla Project planning question: when it's 90% done, are we halfway or not yet?

Reply by email