Solved

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

Posted on 2008-06-24
12
195 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 36

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
 
LVL 28

Expert Comment

by:ciuly
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 36

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:ciuly
ID: 21856620
for the above, I'd rather use a TObjectList and add the edits in it
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 36

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 36

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:ciuly
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now