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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing sub components directly

Status
Not open for further replies.

rcloutie

Programmer
May 26, 2005
164
CA
Hi there,

Suppose the following code:
------------------------------------
type
TPanelList = class(TPanel)
private
btnTitle: TBitBtn;
lstItem: TListBox;
published
property Title:TBitBtn read btnTitle write btnTitle;
property List: TListBox read lstItem write lstItem;
end;
------------------------------------
How to access properties and methods of one sub component directly and how the main component to respond to events of the sub component?

My component is already implemented in many forms but I want to add a new property 'Caption' (via a BitBtn control)to a descendant of a TListBox.

Actually, the references are like MyList.Items... and all events generated come from TListBox (MyList.OnItemSelect) But with the new TPanel as a parent, references will be like MyList.List.Items and events won't be raised...

Using Type TPanelList = class(TPanel, TListBox) will not work because ListBox has to be within a TPanel...

Any clues?

Thanks in advance.
 
Delphi makes it possible to access only one property directly (marked as default) and that property must be an array property.

So in your case it is NOT possible to access subcomponents directly, for two reasons:
- A subcomponent is not a property array
- You can only have one default property array

However the "with" keyword in delphi may help you to trick your way into what you are looking for as for example:


Code:
var
   myPanelList:TPanelList;
...

      with myPanelList do
      begin
           //Here you can refer to List and Title directly
           ShowMessage(List.Items+' '+Title.Caption);
           
           //You can also assign your event handlers 
           //related to the List subcomponent at runtime
           //e.g. 
           List.OnClick=ListBox1Click;
      end; 

...

//////////////////////////////////////////////////////////
// procedure: ListBox1Click
// purpose  : Handle List subcomponent Click event 
// Note: 
//       If you have such events already written than you
//       can assign them to your List subcomponent as 
//       described above 
//////////////////////////////////////////////////////////
procedure TfrmViewActivity.ListBox1Click(Sender: TObject);
begin
     ShowMessage('List has been clicked');
end;


Another trick to refer to both Title and List properties directly may be:

Code:
var
   myPanelList:TPanelList;

...

with myPanelList.List, myPanelList.Title do
begin
     //here you can refer directly to 
     //List (TListBox) and Title(TBtnButton) properties
     //e.g.
     ShowMessage(Items.TExt+' '+Caption);   
end;


I must say that none of the above answers your desire to access subcomponents directly.



 
Hi,

Well I created a frame composed of a TPanel, a BitBtn and a ListBox... It is not as friendly user as a VCL but hey... when no other option is possible... :)

Thanks anyway for your time...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top