wxSpinCtrl - How to set max length of text?

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
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

wxSpinCtrl - How to set max length of text?

Post by Frank » Thu Aug 18, 2005 10:42 pm

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?

ssigala
Earned some good credits
Earned some good credits
Posts: 109
Joined: Fri Sep 03, 2004 9:30 am
Location: Brescia, Italy

Post by ssigala » Thu Aug 18, 2005 10:56 pm

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.
Sandro Sigala - Kynosoft, Brescia

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Fri Aug 19, 2005 12:42 am

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.

Bundy
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Apr 29, 2005 9:46 am
Location: Poland

Post by Bundy » Fri Aug 19, 2005 10:54 am

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())
"Fate rewards prepared mind"

Quote from movie "Liberator 2"

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Fri Aug 19, 2005 6:02 pm

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.

Post Reply