[code]
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TComboBox *cbStdName = new TComboBox(this);
cbStdName->Top = 115;
cbStdName->Left = 195;
cbStdName->Width = 65;
cbStdName->Height = 13;
cbStdName->Visible = true;
}
[/code]
When i run this code it show an empty Form, Where is my ComboBox?
M-Nasim
Comments
: [code]
: void __fastcall TForm1::FormCreate(TObject *Sender)
: {
: TComboBox *cbStdName = new TComboBox(this);
: cbStdName->Top = 115;
: cbStdName->Left = 195;
: cbStdName->Width = 65;
: cbStdName->Height = 13;
: cbStdName->Visible = true;
: }
: [/code]
:
: When i run this code it show an empty Form, Where is my ComboBox?
:
: M-Nasim
:
Hy Guys, here is the answer
[code]
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TComboBox *cb = new TComboBox(this); //1
cb->Left = 15;
cb->Top = 15;
GroupBox1->InsertControl(cb); //2
}
[/code]
Explanation
//1 to make a pointer to a combobox and make his "Owner" our form
//2 to insert this combobox into your groupbox i.e to make the groupbox is the parent (not owner) of this combobox
obviously i create this groupbox at design time.
can you create this parent at runtime ?!
of course, just make its parent our form
Mohammad Nasim
Are you okay if I add this to the CodePedia?
See ya,
bilderbikkel
: BLIMEY! GREAT TRICK! THANKS!
: Are you okay if I add this to the CodePedia?
: See ya,
: bilderbikkel
:
:
sure, do it even if you don't mention my name, and for any trick you see valuable
i will be proud if you make that, i like codepedia, it is very useful
I put it at http://www.codepedia.com/1/CppVclComponentCreateDynamic
Thanks and see you later,
bilderbikkel
yeah, really i didn't think how they programmed MineSweeper
it will be so sad if you put them on the form then hide them from the scene. thanx for this example
:
:
I agree, but how they could handle each button's click-event ?..!
remember that, it gives the user the ability to create his own minesweeper size, so no previously known number of buttons to handle each one click-event.
u used :-) to mention minesweeper, let me use :-( it is not that easy.
Mohammad Nasim
Easy, assign an OnClick Event handler to each Button.
In this example, I create 10x10 TButtons in the TForm's constructor. To each Button I assign DynamicButtonClick as the Event handler. Every Button can be identified as each on has its own Tag.
[code]
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
const int maxx = 10;
const int maxy = 10;
for (int i=0, y=0; y!=maxy; ++y)
{
for (int x=0; x!=maxx; ++x, ++i)
{
TButton * myButton = new TButton(this);
myButton->Left = x * 20;
myButton->Top = y * 20;
myButton->Tag = i;
myButton->Height = 20;
myButton->Width = 20;
myButton->OnClick = DynamicButtonClick;
this->InsertControl(myButton);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DynamicButtonClick(TObject *Sender)
{
TWinControl * control = dynamic_cast(Sender);
ShowMessage(control->Tag);
}
//---------------------------------------------------------------------------
[/code]
See ya later,
bilderbikkel
: : I agree, but how they could handle each button's click-event ?..!
: Easy, assign an OnClick Event handler to each Button.
:
: In this example, I create 10x10 TButtons in the TForm's constructor. To each Button I assign DynamicButtonClick as the Event handler. Every Button can be identified as each on has its own Tag.
Thanx man
Mohammad Nasim
Thank you! This has been the first time I learned something from a forum *cool smiley*.
See ya,
bilderbikkel
: Thank you! This has been the first time I learned something from a forum *cool smiley*.
:
: See ya,
: bilderbikkel
:
:
hahaha
but we all here leearn from you
Mohammad Nasim
I have another solution to your problem.
Ingvar
: [code]
: void __fastcall TForm1::FormCreate(TObject *Sender)
: {
: TComboBox *cbStdName = new TComboBox(this);
: cbStdName->Parent = this; <---- insert this line telling where to place it.
: cbStdName->Top = 115;
: cbStdName->Left = 195;
: cbStdName->Width = 65;
: cbStdName->Height = 13;
: cbStdName->Visible = true;
: }
: [/code]
:
: I have another solution to your problem.
: Ingvar
great solution
and borland suggests to use this coding style to move component from container (groupbox1) to another (groupbox2) ... see help.
this implicitly removes the component from the first container.
thanx for suggestion,
and sorry for late answer, i was very busy last days
M-Nasim
what's wrong?
[code]cbStdName->Parent = this; <---- insert this line telling where to place it.[/code]
I always use this solution because it is good-looking.:)