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>
void MultiplyByTwo::MainPage::ExampleTapped(Platform::Object^ sender,
Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
String^ str1 = txtNumberToMultiply->Text;
wstring ws1( str1->Data());
wstringstream convertor;
int ws1_int;
convertor << ws1;
convertor >> ws1_int;
ws1_int = ws1_int * 2.0;
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.