Solved

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

Posted on 2008-06-24
12
199 Views
Last Modified: 2010-04-05
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?
0
Comment
Question by:Sunsales
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 21856444
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
 
LVL 14

Accepted Solution

by:
SteveBay earned 500 total points
ID: 21856455
Put you uses under the implementation

implementation
uses Unit1;
{$R *.DFM}

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 21856463
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.

 
LVL 28

Expert Comment

by:2266180
ID: 21856530
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
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 21856561
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
 
LVL 28

Expert Comment

by:2266180
ID: 21856620
for the above, I'd rather use a TObjectList and add the edits in it
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 21856658
just an example of a theory
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 21862881
what means
> A .pas file is included
??
how?

meikl ;-)
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 21863010
even more
what's in the included pas file ?
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 21874285
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
 
LVL 21

Expert Comment

by:developmentguru
ID: 21874431
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
 
LVL 28

Expert Comment

by:2266180
ID: 21874640
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question