?
Solved

Making a component with other components

Posted on 2002-04-16
5
Medium Priority
?
314 Views
Last Modified: 2010-04-04
This question is a simplified version of what I'm actually trying to do in real life.  I will give these points to the person who helps me build the following component.  Once this is built successfully, I will know what to do to solve the real problem I have at hand.

I want to make a component called TRadioAndCombo.  It has two components: a radio button and a combo box.  When the radio button is selected, I want the combo box to be enabled.  When it is not selected, the combo box should be disabled and ItemIndex should revert back to -1.  TRadioAndCombo will also have some new properties which I will write which are independent of the child components.

At design-time, I do not want to allow the application developer to directly manipulate the radio button and combo box.  For instance, I do not want them to be able to reposition them within the TRadioAndCombo border; plus I don't want them to be able to enable and disable each component independently.

When the programmer uses TRadioAndCombo at design-time, he/she should have access to an Items property, which is merely a wrapper for the Combo's Items property.  The ItemIndex property will simply read and return the value of ItemIndex from the combo.  TRadioAndCombo.Caption should read and write to the radio button caption.  The Tag property will not read either Tag property of the radio button or combo box, but will simply be a tag for TRadioAndCombo.  Do you see where I'm going?  The new component will expose some key properties of the components that it contains, but not ALL of those properties.

As you can see, I can't inherit from TRadioButton or TComboBox because I want the functionality of both objects.  Plus, I want to hide stuff about them.  An event in TRadioButton must manipulate the ComboBox object, as described above.  I have tried using a TFrame to do all this, but that doesn't work for me since the developer has access to the two inside components.  Plus, the properties I added to the frame were not visible in the Object Inspector, even though they were published simple types.  I also seemed to have lost the radio button event handler that I wrote in the frame's unit.

When a user forward-tabs into my component, I want the radio button to have focus.  If the combo box is enabled, then another forward-tab will move the focus to the combo box.  If the user backward-tabs into my component, then focus will go to the combo box if it is enabled, and to the radio button otherwise.  TRadioAndCombo will not appear to the user as ever having focus.  The user will see the two components and just know that they work together.

I feel sure that TRadioAndCombo will have to inherit from TCustomComponent, TWinControl, or something basic like that.  Maybe it will be a TGroupBox; I don't know.  If I must design the component programmatically, then so be it, as long as it's easy for someone like me who's not quite comfortable with concepts like painting and low-level Windows messaging.  I don't think I'll have the option to design the component visually.

So, EE experts, how do I get this thing to work? :)
0
Comment
Question by:Pummel
[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
  • 3
5 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6945112
hi pummel(chen) :-)),

you have to write two wrapper-classes
(for the combo and the radio),
(maybe it works also with one)
which inherited from tpersistent,
where you passthrough the properties you want

as base-class, which does the both controls
bundles, you could use a twincontrol

just as kick off

meikl ;-)

0
 
LVL 1

Expert Comment

by:Alone
ID: 6945123
Try to inherit it from TCustomGroup box or TCustomPanel. When I've solved similar problem I've used TCustomPanel descedant
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 1200 total points
ID: 6945154
an older sample,
just for showing, what i meant

a panel which holds a speedbutton

unit SBPanel;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Buttons;

type
  TSBPanelBtn = class(TSpeedButton)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    Procedure Click; OverRide;
  published
    { Published declarations }
  end;

  TButtonProperties = class(TPersistent)
  private
    fButton : TSpeedButton;
  protected
    function  GetCaption : TCaption;
    procedure SetCaption(ACaption : TCaption);
    function  GetFlat : Boolean;
    procedure SetFlat(ABoolean : Boolean);
    function  GetGlyph : TBitmap;
    procedure SetGlyph(ABitmap : TBitmap);
  public
    constructor Create(AButton : TSpeedButton); OverLoad;
  published
    property Caption : TCaption read GetCaption write SetCaption;
    property Flat : Boolean read GetFlat write SetFlat;
    property Glyph : TBitmap read GetGlyph write SetGlyph;
  end;


  TSBPanel = class(TPanel)
  private
    fSBPanelBtn : TSBPanelBtn;
    fButtonProperties : TButtonProperties;
  protected
    function  GetOnButtonClick : TNotifyEvent;
    procedure SetOnButtonClick(AEvent : TNotifyEvent);
  public
    constructor Create(AOwner : TComponent); OverRide;
    destructor destroy; OverRide;
  published
    property ButtonProperties : TButtonProperties read fButtonProperties write fButtonProperties;
    property OnButtonClick : TNotifyEvent read GetOnButtonClick write SetOnButtonClick;
  end;

procedure Register;

implementation


//Button
procedure TSBPanelBtn.Click;
begin
  inherited Click;
  if (assigned(Parent)) and
     (parent is TSBPanel) then
    TSBPanel(parent).Click;
end;

//ButtonProperties (make it visible and storeable to the Objectinspector)
constructor TButtonProperties.Create(AButton : TSpeedButton);
begin
  inherited Create;
  if assigned(AButton) then
    fButton := AButton;
end;

function  TButtonProperties.GetCaption : TCaption;
begin
  result := '';
  if assigned(fButton) then
    result := fButton.Caption;
end;

procedure TButtonProperties.SetCaption(ACaption : TCaption);
begin
  if assigned(fButton) then
    fButton.Caption := ACaption;
end;

function  TButtonProperties.GetFlat : Boolean;
begin
  result := false;
  if assigned(fButton) then
    result := fButton.Flat;
end;

procedure TButtonProperties.SetFlat(ABoolean : Boolean);
begin
  if assigned(fButton) then
    fButton.Flat := ABoolean;
end;

function  TButtonProperties.GetGlyph : TBitmap;
begin
  result := nil;
  if assigned(fButton) then
    result := fButton.Glyph;
end;

procedure TButtonProperties.SetGlyph(ABitmap : TBitmap);
begin
  if assigned(fButton) then
    fButton.Glyph := ABitmap;
end;


//The Panel
constructor TSBPanel.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  Caption := '';
  fSBPanelBtn := TSBPanelBtn.Create(Self);
  fSBPanelBtn.Parent := self;
  fSBPanelBtn.Align := alClient;
  fButtonProperties := TButtonProperties.Create(fSBPanelBtn);
end;

destructor TSBPanel.destroy;
begin
  fButtonProperties.free;
  inherited destroy;
end;


function  TSBPanel.GetOnButtonClick : TNotifyEvent;
begin
  result := nil;
  if assigned(fSBPanelBtn) then
    result := fSBPanelBtn.OnClick;
end;

procedure TSBPanel.SetOnButtonClick(AEvent : TNotifyEvent);
begin
  if assigned(fSBPanelBtn) then
    fSBPanelBtn.OnClick := AEvent;
end;


procedure Register;
begin
  RegisterComponents('Samples', [TSBPanel]);
end;

end.

meikl ;-)
0
 

Author Comment

by:Pummel
ID: 6945322
That should do it.  Thank you.  Code always serves as a good teacher to me--more than just words and definitions.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6945353
>Code always serves as a good teacher to me
>--more than just words and definitions.

to me too :-)

thanks for points,
hope you get it work as you recommended,
if you've problems just ask in this thread,
and i will try to help you further

meikl ;-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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