Creating a button during runtime Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Sheeze
In need of some credit
In need of some credit
Posts: 5
Joined: Thu Feb 05, 2009 2:56 am

Creating a button during runtime

Post by Sheeze » Thu Feb 12, 2009 6:15 pm

My program contains a button which when clicked calls a function to create another button in a sizer named m_input. My problem is when the button is not being created in m_input but simply at the top-left corner of the program (seemingly outside of any sizer). My top window is a wxDialog. Here's the code for the function:

Code: Select all

void myFrame::OnClick( wxCommandEvent& event )
{
    wxButton* testButton = new wxButton( this, ID_Button, _("Button"), wxDefaultPosition, wxDefaultSize, 0 );
    m_input->Add(testButton, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5);
}
And the code for the original control creation, which works fine:

Code: Select all

    ...
    wxDialog *Dialog1 = this;

    wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
    Dialog1->SetSizer(sizer);

    wxStaticBox staticSizer = new wxStaticBox(Dialog1, wxID_ANY, _("Test Sizer"));
    // m_input has already been declared as a member variable somewhere so that the myFrame class functions can access it
    m_input = new wxStaticBoxSizer(staticSizer, wxVERTICAL);
    sizer->Add(m_input, 0, wxGROW|wxALL, 5); 
    ...
What am I doing wrong here? Thanks!

User avatar
Disch
Experienced Solver
Experienced Solver
Posts: 99
Joined: Wed Oct 17, 2007 2:01 am

Post by Disch » Thu Feb 12, 2009 6:34 pm

Nothing is standing out to me as being obviously wrong... unless...

You're creating testButton with 'this' as a parent (where 'this' is of type 'myFrame') -- is that the right parent? The dialog should be the parent. Or is myFrame the dialog (in which case that's a somewhat misleading name :wink: ).

Only other thing I can think of would be to call Layout() and/or Fit() after adding the button so that the dialog reshapes to accomidate the new button.

Sheeze
In need of some credit
In need of some credit
Posts: 5
Joined: Thu Feb 05, 2009 2:56 am

Post by Sheeze » Thu Feb 12, 2009 7:51 pm

Yeah, myFrame is referring to the dialog (yeah, I should have named it better =) Adding this->Layout(); and this->Refresh() solved the problem. Thanks Disch!

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu Feb 12, 2009 7:55 pm

m_input seems to be a sizer inside "Dialog1", while you are creating the button with "MyFrame" as parent; there may be something wrong there

EDIT: you were faster than me ;) I still fail to see though how "myFrame", class name, can refer to a wxDialog instance.

Post Reply