Page 1 of 1

wxSpinCtrl - How to set max length of text?

Posted: Thu Aug 18, 2005 10:42 pm
by Frank
I wan't the user to entry a number between 1 and 999. I wan't to do this with a wxSpinCtrl. How can I set the max text length, so that the user can enter a maximum of 3 characters?

In a wxTextCtrl I would use SetMaxLength(), but how do I do it in a wxSpinCtrl?

Posted: Thu Aug 18, 2005 10:56 pm
by ssigala

Code: Select all

wxSpinCtrl *ctrl;
// ...
ctrl->SetRange(1,999);
If the user enters a value greater than 999, the value will be clamped to 999 (and GetValue() will return 999).

If you want to force the maximum string size to 3, I think you have to edit the control code.

Posted: Fri Aug 19, 2005 12:42 am
by Frank
Yes, I know the SetRange-Thing. But I find it very counter-intuitive, when the user can type eg, 1500 and then without him knowing that's not allowed to truncate it without him realizing it.

I want to let him know, that he is only allowed to type in 3 chars. Before he presses the Okay-Button.

Since the SpinCtrl consists of a Textcontrol and a Spinbutton, I thougt there would be a possibility to use the SetMaxLength() function of the Textcontrol or get a pointer to the TextCtrl or something.

Posted: Fri Aug 19, 2005 10:54 am
by Bundy
You can use:

EVT_TEXT(ID_SPIN_CTRL, MyFrame::on_spin_change)

and function that will be called when user enter text into spinctrl:

Code: Select all

void MyFrame::on_spin_change(wxCommandEvent& event )
{
if(event.GetString().Length()>3)
{

wxMessageDialog dialog(this, wxString::Format(_("Enter a number from %d to %d"),0,999), _("Error"), wxICON_ERROR);
dialog.ShowModal();

}

event.Skip();
}
Now, when user put text, function check that this text is valid and show messega if not. You can also check that text is a number (wxString::IsNumber())

Posted: Fri Aug 19, 2005 6:02 pm
by Frank
That's not exacly what I want :)

I want the same behavior like wxTextCtrl::SetMaxLength().

Il wrote my own control now. Derived from wxSpinCtrl with the addition of SetMaxLength():

Code: Select all

void wxMySpinCtrl::SetMaxLength (int len)
{
   SendMessage((HWND)m_hwndBuddy, EM_SETLIMITTEXT, len, 0);
}
Not pretty, but works.