wxwidgets TextCtrl eingabe speichern!



  • Hallo,
    Wenn ich die eingabe als Wert speichern möchte geht das nicht !

    string zahl = textctrl->GetValue();
       string zahl2 = textctrl2->GetValue();
       string summe = zahl + zahl2;
       statictext->SetLabel(summe);
    

    Wenn ich jetzt 1 und 2 eingebe kommt als Lösung 12 und nicht 3 ?
    Und wenn ich die Variablen als integer speicher kommt dieser Fehler :

    C:\Users\AirTrake\Documents\wxwidgets\base.cpp||In member function 'void BasicFrame::OnClickButtonOK(wxCommandEvent&)':|
    C:\Users\AirTrake\Documents\wxwidgets\base.cpp|65|error: invalid conversion from 'const wxChar*' to 'int'|
    C:\Users\AirTrake\Documents\wxwidgets\base.cpp|66|error: invalid conversion from 'const wxChar*' to 'int'|
    C:\CodeBlocks\wxWidgets-2.8.12\include\wx\string.h|682|error: 'wxString::wxString(int)' is private|
    C:\Users\AirTrake\Documents\wxwidgets\base.cpp|68|error: within this context|
    ||=== Build finished: 4 errors, 0 warnings ===|
    

    Mit Freundlichen Grüssen



  • du kannst nicht mit strings rechnen. du musst sie davor umrechnen:

    #include <sstream>
    
    int to_int(const std::string& value)
    {
      std::stringstream ss;
      ss << value;
    
      int ret_val;
      ss >> ret_val;
      return ret_val;
    }
    
    std::string to_string(int value)
    {
      std::stringstream ss;
      ss << value;
    
      std::string ret_val;
      ss >> ret_val;
      return ret_val;
    }
    
    int zahl = to_int(textctrl->GetValue());
       string summe = to_string(zahl + zahl);
       statictext->SetLabel(summe);
    

    wie du siehst, ändern sich nur die typen, der rest bleibt, also kannst du auch ein template draus machen:

    #include <sstream>
    
    template <typename OUT, typename IN>
    OUT convert(const IN& value)
    {
      std::stringstream ss;
      ss << value;
    
      OUT ret_val;
      ss >> ret_val;
      return ret_val;
    }
    
    int zahl = convert<int>(textctrl->GetValue());
       string summe = convert<std::string>(zahl + zahl);
       statictext->SetLabel(summe);
    

    bb



  • So muss es sein !
    Es klappt ich bedanke mich sehr !

    Mfg


Anmelden zum Antworten