Discussion:
Dynamic TEdit instance
(too old to reply)
Sipke Wadman
2008-08-30 15:59:04 UTC
Permalink
Hi,

I want to create TEdit boxes at runtime. I am using;
TEdit *Edit = new TEdit(this);
Edit->Parent = this;

That is working fine, but now I want to create an array of varying size of
them. I would think the following would work, as it does with 'normal'
types, but of course it does not;
TEdit **Edit = new *TEdit[10];
for (int x=0;x<10;x++){
Edit[x] = new TEdit(this);
Edit[x]->Parent = this;
}

The following seems to be working fine, but then I cannot adjust the number
of boxes at runtime;
TEdit *Edit[10]
for (int x=0;x<10;x++){
Edit[x] = new TEdit(this);
Edit[x]->Parent = this;
}

Anyone that can help me with this? I suppose it is really easy.

Thanks,
Sipke
m***@gmail.com
2008-09-02 08:59:24 UTC
Permalink
Something like this:


TEdit ***Edits; // Must be visible in destructor
int N1 = 5,
N2 = 5;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
// . . . Must manually instantiate: TEdit *Edits[5][5];
Edits = new TEdit**[N1]; // pointers of pointers array
for (int y=0; y<N1; ++y) Edits[y] = new TEdit*[N2]; // pointers
arrays

// . . . Instantiate controls
for (int y=0; y<N2; ++y)
for (int x=y; x<N1; ++x)
{
Edits[x][y] = new TEdit(this);
Edits[x][y]->Width = 70;
Edits[x][y]->Height = 20;
Edits[x][y]->Top = 80 + Edits[x][y]->Height*y;
Edits[x][y]->Left = 15 + Edits[x][y]->Width*x;
Edits[x][y]->Text = String(x) + String(";") + String(y);
Edits[x][y]->Parent = this;
}
}

//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
// Edits would be disposed by respective owner,
// but I must manually delete the arrays
for (int y=0; y<N2; ++y) for (int x=y; x<N1; ++x) delete Edits[x]
[y];
for (int y=0; y<N2; ++y) delete[] Edits[y];
delete[] Edits;
}
//---------------------------------------------------------------------------
Sipke Wadman
2008-09-03 18:40:56 UTC
Permalink
Ok, Thanks...
So basically, I was stupid and put the * at the wrong side of TEdit....
hmmm.
Thanks a lot!
--
My e-mail address is changing!
Post by m***@gmail.com
TEdit ***Edits; // Must be visible in destructor
int N1 = 5,
N2 = 5;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
// . . . Must manually instantiate: TEdit *Edits[5][5];
Edits = new TEdit**[N1]; // pointers of pointers array
for (int y=0; y<N1; ++y) Edits[y] = new TEdit*[N2]; // pointers
arrays
// . . . Instantiate controls
for (int y=0; y<N2; ++y)
for (int x=y; x<N1; ++x)
{
Edits[x][y] = new TEdit(this);
Edits[x][y]->Width = 70;
Edits[x][y]->Height = 20;
Edits[x][y]->Top = 80 + Edits[x][y]->Height*y;
Edits[x][y]->Left = 15 + Edits[x][y]->Width*x;
Edits[x][y]->Text = String(x) + String(";") + String(y);
Edits[x][y]->Parent = this;
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
// Edits would be disposed by respective owner,
// but I must manually delete the arrays
for (int y=0; y<N2; ++y) for (int x=y; x<N1; ++x) delete Edits[x]
[y];
for (int y=0; y<N2; ++y) delete[] Edits[y];
delete[] Edits;
}
//---------------------------------------------------------------------------
A***@googlemail.com
2008-09-02 10:26:29 UTC
Permalink
Don't use arrays! Use a vector instead:

In your header file:

#include <vector>
using namespace std;

class TForm5 : public TForm
{
//add this line to the private section:
vector <TEdit *> edits;
}

In your cpp file:

void __fastcall TForm5::FormShow(TObject *Sender) //or wherever seems
appropriate
{
for (int i = 0; i < 10; ++i)
{
TEdit * e = new TEdit(this);
e->Parent = this;
//set other properties as needed

//store the pointer in the vector, where it can be retrieved/deleted/
new edits added etc
edits.push_back(e);
}
}

When your form is released/closed the vector will take care of its own
memory. The edits will be freed by the form as it is their parent.

Andy
Loading...