Solved

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

Posted on 2008-06-24
12
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 38

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 38

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 38

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 38

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month5 days, 17 hours left to enroll

627 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