A clear and simple way to convert a string to integer in C++/CX

Translate This Page

A clear and simple way to convert a string to integer in C++/CX

Rate This
  • Comments 3

Oh my.  No one really cares about this, but it has just annoyed the heck out of me that there is clear cut instructions on how to use the text in a textbox in XAML with C++/CX.  And this blog is just a place for me to put my personal instructions on how to convert the XAML textbox text when using C++/CX. 

Nothing against the existing documentation, let’s face it, there are bigger fish to fry then just how to use user input in C++/CX, after all we haven’t really thought about that in the past.  There are many ways to do any one thing in C++ and then on top of that you have the C++/CX innovations (or for the ISO C++ programmers, other comments can be inserted at this point).  Add on top of that of years of different approaches, I just don’t know what is the most efficient.

Download the code from:

http://code.msdn.microsoft.com/Super-Simple-XAMLCCX-0052e692

 

Let’s get started:

XAML Adds

In the case of XAML add the following grid, as usual your project name will vary, etc.  Now XAML is much more interesting than what I show in my blogs, so read Jerry Nixon’s blog for more on XAML.

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="2">
        <Button x:Name="btnMultiply" 
                Content="Touch to multiply" 
                HorizontalAlignment="Left" VerticalAlignment="Top" 
                Height="135" Width="446"
                Tapped="ExampleTapped" />
        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" 
                   Height="54" Margin="10,169,0,0" Width="260" 
                   TextWrapping="Wrap" Text="Type in a number less than 1000" 
                   FontFamily="Global User Interface"
                   FontSize="20"/>
        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"
                   Height="54" Margin="10,271,0,0" Width="260"
                   TextWrapping="Wrap" Text="Click button and number is multiplied by two" 
                   FontFamily="Global User Interface"
                   FontSize="20"/>
        <TextBox x:Name="txtNumberToMultiply" 
                 HorizontalAlignment="Left" VerticalAlignment="Top" 
                 Height="131" Margin="275,135,0,0" Width="171"
                 Text="" TextWrapping="Wrap"  />
        <TextBox x:Name="txtNumberAfterMultiplied" 
                 HorizontalAlignment="Left"  VerticalAlignment="Top" 
                 Height="131" Margin="275,271,0,0" Width="171"
                 TextWrapping="Wrap" Text=""/>
        
    </Grid>

 

PCH.H Adds

In you pch.h file add the following include statements (I also show the existing ones):

//
// pch.h
// Header for standard system include files.
//
 
//Following lines is included with the blank template
#pragma once
#include <collection.h>
#include "App.xaml.h"
//The following lines are added by you
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <iostream> 
#include <sstream> 
//End of the lines included by you

 

Global scope on the MainPage.XAML.cpp

Make sure to add the following using statement:

using namespace std;

Tapped Event

In my tapped event, yours will be different, or mine in the unknown and unknowable future, look like the following.  If you think getting to this point was easy, it wasn’t, I was trying to keep this simple and reproducible.  If this is not the most efficient please send me an email with the more efficient code.  Finally, this should be made into a class or a function, but I didn’t do so just to keep this really simple.

 

 

void MultiplyByTwo::MainPage::ExampleTapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
    //Use the Platform string to set up a shorter easy to use version of the string out of the textbox
    //This will allow the use the std library wstring object
    String^ str1 = txtNumberToMultiply->Text;
    //Convert the the String^ object to a string that can easily be manipulated 
    //No test for valid data in this blog, could be a later post
    wstring ws1( str1->Data());
    // Create a wstringstream object to convert the wstring to integer
    wstringstream convertor;
    //create a integer, to hold the coverted variable
    int ws1_int;
    //Convertor object can do a bunch of stuff so take a look at how it works, 
    //but keeping things simple
    convertor << ws1;
    convertor >> ws1_int;
    //Do some work, in this case multiple by two
    ws1_int = ws1_int * 2.0;
    //Convert the int back to string
    txtNumberAfterMultiplied->Text = ws1_int.ToString();
}

Run your project

Your project should run and look something like the following, if you type in 444. 

image

Troubleshooting:

  • Did you add the following line:

using namespace std;

If you don’t, then the wstring object won’t be available

Leave a Comment
  • Please add 4 and 1 and type the answer here:
  • Post
Page 1 of 1 (3 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them