Can i acess the "main" unit from a .pas file?

I have a form-unit with some Tedit fields. A .pas file is included. In the .pas file i want to set the Tedit fileds in the "main" unit, but i cant get access to the owning form. If i include the name in "uses" i get error. I dont want to have to pass every Tedit pointer. Is there any way to do this?
Paer ToernellAsked:
Who is Participating?
 
SteveBayConnect With a Mentor Commented:
Put you uses under the implementation

implementation
uses Unit1;
{$R *.DFM}

Open in new window

0
 
rfwoolfCommented:
In your project you will have your main form, and also this form that you are talking about now.
If your project doesn't have both these forms, then make sure you add whatever forms are missing to the project.

Then, in the form you speak of, click File -> Use Unit and select the main form you'd like to 'use'. This will add the main form to the uses clause of the secondary form.

Alternatively you can reference it like this
MainForm.Edit1.text := 'whatever';
MainForm.Edit2.text := 'whatever';
MainForm.Edit3.text := 'whatever';

But then you can take a shortcut:
With MainForm do
begin
  Edit1.Text := 'whatever';
  Edit2.Text := 'whatever';
  Edit3.Text := 'whatever';
end;

...I hope this helps
0
 
Geert GOracle dbaCommented:
add the mainunit to the implementation uses section

below is not the best way, but it works
it doesn't however use the blackbox theory ...
unit SubFormUnit;
 
interface 
 
uses Windows, SysUtils, Forms, ...;
 
type
  TSubForm = class(TForm)
    procedure UpdateMain;
  end;
 
var SubForm: TSubForm;
 
implementation
  
uses MainFormUnit;
 
procedure TSubForm.UpdateMain;
begin
  if Assigned(MainForm) then 
  begin
    MainForm.Edit1.Text := Self.EditSub1.Text;
    ...
  end;
end;
 
end.
 
-----------
 
unit MainFormUnit;
 
interface 
 
uses Windows, SysUtils, Forms, ...;
 
type
  TMainForm = class(TForm)
    procedure OpenSubForm;
  end;
 
implementation
 
uses SubFormUnit;
 
procedure TMainForm.OpenSubForm;
begin
  SubForm := TSubForm.Create(Self);
  try
    SubForm.EditSub1.Text := MainForm.Edit1.Text;
    SubForm.ShowModal;  
  finally
    FreeAndNil(SubForm);
  end;
end;

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
2266180Commented:
not the way you dream it :)

but you can change your included pas file to contain


procedure form1.whatever;
var i:integer;
begin
  for i:=1 to componentcount do
    if components[i-1] is TEdit then
      // do whatever
end;

Open in new window

0
 
Geert GOracle dbaCommented:
a better approach is the blackbox theory with using a TStringList as data
That way you can call it with data from any form ...
unit SubFormUnit;
 
interface 
 
uses Windows, SysUtils, Forms, ...;
 
type
  TSubForm = class(TForm)
  end;
 
var SubForm: TSubForm;
 
function OpenSubForm(AOwner: TComponent; var Data: TStrings): boolean;
 
implementation
  
function OpenSubForm(AOwner: TComponent; var Data: TStrings): boolean;
begin
  Result := False;
  SubForm := TSubForm.Create(AOwner);
  try
    SubForm.EditSub1.Text := Data.Values['EDIT1'];
    ...
    if SubForm.ShowModal = mrOk then 
    begin
       Result := True;
       Data.Values['EDIT1'] := SubForm.EditSub1.Text;
       ...
    end;
  finally
    FreeAndNil(SubForm);
  end;
end;
 
-----------
 
and in the mainform :
 
procedure TMainForm.EditData;
var Data: TStringList;
begin
  Data := TStringList.Create;
  try
    Data.Values['EDIT1'] := Edit1.Text;
    ...
    if OpenSubForm(Self, Data) then 
    begin
      Edit1.Text := Data.Values['EDIT1'];
      ...
    end;
  finally
    FreeAndNil(Data);
  end;
end;

Open in new window

0
 
2266180Commented:
for the above, I'd rather use a TObjectList and add the edits in it
0
 
Geert GOracle dbaCommented:
just an example of a theory
0
 
kretzschmarCommented:
what means
> A .pas file is included
??
how?

meikl ;-)
0
 
Geert GOracle dbaCommented:
even more
what's in the included pas file ?
0
 
developmentguruPresidentCommented:
You can refer to your main form, without including it in your sues clause as follows:

  Application.MainForm

If you are using good programming practices then you can use the form method FindChildControl like this:

  (FindChildControl('eCustomerName') as TEdit).Text := 'Test text';

This allows you to use some of Delphi's built in OO power.  You should use appropriate caution with this type of technique though.  It is possible that FindChildControl can't find the control, in which case it returns nil.  This could happen due to a control being removed down the road, or a typo.  This is one example of safer code.

procedure TForm3.Button1Click(Sender: TObject);
var
  Edit : TEdit;

begin
  Edit := FindChildControl('eCustomerName') as TEdit;
  if Assigned(Edit) then
    Edit.Text := 'Test text';
end;

Let me know if you have any questions.
0
 
developmentguruPresidentCommented:
sues = uses

technically you should should also consider having a variable that is of type TConrol to assign the initial result to.  Then you can test like this:

procedure TForm3.Button1Click(Sender: TObject);
var
  Control : TControl;
  Edit : TEdit;

begin
  Control := FindChildControl('eCustomerName');
  if Assigned(Control) and (Control is TEdt) then
    begin
      Edit := Control as TEdit;
      Edit.Text := 'Test text';
    end;
end;

Of course you may want to do assertions to alert you when an expected control is the wrong type or just not found.
0
 
2266180Commented:
developmentguru: you are hard to track down. please post in this quesiton so I can close it: http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23459289.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.