Recent

Author Topic: Extra Unit procedures can't update component values?  (Read 3253 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
Extra Unit procedures can't update component values?
« on: August 03, 2011, 12:26:00 pm »
Dear forum people,

I have sorted out (with this forums's help) how to add an extra Unit to the project. I like that.

However, due to if it were possible would be great, is there a mechanism by which a procedure in the extra Unit can update directly values (properties) of components?

Unit1 defines the Form1 in the INTERFACE section and I thought so it would be PUBLIC, for all other units to see it.

I already am ahead in changing parts on my project to set code in the extra Unit, but, of course, have to use variables, and not the component properties directly.

So after much of that effort, I decided to ask this, just in case there is some special mechanism, setting, options, or tricks to do it.

(I mean if it is not OVERLY compex, or requires resources outside of Lazarus).

Thanks!
« Last Edit: August 03, 2011, 12:32:57 pm by Elmug »

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Extra Unit procedures can't update component values?
« Reply #1 on: August 03, 2011, 12:58:07 pm »
Assuming that Form1 resides in unit1, then simply add unit1 to the uses clause of unit2.
If unit1 uses unit2 (most likely scenario in your case), then a situation of "circular references" can occur.

For this reason, in Unit2 you add Unit1 to the usesclause in the Implementation section of Unit2 and not in the Interface section.

A simple form with a single button. If you click the button a procedure from another unit (Unit2) is called:

Code: [Select]
unit unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, StdCtrls,
  //here is the reference to Unit2, so we can access it's procedures/functions etc.
  Unit2;

type
  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  DoSomethingFromUnit2;
end;

end.

A simple Unit2 that access a property of the Form in Unit1.

Code: [Select]
unit Unit2;

{$mode objfpc}{$H+}

interface

{
//If you do thie you get a circular reference error on compile
uses
  Unit1;
}
Interface

//Make this procedure visible to other units / programs that use this unit
procedure   DoSomethingFromUnit2;

Implementation

//instead put the reference to Unit1 here:
uses
  Unit1;

procedure   DoSomethingFromUnit2;
begin
  //Move Form1 to TopLeft of screen
  //Form1 is declared in Unit1
  Form1.Top := 0;
  From1.Left := 0;
end;

Bart

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Extra Unit procedures can't update component values?
« Reply #2 on: August 03, 2011, 04:41:56 pm »
Great reply, and very happy to hear that, Bart, which makes a LOT of sense to me.

I will do it like that, even if it means trashing a whole day of work, but it is worth knowing that. And it is much what I expected should be possible, but the devil was laughing at  me on the details. >:D

 

TinyPortal © 2005-2018