-1

I have a custom component, currently I have to place a TScrollbox on the form then Add/drag the custom component to the scroll box.
How do I go about changing the component so it automatically places itself inside a scrollbox when placed/dragged onto a form?

The custom component is a TGraphicControl.

Johan
  • 74,508
  • 24
  • 191
  • 319
Glen Morse
  • 2,437
  • 8
  • 51
  • 102
  • 1
    Surely you need to get your component to host its own scrollbars – David Heffernan Apr 24 '14 at 10:59
  • yes, but how do I create a scrollbox and set this components parent to it when dragged / dropped on to the form , during desgintime? – Glen Morse Apr 24 '14 at 11:19
  • You don't. You really don't want to do it that way. I know that's what you asked, but it's the wrong solution. You make a control, a single control, that has its own scroll bars. Not a scroll box in sight. – David Heffernan Apr 24 '14 at 11:21
  • Ah, That makes sense.. but does not sound easy.. – Glen Morse Apr 24 '14 at 11:23
  • It may not be trivially easy, but it has the benefit of being tractable and actually working! – David Heffernan Apr 24 '14 at 11:23
  • 2
    If you drag your component *onto* a scrollbox which is on the form then it should be created already inside the scrollbox. Unless you mean that you want to be able to lazily drop your component anywhere on the form, which may or may not contain a scrollbox, and, if it does, to have your component appear inside that scrollbox (which is completely non-intuitive...). Or perhaps you mean to say that you would like your component to create a scrollbox, that doesn't already exist, and place itself inside it when dropped onto a form. It's not really clear... If the latter, just make a UserControl. – J... Apr 24 '14 at 11:46
  • Or perhaps you mean to say that you would like your component to create a scrollbox, that doesn't already exist, and place itself inside it when dropped onto a form. That one – Glen Morse Apr 24 '14 at 21:12

1 Answers1

3

If you have a custom component and you always want it to exist inside of a scrollbox then the cleanest solution would be to either update or extend that component to have its own scrollbox. Here is an example using a TLabel, but you can replace it with whatever your custom component is.

unit MyScrollBox;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.Forms, Vcl.StdCtrls;

type
  TMyScrollComponent = class(TScrollBox)
  private
    FLabel : TLabel;
    procedure SetLabelText(AText : string);
    function GetLabelText : string;
  protected
    constructor Create(AOwner : TComponent); override;
  published       
    property LabelText : string read GetLabelText write SetLabelText;
  end;

procedure Register;

implementation

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

constructor TMyScrollComponent.Create(AOwner : TComponent);
begin
  inherited;
  FLabel := TLabel.Create(self);
  FLabel.Parent := self;
  FLabel.Caption := 'Hello From Scrollbox!';
end;

procedure TMyScrollComponent.SetLabelText(AText : string);
begin
  FLabel.Caption := AText;
end;

function TMyScrollComponent.GetLabelText : string;
begin
  result := FLabel.Caption;
end;

end.

This demonstrates a custom component that inherits from TScrollBox, contains a TLabel and exposes the Caption property of the TLabel through a custom published property. Following this pattern you can also expose whatever other properties of your custom control that you need, etc.

Alternatively, if you want to preserve the ability to make layout changes inside the scrollbox at designtime then another solution would be to make a custom TFrame. Just add one to your project and, following a build, it becomes available in the tool pallette under Standard -> Frames.

J...
  • 30,968
  • 6
  • 66
  • 143
  • When i do this, This `create` will call `map := ThexMap.Create` At which time it should run the create for THexMap correct... cause i am just getting scrollbars with no map now like it never actually runs `create` at desgin time. But just wanted to make sure i was correct here. – Glen Morse Apr 25 '14 at 07:34
  • @GlenMorse You seem to be making reference to code I can't see. I can't say whether it is correct or not. – J... Apr 25 '14 at 09:05
  • Ok, This works i was forgetting to assign parent :D I will accept this answer, but it brings me to another question. How would i access (in this example) Flabel OnMouseDown ... Cause if you click on the component and look at object inspector, it only has OnMouseDown for the scrollbox.. – Glen Morse Apr 25 '14 at 09:25
  • @GlenMorse It does sound like another question. I'd post it as a new question. You might find some guidance here, however : http://stackoverflow.com/questions/9595166/tscrollbox-mousedown-override – J... Apr 25 '14 at 11:09