Board index » delphi » dynamically create/free multiple controls on a scrollbox

dynamically create/free multiple controls on a scrollbox

If I dynamically create/free multiple controls on a scrollbox, they redraw
one by one as they are created/freed, which I want to suppress.
What I use is as follows:

procedure Load;
var i:Integer;
    y:Integer;
begin
   ScrollBox.AutoScroll:=False;
   ScrollBox.VertScrollBar.Visible:=False;
   ScrollBox.Visible:=False;
   try
      i:=ScrollBox.ComponentCount-1;
      while i>=0 do begin
         ScrollBox.Components[i].Free;
         Dec(i);
      end;
      y:=4;
      Query.SQL:='SELECT name FROM table';
      Query.Open;
      while not(Query.EOF) do begin
         with TEdit.Create(ScrollBox) do begin
            Parent:=ScrollBox;
            Left:=4;
            Width:=Parent.ClientWidth-8;
            Top:=y;
            Text:=Query.Fields[0].AsString;
            Inc(y,Height+4);
         end;
         Query.Next;
      end;
      Query.Close;
   finally
      ScrollBox.Visible:=True;
      ScrollBox.VertScrollBar.Visible:=True;
      ScrollBox.AutoScroll:=True;
   end;
end;

This works, it is fast, but it is not nice, as during creation the component
is hidden, which affects dimensions of neighbouring components etc.

(In the above example only Edits are used for short, I know that in this
special case the solution is using string grid, but in reality
I need to create a more complex structure, like an image,
label and a checkbox for each item.)

Any hints?  TIA.
Daniel Rott
--
<sorry about my english>

 

Re:dynamically create/free multiple controls on a scrollbox


What about creating a class that will contain all the controls for each type
of 'setting' you want to show? As in using a TWinControl as the ancestor..
Then all you need to create and free would be your 'showsettings' class..

Viva OOP !

cheers,
Siegfried Niedinger

Quote
Daniel Rott <rox...@iol.cz> wrote in message

news:7h8kj6$nbb16@forums.borland.com...
Quote
> If I dynamically create/free multiple controls on a scrollbox, they redraw
> one by one as they are created/freed, which I want to suppress.

Re:dynamically create/free multiple controls on a scrollbox


This reduces, but does not eliminate the problem.
Several items have to be created anyway.
Maybe, setting scrollbar's ScrollBox.AutoScroll:=False would do.
Don't you have an example?

Thanks
Daniel Rott

Quote
>What about creating a class that will contain all the controls for each
type
>of 'setting' you want to show? As in using a TWinControl as the ancestor..
>Then all you need to create and free would be your 'showsettings' class..

>Viva OOP !

>cheers,
>Siegfried Niedinger

Re:dynamically create/free multiple controls on a scrollbox


example..

if you derive a new class from TWinControl..{or even TCustomPanel?}

type TShowControlsType1 = Class(TWinControl) private
  FDBEdit: TDBEdit;
  FImage: TImage;
  FDataset: TDataset;
public
  FDBEditDataset : TDataset;
  FDBEditDatafield: string;
  constructor Create(AOwner); override;
  destructor Destroy; override;
end;

constructor TShowControlsType1.Create(AOwner);
begin
    inherited Create(AOwner);
    Parent := AOwner as TWinControl;  {So that you don't need to set in app}
    Left := 1;
    Top := 1;
     Width := (AOwner as TWincontrol).Width;
      etc..

    FEditbox := TEditBox.Create(Self);
    with FEditBox do
    begin
         Parent        := Self as TWinControl;
         Dataset     := FDBEDitDataset;
         DataField :=  FDBEditDataField;
         Height       := 21;
         Left            := ....etc.
    end;
....etc.....
end;

destructor TShowControlsType1.Destroy;
begin
     FDBEdit.Free;
     FImage.Free;
     inherited;
end;
--------------
I dunno if I understand your problem correctly, but I just set the
Scrollbox.Visible to False before creating the 'childcontrols' ..

Regards,
Siegfried Niedinger

Quote
Daniel Rott <rox...@iol.cz> wrote in message

news:7h99v4$o778@forums.borland.com...
Quote
> This reduces, but does not eliminate the problem.
> Several items have to be created anyway.
> Maybe, setting scrollbar's ScrollBox.AutoScroll:=False would do.
> Don't you have an example?

> Thanks
> Daniel Rott

Re:dynamically create/free multiple controls on a scrollbox


Quote
In article <7h99v4$o...@forums.borland.com>, Daniel Rott wrote:
> Maybe, setting scrollbar's ScrollBox.AutoScroll:=False would do.

Does the following help?:

  ScrollBox1.DoubleBuffered := True;

 Mike Orriss (TeamB)
 (No e-mail replies, please, unless explicitly requested!)

Re:dynamically create/free multiple controls on a scrollbox


Maybe this one will do the trick:

procedure Load;
var i:Integer;
    y:Integer;
begin
   LockWindowUpdate(ScrollBox.Handle);
   try
      i:=ScrollBox.ComponentCount-1;
      while i>=0 do begin
         ScrollBox.Components[i].Free;
         Dec(i);
      end;
      y:=4;
      Query.SQL:='SELECT name FROM table';
      Query.Open;
      while not(Query.EOF) do begin
         with TEdit.Create(ScrollBox) do begin
            Parent:=ScrollBox;
            Left:=4;
            Width:=Parent.ClientWidth-8;
            Top:=y;
            Text:=Query.Fields[0].AsString;
            Inc(y,Height+4);
         end;
         Query.Next;
      end;
      Query.Close;
   finally
      LockWindowUpdate(0);
   end;
end;

Other Threads