Search code examples
c++wxwidgetscodeblocks

How to convert a wxString to Int for using arithmetic operations?


I am new to Wxsmith and C++ I have created a basic UI with 2 text boxes, a static-text and a button. The question is how do i add the 2 values entered in the two text boxes and display it in the static-text when the button is clicked ? In visual Basic all you had to do was :

variable = Val(textbox1.text)

Solution

  • I'm not familiar with wxwidgets, but wxString has a ToLong member function: http://docs.wxwidgets.org/stable/wx_wxstring.html#wxstringtolong

    It seems this attempts to convert the string to a long and store it in a location provided, returning true or false to indicate whether it succeeded or failed.

    // Created a string up here somewhere
    long converted;
    if ( myString.ToLong (&converted) )
    { /* Do something with the number */ }
    else
    { /* It wasn't a valid number */ }
    

    I note there doesn't seem to be a version for converting directly to int.