Super simple XAML text arithmetic manipulation





4.00/5 (1 vote)
How to perform arithmetic operations on XAML Text
Introduction
How do you manipulate XAML User input text in Windows 8 C++/CX?
Background
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"
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"
FontSize="40"/>
<TextBox x:Name="txtNumberAfterMultiplied"
HorizontalAlignment="Left" VerticalAlignment="Top"
Height="131" Margin="275,271,0,0" Width="171"
TextWrapping="Wrap" Text=""
FontSize="40"/>
</Grid>
//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.
History
Initial entry: 8 Oct 2012.