Board index » cppbuilder » Positioning of dynamically created components

Positioning of dynamically created components

A short question concerning the alignment of dynamically created
components. I create the components at runtime by the following code
example. The result is are three instances of a TPanel and a
TStringGrid. Each TPanel should contain the title of the TStringGrid.
But to keep the correct order I have to start with the creation of the
last TPanel-TStringGrid-combination and decrease the loop counter to get

Panel1
StringGrid1
Panel2
StringGrid2
Panel3
StringGrid3

If another panel is statically placed on top of the form (alTop) it is
pushed to the bottom after the dynamical creation of the others. What is
a better approach to get the alignment correct and in a chronological
sorting
(top to bottom with respect to the creation order)?

Example:
======

typedef vector<TStringGrid *> TStringGrids;
typedef vector<TPanel *> TPanels;

//---------------------------------------------------------------------------

class TForm1 : public TForm
{
    .
    .
  TStringGrids StringGrids;
  TPanels Panels;

Quote
};

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  for (int k=3; k>0; --k)   // decrease the counter
!!!!!!!!!!!!!!!!!!!!!!!!!
  {
    TStringGrid *StringGrid = new TStringGrid(this);
    StringGrid->Parent = Form1;
    StringGrid->RowCount = 10;
    StringGrid->ColCount = 6;
    //put stuff in the grid
    for (int i=0;i<StringGrid->RowCount;i++)
      for (int j=0;j<StringGrid->ColCount;j++)
        StringGrid->Cells[j][i]="R"+IntToStr(i)+"C"+IntToStr(j);
    StringGrid->Align = alTop;
    StringGrids.push_back(StringGrid);

    TPanel *Panel = new TPanel(this);
    Panel->Parent = Form1;
    Panel->Height = 30;
    Panel->Align = alTop;
    Panels.push_back(Panel);
  }

Quote
}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
// free  memory ...

Quote
}

Thanks,

Marold

  Marold.Moosrainer.vcf
< 1K Download
 

Re:Positioning of dynamically created components


Hi Marold,

Quote
> What is a better approach to get the alignment correct and in a chronological
> sorting (top to bottom with respect to the creation order)?

At design-time, to manipulate the placement of the controls whose Align property
are the same, you'd simply drag and drop the component to a new location.  For
example, if you have three Panels all Align(ed) to alTop, you can make the
bottom-most Panel be the top-most by simply dragging it to the top.  At run-time
the idea is the same -- simply change the location of the control whose
alignment order you'd like to change.  For the Panels example, you'd simply set
the bottom-most Panel's Top property to zero.  For your situation, you can do
something similar to...

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    for (int index = 0; index < 3; index++)
    {
        TPanel *Panel = new TPanel(this);
        Panel->Parent = this;
        Panel->Height = 30;
        Panel->Align = alTop;
// -----------------------------------
        Panel->Top = Height;
// -----------------------------------
        Panels.push_back(Panel);

        TStringGrid *StringGrid = new TStringGrid(this);
        StringGrid->Parent = this;
        StringGrid->RowCount = 10;
        StringGrid->ColCount = 6;
        StringGrid->Align = alTop;
// -----------------------------------
        StringGrid->Top = Height;
// -----------------------------------
        StringGrids.push_back(StringGrid);

        //put stuff in the grid ...
    }

Quote
}

Good luck!

--------------------------------------
Damon Chandler

http://bcbcaq.freeservers.com
Answers to <Commonly Asked Questions>
52064929

Other Threads