Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing tabsheet child object properties

Status
Not open for further replies.

rtb1

IS-IT--Management
Jul 17, 2000
73
ES
Hi,

I have tabsheets created in run-time and TMemo's as the child object.

On top of the PageControl I have a TEdit. The idea is that the TEdit shows the name of the TMemo that is on the active tabsheet. How can you access the name property of the specific TMemo on the active TabSheet?

Code:
procedure TForm1.TabSheet1Show(Sender: TObject);
begin
   Edit1.Text := (Sender as TTabsheet).?????????.Name;
end;

Thanks,
Raoul
 
In the interface section of your unit, under where your form components are declared, add the following declaration:
Code:
procedure GenericTabShow(Sender: TObject);
In your implementation section define the routine as:
Code:
procedure TForm1.GenericTabShow(Sender: TObject);
var
  i: Integer;
begin
  // finds first TMemo child component and displays name
  for i := 0 to (Sender as TTabSheet).ControlCount - 1 do
    if (Sender as TTabsheet).Controls[i] is TMemo then
    begin
      Edit1.Text := (Sender as TTabsheet).Controls[i].Name;
      Exit;
    end;
end;
Finally, go to each TabSheet's OnShow event in the property editor, and select GenericTabShow as the referenced method.
This way you have only needed to write one method to provide the same functionality for each tabsheet's OnShow event.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks Stretchwickster!

For the Name property this works fine. But how to access the properties of for example a TMemo that are not associated with Controls (like WordWrap)?

Raoul
 
You just need to typecast the control as a TMemo, i.e.
Code:
((Sender as TTabsheet).Controls[i] as TMemo).Name;
The typecast will be safe because we already know that the control is a memo, because of the line
Code:
if (Sender as TTabsheet).Controls[i] is TMemo then
For the sake of making your code more readable you could assign the control to a variable, e.g.
Code:
procedure TForm1.GenericTabShow(Sender: TObject);
var
  i: Integer;
  ActiveMemo: TMemo;
begin
  // finds first TMemo child component and displays name
  for i := 0 to (Sender as TTabSheet).ControlCount - 1 do
    if (Sender as TTabsheet).Controls[i] is TMemo then
    begin
      ActiveMemo := ((Sender as TTabsheet).Controls[i] as TMemo);
      Edit1.Text := ActiveMemo.Name;
      Exit;
    end;
end;

Steve
 
Thanks, it is working now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top