Board index » cppbuilder » Dynamic VCL objects

Dynamic VCL objects


2004-01-29 06:15:58 PM
cppbuilder57
Hi All...
I have a need to create VCL objects on the fly...
I use this code:
class TEd: public TEdit
{
public:
__fastcall TEd(void);
};
__fastcall TEd::TEd(void):TEdit((TComponent*) NULL)
{
Visible=true;
}
Then I instanciate....
TEd te= new TEd;
te.Width=99;
te.ParentWindow=WinHan;
te.Text="Hello";
This all works OK, and the controls appear, but how do I assign the callback
functions for "Change", Mouse clicks etc.
Mark Ravelle
 
 

Re:Dynamic VCL objects

Hi,
Here is a excerpt from some code that I have written to do specifically
that:
In the header file I declare the following variable:
TEdit VAData[3][12]; // 2 dimensional array of edit boxes, dynamically
created
Here is the code that dynamically creates the components:
for(int x = 0, i = 0; x < 3; x++, i++) {
for(int y = 0; y < 12; y++) {
VAData[y][i] = new TEdit(this);
VAData[y][i]->Parent = ChanData;
VAData[y][i]->Top = 40 + (y * 21);
VAData[y][i]->Left = 35 + (x * 42);
VAData[y][i]->Color = (TColor)0x00E3F0DD;
VAData[y][i]->Width = 40;
VAData[y][i]->Height = 17;
VAData[y][i]->AutoSize = false;
VAData[y][i]->Visible = true;
VAData[y][i]->Tag = (12 * x) + y;
VAData[y][i]->OnClick = VADataClick;
VAData[y][i]->OnExit = VADataExit;
VAData[y][i]->OnKeyPress = VADataKeyPress;
}
}
Here is one of the event handlers:
void __fastcall TForm1::VADataClick(TObject *Sender)
{
TEdit *ep = dynamic_cast<TEdit *>(Sender);
ep->SelectAll();
}
Here is another handler. This one makes use of the Tag property:
//--------------------------------------------------------------------------
-
void __fastcall TForm1::VADataExit(TObject *Sender)
{
short FldVal;
double temp;
TEdit *ep = dynamic_cast<TEdit *>(Sender);
try {
int Col = ep->Tag / 12;
int Row = ep->Tag % 12;
temp = ep->Text.ToDouble() * 100.0;
FldVal = (short)temp;
switch(Col) {
case 0:
Channel[Row]->DimmingR = FldVal;
break;
case 1:
Channel[Row]->VThreshR = FldVal;
break;
case 2:
Channel[Row]->PThreshR = FldVal / 10.0;
break;
}
UpdateChannelData();
SetChannelParameters(Row);
}
catch(EConvertError&) {
Application->MessageBox("Invalid data for floating point number - Please
try again!",
"Bad Data Entered", MB_OK);
ep->Clear();
return;
}
}
//--------------------------------------------------------------------------
-
Hope this helps!