Board index » delphi » Accessing Child form objects from the Parent form?

Accessing Child form objects from the Parent form?

I am writing a MDI application.  I need to access object properties of
child windows from the parent form as if they were on the parent form.
For example, I have a memo component on the child form and want to add
a speedbutton on the parent form and do something like
(This code is in Unit1)

procedure TForm1.SpeedButton1Click(sender: TObject);
begin
Memo1.Lines.Add('I need text here');
end;

Or even reverse the procedure ( Edit2 object on the child and Edit1 &
Button1 on the parent) something like

procedure TForm1.Button1Click(sender: TObject);
begin
  Edit1.Text := Edit2.Text;
end;

I have added each unit to the uses and implementation areas to access
the form.  But of course this does not give me the ability to access
object properties from one another.  I have also tried declaring each
form in the type area ie. Form2: TForm2; for unit1.

I realized this is probably a very basic concept but I am new to
delphi and have not been able to find any reference to this in the
manual or help files.  The only thing I can find is using menus to do
this. However, I need to use a toolbar as well.  I have fiddled with
the group index of the buttons but have not had any luck.

Thanks in advance
Mike Cox
zor...@inetw.net

PS I need a 16 and 32 bit app so I would like to compile the 16 bit
version in D1. Hence, any commands should be applicable to D1.

 

Re:Accessing Child form objects from the Parent form?


Quote
Mike Cox wrote:

> I am writing a MDI application.  I need to access object properties of
> child windows from the parent form as if they were on the parent form.
> For example, I have a memo component on the child form and want to add
> a speedbutton on the parent form and do something like
> (This code is in Unit1)

> procedure TForm1.SpeedButton1Click(sender: TObject);
> begin
> Memo1.Lines.Add('I need text here');
> end;

Off the top of my head:

MDIActiveChild.Memo1.Lines.Add('I need text here');

Quote

> Or even reverse the procedure ( Edit2 object on the child and Edit1 &
> Button1 on the parent) something like

> procedure TForm1.Button1Click(sender: TObject);
> begin
>   Edit1.Text := Edit2.Text;
> end;

Again, off the top of my head:

Edit1.Text := MDIActiveChild.Edit2.Text;

Quote

> I have added each unit to the uses and implementation areas to access
> the form.  But of course this does not give me the ability to access
> object properties from one another.  I have also tried declaring each
> form in the type area ie. Form2: TForm2; for unit1.

You should be able to access properties of the children and parent from
each other just fine, assuming you made them public.  But you have to
find the instance of the child form that you are interested in, either
by using MDIActiveChild, or looping through MDIChildren of the parent
form...
Quote

> I realized this is probably a very basic concept but I am new to
> delphi and have not been able to find any reference to this in the
> manual or help files.  The only thing I can find is using menus to do
> this. However, I need to use a toolbar as well.  I have fiddled with
> the group index of the buttons but have not had any luck.

We've all been there.  Hope this helps...  :-)

Other Threads