Click here to Skip to main content
13,357,219 members (72,574 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 9 Oct 2012

Super simple XAML text arithmetic manipulation

, 9 Oct 2012
Rate this:
Please Sign up or sign in to vote.
How to perform arithmetic operations on XAML Text


How do you manipulate XAML User input text in Windows 8 C++/CX?


For the first time C++/CX has a modern way of interacting with the end user.  In the other Windows 8 Visual Studio Windows 8 Store app languages interaction with the user interface is easy.  Documentation on how to change the string output from the textbox object is difficult to figure out how to get a number out of the text.  This tip assumes that the user input is a number only.

Using the code

The code is pretty simple, create a blank XAML page and then add the controls as shown.  Then add a tapped event.  Add the code shown to it and you should have a way to manipulate the user input with arithmetic operations.  This is pretty critical, and it is likely that after the date of posting there will be a more efficient way to perform this operation, so make sure to see if there is a better way to do this if you are reading this after 8 Oct. 2012.

//XAML Code    
<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"

    <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"

    <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=""

//Make sure to add using namespace std; to the global scope area of the code.
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 = "Ha ha " + ws1_int.ToString();

Points of Interest

Nothing zany, rather a consolidation of my research on how to implement arithmetic manipulation with Text in XAML Textboxes was kind of documented all over the place.


Initial entry: 8 Oct 2012.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

sam stokes
Instructor / Trainer Microsoft
United States United States
Sam Stokes works for Microsoft as a technology evangelist and is focused on working with colleges, students and professors.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 9 Oct 2012
Article Copyright 2012 by sam stokes
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid