Recent

Author Topic: Newbie query-how to reference control on another form  (Read 315 times)

DavidPowell

  • Newbie
  • Posts: 3
Newbie query-how to reference control on another form
« on: June 24, 2019, 04:09:08 pm »
Hi,
I am new to Lazarus, and been a while since I used Delphi.

I have looked for quite a while for an answer to my query, but I think it is so basic, no one actually mentions how to do it!

I have form2, with a control on it called edit2.  I want to set the value of edit2 to   a control on another form (edit1 on form1).

In the properties dialog of form2.edit2 I have set the text value to be every possible combination I can think of  (eg  form1.edit1, form1.edit1.text, form1.edit1.value,  tform.edit1.  Ihave used [] brackets, used an = sign before all to no avail.

I have put form2's unit in the uses section of form1, and put form2's unit into a uses section beneath implementation in form2.

I cant think what else to try, and wonder if anyone has a minute to put me right?

I enclose a screenshot of where I am attempting to place the reference...… not even sure if that is right either!

David.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 635
Re: Newbie query-how to reference control on another form
« Reply #1 on: June 24, 2019, 04:36:01 pm »
This is not a simple question, because of scope.

You have to put the function that copies the value in an unit that uses both form units, and make sure the form variable is in the interface section.

ASerge

  • Hero Member
  • *****
  • Posts: 1302
Re: Newbie query-how to reference control on another form
« Reply #2 on: June 24, 2019, 05:09:49 pm »
In the properties dialog of form2.edit2 I have set the text value to be every possible combination I can think of  (eg  form1.edit1, form1.edit1.text, form1.edit1.value,  tform.edit1.  Ihave used [] brackets, used an = sign before all to no avail.
Text is only text, it is not analyzed or interpreted. To pass values, you must reference variable names or field values.
It is best to declare some configuration procedure in TForm2 and pass it the necessary parameters before calling, for example:
Code: Pascal  [Select]
  1. type
  2.   TForm2 = class(TForm)
  3. //...
  4.   public
  5.     procedure SetupForm(const EditText: string; ListBoxIndex: Integer);
  6. //...
  7. procedure TFrom2.SetupForm(const EditText: string; ListBoxIndex: Integer);
  8. begin
  9.   Edit2.Text := EditText;
  10.   ListBox2.ItemIndex := ListBoxIndex;
  11. end;
  12.  
Code: Pascal  [Select]
  1. uses Form2;
  2. //...
  3. procedure TForm1.ButtonCallForm2Click(Sender: TObject);
  4. begin
  5.   Form2.SetupForm(Edit1.Text, StrToInt(EditIndex.Text));
  6.   Form2.Show;
  7. end;

lucamar

  • Hero Member
  • *****
  • Posts: 1824
Re: Newbie query-how to reference control on another form
« Reply #3 on: June 24, 2019, 06:43:49 pm »
For completeness sake, a more flexible way is adding to your Form2 a property whose setter/getter set/get Edit2.Text directly:

Code: Pascal  [Select]
  1. {NOTE: NOT COMPLETE CODE}
  2. type
  3.  
  4.   { TForm2 }
  5.  
  6.   TForm2 = class(TForm)
  7.     Edit2: TEdit;
  8.   private
  9.     procedure SetSomeText(AValue: String);
  10.     function GetSomeText: String;
  11.   public
  12.     property SomeText: String read GetSomeText write SetSomeText;
  13.   end;
  14.  
  15. var
  16.   Form2: TForm2;
  17.  
  18. implementation
  19.  
  20. {$R *.lfm}
  21.  
  22. { TForm2 }
  23.  
  24. procedure TForm2.SetSomeText(AValue: String);
  25. begin
  26.   Edit2.Text := AValue;
  27. end;
  28.  
  29. function TForm2.GetSomeText: String;
  30. begin
  31.   Result := Edit2.Text;
  32. end;

Then you just have to add unit2 to unit1's "uses" clause and to synch both edit controls do for example:
Code: Pascal  [Select]
  1. procedure TForm1.ButtonClick(Sender: TObject);
  2. begin
  3.   Form2.SomeText := Edit1.Text;
  4.   if Form2.ShowModal = mrYes then
  5.     Edit1.Text := Form2.SomeText;
  6. end;

This solution has (among others) the advantage that if tomorrow you decide to delete Edit2 and use, for example, a TComboBox, all you have to modify are the setter and getter for the "SomeText" property, instead of having to go through all your code looking for places referencing Form2.Edit2.Text.

This is what is referred to as implementation hiding. :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 3043
Re: Newbie query-how to reference control on another form
« Reply #4 on: June 24, 2019, 10:12:35 pm »
I have form2, with a control on it called edit2.  I want to set the value of edit2 to   a control on another form (edit1 on form1).

In the properties dialog of form2.edit2 I have set the text value to be every possible combination I can think of  (eg  form1.edit1, form1.edit1.text, form1.edit1.value,  tform.edit1.  Ihave used [] brackets, used an = sign before all to no avail.

The "properties dialog" you refer to is usually called the Object Inspector.

Its Properties tab is a two-column grid, which has no header.
But if it did have a header, the first column would be called "Property name" and the second column would be headed "Property value".
Each row of the second column is an editor. The values you type (or choose) in each row's editor are text values.
Some of the editors interpret that text as an integer value or as a set item value or an enumeration value etc., but for string properties the value entered is always interpreted as simple text (a string).
As ASerge writes, this text is not interpreted or analysed in any way. It is merely copied - at design time, before your app runs - to the applicable string property you are editing.
So inserting into Edit2.Text the value "Form1.Edit1.Text" merely copies that static 16-character string to the Edit2.Text property.
The Lazarus IDE and the Object Inspector has no notion that such a string can be interpreted by a human as referring to an edit control on another form.
When your app starts, Edit2 will display that 16-character string.
To access at runtime the actual value of Form1.Edit1.Text (a variable, whose value may be continuously changing) requires writing code, as the examples given show.