Dynamic Component Allocation

[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

  • [b][red]This message was edited by luckyboy at 2007-3-16 7:43:12[/red][/b][hr]
    : [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 :p

    Mohammad Nasim


  • BLIMEY! GREAT TRICK! THANKS!
    Are you okay if I add this to the CodePedia?
    See ya,
    bilderbikkel

  • [b][red]This message was edited by luckyboy at 2007-3-16 9:7:18[/red][/b][hr]
    : 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

  • That's how they programmed Minesweeper, creating 100 TButtons dynamically :-) !

    I put it at http://www.codepedia.com/1/CppVclComponentCreateDynamic

    Thanks and see you later,
    bilderbikkel

  • : That's how they programmed Minesweeper, creating 100 TButtons dynamically :-) !

    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
  • : : That's how they programmed Minesweeper, creating 100 TButtons dynamically :-) !
    :
    :
    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
  • : 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.

    [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

  • [b][red]This message was edited by luckyboy at 2007-3-18 13:3:50[/red][/b][hr]
    : : 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

  • : Thanx man
    Thank you! This has been the first time I learned something from a forum *cool smiley*.

    See ya,
    bilderbikkel

  • : : Thanx man
    : 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
  • Hi M-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]
    :

  • : Hi M-Nasim
    : 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
  • I try to assigne event like this and it always wrote me error undefined symbol DynamicButtonClick and void __fastcall TForm1::DynamicButtonClick(TObject *Sender) is not member of 'Tform1'

    what's wrong?
  • Hi all

    [code]cbStdName->Parent = this; <---- insert this line telling where to place it.[/code]

    I always use this solution because it is good-looking.:)


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories