Board index » cppbuilder » How to create a control array at runtime (for example like in Visual Basic)

How to create a control array at runtime (for example like in Visual Basic)

Thanks to bcbdev.com, I have known how to creat a control at runtime. It's
something like:

    class TForm1 : public TForm
    {
        __published:   // IDE-managed Components
        private:       // User declarations
            TButton *OKButton;
        ...

    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
        OKButton = new TButton(this);
        OKButton->Parent  = this;
        OKButton->Caption = "Dynamic Button";
        OKButton->SetBounds(10,10,110,25);
    }

But How to create a control array at runtime (for example like in Visual
Basic).

TIA.

--
$$$$$$$$$$$$
James Pontiff
$$$$$$$$$$$$

 

Re:How to create a control array at runtime (for example like in Visual Basic)


Hi, James,

Quote
> But How to create a control array at runtime (for example like in Visual

As long as You know, how many controls are to be designed, its not much
different:

     class TForm1 : public TForm
     {
         __published:   // IDE-managed Components
         private:       // User declarations
             TButton * OKButton[20];
         ...

     __fastcall TForm1::TForm1(TComponent* Owner)
         : TForm(Owner)
     {
          int i;
          for (i=0;i<20;i++)
         {
          OKButton[i] = new TButton(this);
          OKButton[i]->Parent  = this;
          OKButton[i]->Caption = "Dynamic Button"+IntToStr(i);
          OKButton[i]->SetBounds(10,10+30*i,110,25);
         }
     }

But, important: before OKButton[] is deleted, all the buttons must be
deleted - OKButton[] is nothing but an array of pointers, so

           for (i=0;i<20;i++)
             if (OKButton[i])
                delete OKButton[i];
must be done as cleanup.

Good Luck
Michael

Re:How to create a control array at runtime (for example like in Visual Basic)


Actually, it doesn't need to be done.  Each button was given an Owner in the
constructor, and the Oner will automatically handle cleaning up the buttons.

Gambit

Quote
"Michael Kutscher" <Michael-Kutsc...@pc-coach.de> wrote in message

news:9q0tfs$pmf$05$1@news.t-online.com...
Quote
> But, important: before OKButton[] is deleted, all the buttons must be
> deleted - OKButton[] is nothing but an array of pointers, so

>            for (i=0;i<20;i++)
>              if (OKButton[i])
>                 delete OKButton[i];
> must be done as cleanup.

Re:How to create a control array at runtime (for example like in Visual Basic)


Thank you, Michael,
But I do not know how many controls are to be designed before, controls's
count is dynamic.
How should I write the soruce?
Quote
> As long as You know, how many controls are to be designed, its not much
>              TButton * OKButton[20];

Re:How to create a control array at runtime (for example like in Visual Basic)


Thank you, Remy.

Re:How to create a control array at runtime (for example like in Visual Basic)


You can do one of two things:

1) allocate the array dynamically when you do know the count:

TButton** OKButtons;
int count = 20;

OKButtons = new TButton*[count];
for(int x = 0; x < count; x++)
{
    OKButtons[x] = new TButton(this);
    // other properties....

Quote
}

// when finished... remembering that the actual buttons are
// automatically taken care of by the Owner
delete[] OKButtons;

2) use a std::vector instead (this is much safer than handling all of the
allocates yourself, and you can access the items in it just like a regular
array, using the [] operator):

#include <vector>

std::vector<TButton*> OKButtons;
for(int x = 0; x < 20; x++)
{
    TButton *btn = new TButton(this);
    // other properties....

    OKButtons.push_back(btn);

Quote
}

for(int x = 0; x < 20; x++)
{
    TButton *btn = OKButtons[x];
    // use btn as needed...

Quote
}

Gambit

Quote
"James Pontiff" <jamespont...@citiz.net> wrote in message

news:3bc4e20d_2@dnews...
Quote
> But I do not know how many controls are to be designed before, controls's
> count is dynamic.
> How should I write the soruce?

Other Threads