5b-4. wxWidgets – FlexGridSizer

OVERVIEW

The difference between wxGridSizer and wxFlexGridSizer is that in wxFlexGridSizer, rows can have different heights and columns can have different widths. For eg. in a 4 x 4 grid, row 1 may be given a separate height as compared to the other rows and column 3 may be given a separate width as compared to the other columns.

The arguments used to create a wxFlexGridSizer are (rows, columns, vertical spacing, horizontal spacing)

In the sample code below, we setup a dialog with labels on the left and text boxes on the right. The last textbox is a multi-line textbox so it stretches down the rest of the window.

SAMPLE CODE

wxflexgridsizer.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif

class FlexGridSizer: public wxFrame {
	public:
		FlexGridSizer(const wxString &title);	
};
wxflexgidsizer.cpp
#include "wxflexgridsizer.h"

FlexGridSizer::FlexGridSizer(const wxString &title):
	wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,250)) {

	wxPanel *basePanel = new wxPanel(this,-1);
	wxFlexGridSizer *fgs = new wxFlexGridSizer(4,2,3,10);
	basePanel->SetSizer(fgs);

	wxStaticText *lblA1=  new wxStaticText(basePanel, -1, wxT("Answer 1"));
	wxStaticText *lblA2=  new wxStaticText(basePanel, -1, wxT("Answer 2"));
	wxStaticText *lblA3=  new wxStaticText(basePanel, -1, wxT("Answer 3"));
	wxStaticText *lblA4=  new wxStaticText(basePanel, -1, wxT("Answer 4"));


	wxTextCtrl *txtA1=  new wxTextCtrl(basePanel, -1, wxT(""));
	wxTextCtrl *txtA2=  new wxTextCtrl(basePanel, -1, wxT(""));
	wxTextCtrl *txtA3=  new wxTextCtrl(basePanel, -1, wxT(""));
	wxTextCtrl *txtA4=  new wxTextCtrl(basePanel, -1, wxT(""), wxPoint(-1,-1), wxSize(-1,-1), wxTE_MULTILINE);

	fgs->Add(lblA1); fgs->Add(txtA1, 1, wxEXPAND);
	fgs->Add(lblA2); fgs->Add(txtA2, 1, wxEXPAND);
	fgs->Add(lblA3); fgs->Add(txtA3, 1, wxEXPAND);
	fgs->Add(lblA4); fgs->Add(txtA4, 1, wxEXPAND);

	fgs->AddGrowableRow(3, 1);
	fgs->AddGrowableCol(1,1);


	Center();
}

The output is given below

Be the first to comment

Leave a Reply

Your email address will not be published.


*