Get label wxTextCtrl

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
wxProgrammer
Experienced Solver
Experienced Solver
Posts: 96
Joined: Thu Apr 17, 2014 10:10 am

Get label wxTextCtrl

Post by wxProgrammer »

Hi guys,
I need to create a simple dialog with some textCtrl and "save" button; a simple edit-dialog. I've already do this but now it don't work; when I try to get the label of textctrl (GetLabelText() or GetLabel()), the string returned is always the default string that I put on the constructor of the textctrl. This is the essential code:

P.S. cBook is a clas that contains 3 wxString and 1 eBookTypes (that is an enum) and it work perfectly.

Constructor:

Code: Select all

cEditBookDialog::cEditBookDialog(cBook* pBook, wxWindow* parent, wxString caption)
	: wxDialog(parent, -1, caption, wxDefaultPosition, wxSize(350, 370)), pBook(book), sFilePath("")
{
	wxBoxSizer*		sizer	= new wxBoxSizer(wxVERTICAL);
	wxScrolledWindow*	scroll	= new wxScrolledWindow(this, -1, wxDefaultPosition, this -> GetSize());
	
	this -> CenterOnScreen();

	wxStaticText**	vTitles = new wxStaticText*[2];

	vTitles[0] = new wxStaticText(scroll, -1, "Name:");
	vTitles[1] = new wxStaticText(scroll, -1, "Info:");

	pName		= new wxTextCtrl	(scroll, -1, pBook -> GetName(),	wxDefaultPosition, wxSize(this -> GetSize().x - 30, -1));
	pComment	= new wxTextCtrl	(scroll, -1, pBook -> GetComment(),	wxDefaultPosition, wxSize(this -> GetSize().x - 30, 70), wxTE_MULTILINE);

	pName		-> SetMaxLength(100);
	pComment	-> SetMaxLength(65535);

	wxBoxSizer** subsizers = new wxBoxSizer*[3];

	for (int i(0); i < 2; ++i)
	{
		vTitles[i] -> Wrap(this -> GetSize().x - 30);
		subsizers[i] = new wxBoxSizer(wxVERTICAL);
		subsizers[i] -> Add(vTitles[i], 0, wxALL, 5);
	}

	subsizers[0] -> Add(pName,		0, wxALL, 5);
	subsizers[1] -> Add(pComment,	0, wxALL, 5);
	subsizers[2] -> Add(new wxButton	(scroll, eebdID_SELECT_DOCUMENT, "Select"), 0, wxALL, 5);

	for (int i(0); i < 3; ++i)
	{
		sizer -> Add(subsizers[i], 0, wxALL, 5);
		sizer -> AddSpacer(20);
	}

	wxBoxSizer* actions = new wxBoxSizer(wxHORIZONTAL);

	actions -> Add(new wxButton(scroll, eebdID_SAVE, "Save"), wxSizerFlags().Left());
	actions -> AddSpacer(80);
	actions -> Add(new wxButton(scroll, eebdID_CANCEL, "Cancel"), wxSizerFlags().Right());

	sizer -> Add(actions, 0, wxALL, 9);

	scroll	-> SetSizer(sizer);
	scroll	-> SetAutoLayout(true);
	scroll	-> FitInside();
	scroll	-> SetScrollRate(20, 20);
	scroll	-> SetSize(sizer -> GetSize() + wxSize(20, 50));
	this	-> SetSize(scroll -> GetSize());
}
Button event:

Code: Select all

void cEditBookDialog::OnSaveDocument(wxCommandEvent& WXUNUSED(event))
{
	wxString name, comment;
	name	= pName -> GetLabelText();
	comment = pComment -> GetLabelText();

	if (name.IsEmpty())
	{
		wxMessageBox("Insert name!", "Attenzione", wxICON_EXCLAMATION | wxOK);
		return;
	}

	if (comment.IsEmpty())
	{
		wxMessageBox("Insert comment!", "Attenzione", wxICON_EXCLAMATION | wxOK);
		return;
	}

	pBook -> SetName	(name);
	pBook -> SetComment	(comment);

	this -> EndModal(wxID_OK);
}
name and comment are always equal to the initial label of textCtrl (so pBook -> GetName() and pBook -> GetComment())

I don't find the problem. How can I solve it?
Thank you
I'm Italian but we can speak C++ :)
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: Get label wxTextCtrl

Post by xaviou »

Hi.

Just replace "GetLabelText" by "GetValue" and it should work.

Regards
Xav'
My wxWidgets stuff web page : X@v's wxStuff
wxProgrammer
Experienced Solver
Experienced Solver
Posts: 96
Joined: Thu Apr 17, 2014 10:10 am

Re: Get label wxTextCtrl

Post by wxProgrammer »

Work, thank you :)
I'm Italian but we can speak C++ :)
Post Reply