wxWidgets: wxTextCtrl nur Zahleneingabe



  • Hallo,

    ich habe nochmal eine Frage zum wxTextCtrl. Und zwar möchte ich, dass das wxTextCtrl nur Zahlen akzeptiert. (Eventuell noch einen Dezimalpunkt (oder -Komma)). Buchstaben und sonstige Zeichen sollen gar nicht akzeptiert werden.

    Kann ich die Einagbe in mein wxTextCtrl irgendwie filtern oder muss ich mir eine neue Klasse schreiben (ableiten)?

    Gruß,
    Sebastian



  • Leite wxTextCtrl ab und filter das OnChar ereigniss.

    So: http://www.wxwidgets.org/manuals/2.6.2/wx_eventhandlingoverview.html#eventprocessing


  • Mod



  • Danke,

    schau ich mir beides mal an.

    Ich tendiere inzwischen doch zum ableiten, weil ich dann flexibler bin. Aber mal sehen.

    Gruß,
    Sebastian



  • Hallo,

    sorry, ich muss nochmal nerven 😉 Dieser ganze OOP-Kram ist eben neu für mich. Ich weiß, diese Frage passt evtl. besser ins C++-Forum, aber ich stelle sie hier, ist ja auch mit wxWidgets und meinem obrigen Problem verknüpft.

    Ich habe versucht, eine Klasse wxNumberCtrl von wxTextCtrl abzuleiten. Ich bekomme aber immer einen Copiler-Error. Irgendwas mach ich falsch, weiß aber nicht, was:

    wxNumberCtrl.h:

    class wxNumberCtrl: public wxTextCtrl
    {
    
        public:
            wxNumberCtrl( );
    
            wxNumberCtrl(wxWindow* parent, wxWindowID id, const int value = 0, 
                const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, 
                long style = 0, const wxValidator& validator = wxDefaultValidator, 
                const wxString& idname = wxTextCtrlNameStr);
    
            /// Creation
            bool Create(wxWindow* parent, wxWindowID id, const int value = 0, 
                const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, 
                long style = 0, const wxValidator& validator = wxDefaultValidator, 
                const wxString& idname = wxTextCtrlNameStr);
    }
    

    wxNumberCtrl.cpp:

    wxNumberCtrl::wxNumberCtrl()
    {}
    
    wxNumberCtrl::wxNumberCtrl(wxWindow* parent, wxWindowID id, const int value, 
                const wxPoint& pos, const wxSize& size, 
                long style, const wxValidator& validator, 
                const wxString& idname)
    {
        Create( parent, id, value, pos, size, style, validator, idname);
    }
    
    bool wxNumberCtrl::Create(wxWindow* parent, wxWindowID id, const int value, 
                const wxPoint& pos, const wxSize& size, 
                long style, const wxValidator& validator, 
                const wxString& idname)
    {
        wxString s;
        s.Printf("%i",value);
        if (!wxTextCtrl::Create(parent, id, s, pos, size, style, validator, idname))
            return false;
        else
            return true;
    }
    

    Natürlich habe ich alles, was ich brauche, includiert, aber so ists übersichtlicher hier.
    Ich bekomme folgenden Fehler (BCC, andere Compiler entsprechend)

    ERROR E2111 wxNumberCtrl.cpp 6: Type 'wxNumberCtrl' may not be defined here
    ERROR E2136 wxNumberCtrl.cpp 6: Constructor cannot have a return type specification
    

    Aber der Constructor hat doch auch keinen return type?!? Oder was habe ich falsch verstanden?

    Wenn ich mit MingW oder VCC compiliere, bekomme ich übrigens nur den zweiten Fehler.

    😞

    Gruß,
    Sebastian



  • Mist, ich habs gefunden. War ein fehlendes ";" im header!

    Sebastian


  • Mod

    Geht viel einfacher mit einem wxTextValidator:

    someTextCtrl->SetValidator( wxTextValidator(wxFILTER_NUMERIC));
    


  • Arrgh. Ich dachte, ich könnte wxValidator nur nehmen, wenn ich das wxTextCtrl gleichzeitig mit einer Variablen verknüpfe. Und das wollte ich nicht. Naja, jetzt bleibt es so. Ich kann jetzt nämlich auch "," als "." verarbeiten, GetInt() ist implementiert, GetDouble() wird folgen, und ich kann, wenn ich will, das Glockenklingeln abschalten.

    Aber einfacher wärs schon gewesen 🙄

    Danke.
    Sebastian


Anmelden zum Antworten