Link to home
Start Free TrialLog in
Avatar of ChrisBerry
ChrisBerryFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Dynamic creation of controls

Hi there,

Using Delphi 3, I wish to create an indeterminate number of controls(components) such as gauges, progress bars, edit boxes and place them on a form at runtime.

The idea is that the user can select the type of control from a list of controls, and place the control on the form where required, set the size etc.

Basically, I wish to create the controls in an array or list and be able to modify the properties and events as required. I know how to do this with single variables of type TGauge etc but cannot seem to get it to work in an array of type TControl.

Any ideas?

Thanks

Chris
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually this doesn't show how to do it with an arrya but you got the idea how to change the Class Reference to another control at run-time... I hope now you can build your array thing using the example above =)

Regards,
Viktor Ivanov
Avatar of ChrisBerry

ASKER

Hi Viktor,

Thanks for the reply. Looking at the code you have supplied it would appear to do nearly exactly as I wish, I do not need an array.

However two things.

1) I need to reference the controls at run time eg connect say a trackbar to a progress bar and

2) Using your example I am unable to assign any procedures to events. i.e. NewCtrl.OnMouseMove := MagicMouseMove; does not work as OnMouseMove is not recognised.

Any ideas?

If you wish to e-mail me anything my address is cjberry@global.co.za

Thanks

Chris

Here is an example.....
public
    procedure MagicMouseMove(Sender: TObject; Shift: TShiftState; X,
        Y: Integer);
{...}
procedure TForm1.MagicMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
begin
    Caption := IntToStr(x)+','+IntToStr(y);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{
  Instead of having Button1.OnMouseMove put your control instead of that like this....
  NewCtrl.OnMouseMove := MagicMouseMove;
}
    Button1.OnMouseMove := MagicMouseMove;
end;

I hope this helps =)

Regards,
Viktor Ivanov

>1) I need to reference the controls at run time eg connect say a trackbar to
>     a progress bar and

I'm not sure what you mean by connect TrackBar to a ProgressBar... Would you please tell me what exactly is that you are trying to do???

Regards,
Viktor Ivanov
Thanks for the reply.

1) I am not sure what you mean by:-
 
  Instead of having Button1.OnMouseMove put your control instead of that like this....
  NewCtrl.OnMouseMove := MagicMouseMove;
}
    Button1.OnMouseMove := MagicMouseMove

I have tried this but it is rejected by the compiler because NewCtrl does not have a recognised property OnMouseMove. The only way I have been able to achieve this is to use:-

(NewCtrl as TButton).OnMouseMove but this is a problem because it is not generic to all controls. Now NewCtrl must know what type of control it is, is it not possible to use this informarion or the information contained in ClassRef?

2) TrackBar to ProgressBar. Say I create both a TrackBar and a ProgressBar at runtime and wish to connect the output of the TrackBar to the input value (Position) of the ProgressBar. I wish to be able to do this by the operator using a PopUp Menu on the ProgressBar where he has the option to select a source component for input, the TrackBar in this case. Again I think it is one of being able to address the component in a generic fashion. Hope this is clearer.

Thanks once again.

Regards

Chris
Hello Chris =)

If the project is not too big you could send it to me so I can see what exactly is going on and can write the code right in there so I can try it myself... My e-mail is viktor@megsinet.net
I'm going to school today so I'll maybe able to answer later in the evening :(

Regards,
Viktor Ivanov
Hi Viktor,

Please send me a small message at cjberry@global.co.za as I am unable to send to the address you have specified above (not recognised!!!). I will then reply with the project file.

Thanks

Chris