sgbrown
sgbrown

Reputation: 473

wxWidgets Sizers: How to get sizers to behave properly on resize

I am currently learning to use wxWidgets in Microsoft Visual Studio C++, and I think that I have my head wrapped around the concept of Sizers. However, I can't seem to get them to behave how I want them to while resizing. I am following a tutorial (the link to which I seem to have misplaced, but it is pretty simple), which aims to create a dialog window with an expandable text box and two buttons beneath it which should stay approximately centered. My code for the window is below, found within the constructor of my dialog window:

vertSizer = new wxBoxSizer(wxVERTICAL); // Create vertical (parent) sizer, will contain text box and child sizer
vertSizer->Add(
    new wxTextCtrl(this, -1, "My text", wxDefaultPosition, wxSize(100,80), wxTE_MULTILINE),
    1, // vertically stretchable
    wxEXPAND | wxALL, // horizontally stretchable, borders all around
    10); // Add text box to parent sizer

horizSizer = new wxBoxSizer(wxHORIZONTAL); // Make child sizer, will contain buttons
wxSizerFlags ButtonFlags(1); // Make controls stretch hoizontally (to cover entire sizer area)
ButtonFlags.Expand().Center().Border(wxALL,10); // Make controls expand vertically, add border
horizSizer->Add(new wxButton(this,wxID_OK,"OK"), ButtonFlags); // Add first button
horizSizer->Add(new wxButton(this, ID_Cancel,"Cancel"), ButtonFlags); // Add second button

vertSizer->Add(horizSizer); // Add child sizer to parent sizer
SetSizerAndFit(vertSizer);

The displayed window looks correct (a large text box on the top portion of the window, with two properly spaced buttons, side-by-side, below it). However, when the window is resized, the text box expands both vertically and horizontally (expected behavior), but the buttons stay in place, left-aligned (unwanted behavior). (I apologize for not being able to post a picture; I have a couple prepared, but I can't post them with as little reputation as I have.)

The formatting on initialization great, however I would to maintain that formatting while resizing. Meaning, I want the two buttons beneath the text box to stay in the middle of the window, still side-by-side (although, it's acceptable for the distance between them to increase as the window gets bigger). Alternatively, I would like the buttons to increase in size horizontally, so that each button takes up half of the area beneath the text box.

I apologize if a similar question has already been asked, but I haven't found a satisfactory answer on this site in my research.

Any help with this would be greatly appreciated. Thank you!

Upvotes: 5

Views: 4340

Answers (2)

Buddy
Buddy

Reputation: 69

Not sure if this is what you're looking for but the key is making a sizer and adding the panel inside that you want to stretch in this format:

Sizer->Add(Panel, 1, wxEXPAND | wxALL);

It stretched the panel out with the width of the window (the key here is wxAll) as per the documentation. Hope this helps someone and future me when I forget

Upvotes: 1

Kristian Duske
Kristian Duske

Reputation: 1779

You need to tell the outer box sizer that it should expand the horizontal child sizer as well:

vertSizer->Add(horizSizer, 0, wxEXPAND);

The second parameter, 0, tells the vertical sizer to expand the child sizer only horizontally. For box sizers, passing 0 as the second parameter and wxEXPAND as (part of) the third will make sure that the added child control or sizer is expanded in the "other" direction, i.e., for a vertical sizer, the child is expanded horizontally whereas for a horizontal sizer, the child is expanded vertically.

Passing 1 as the second parameter will, as you already know from the text box, expand in both directions.

Upvotes: 3

Related Questions