Aligning Buttons Right and Static Text Left inside a Panel using wxBoxSizer 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
dermayank
Knows some wx things
Knows some wx things
Posts: 30
Joined: Fri Jan 22, 2021 8:56 am

Aligning Buttons Right and Static Text Left inside a Panel using wxBoxSizer

Post by dermayank »

I have a panel, that contains a static text and two buttons. Now, I want to align the text on the left of the screen and the buttons on the right side of the screen. For Aligning I'm using wxHorizontal and wxVertical BoxSizers.


Below is a code snippet of what I tried to do.

Code: Select all

    wxBoxSizer* vbs = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* hbs = new wxBoxSizer(wxHORIZONTAL);

    wxBoxSizer* ht1 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* vt1 = new wxBoxSizer(wxHORIZONTAL);
    wxStaticText* name = new wxStaticText(panel, wxID_ANY, "Name Of the Application");

    ht1->Add(name);
    vt1->Add(ht1,0,wxALIGN_RIGHT);

    wxButton* button1 = new wxButton(panel, wxID_ANY, "Button1", wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
    wxButton* button2 = new wxButton(panel, wxID_ANY, "Button2", wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
    
    hbs->Add(vt1,0);
    hbs->Add(button1, 0);
    hbs->Add(button2, 0);
    vbs->Add(hbs, 0, wxALL|wxALIGN_LEFT, 2);
    
    panel->SetSizer(vbs);

Please tell help me how can I align the text left and button right inside the same panel.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Aligning Buttons Right and Static Text Left inside a Panel using wxBoxSizer

Post by doublemax »

Code: Select all

    hbs->Add(vt1,0);
    hbs->AddStretchSpacer(1);       // new line
    hbs->Add(button1, 0);
    hbs->Add(button2, 0);
    vbs->Add(hbs, 0, wxALL|wxEXPAND, 2);  // changed line
Use the source, Luke!
Post Reply