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:
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"/>
Height="54" Margin="10,271,0,0" Width="260"
TextWrapping="Wrap" Text="Click button and number is multiplied by two"
<TextBox x:Name="txtNumberToMultiply"
Height="131" Margin="275,135,0,0" Width="171"
Text="" TextWrapping="Wrap" />
<TextBox x:Name="txtNumberAfterMultiplied"
Height="131" Margin="275,271,0,0" Width="171"
TextWrapping="Wrap" Text=""/>
</Grid>
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
Make sure to add the following using statement:
using namespace std;
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();
}
Your project should run and look something like the following, if you type in 444.
using namespace std; If you don’t, then the wstring object won’t be available
If you don’t, then the wstring object won’t be available
Hi Surf4fun,
thanks for sharing this. not sure if I am missing something but I found this way to achieve the same result with an easier approach (or at least from my PoV)
kindly refer @ massimo-fascinari.com/.../simple-and-effective-way-to-convert-int-to-string-and-back-in-ccx-xaml-windows-store-app
Regards,
Massimo
Massimo,
Yes you are the simplest of all programmers. Umm, that came out wrong. You are the best at showing simple programming. Yes. Good job. Thank you.
However, my approach uses the std library. Not a good excuse but an excuse.
Sam
Now that we have C++11 we can just use std::to_string and, say, std::stoi:
en.cppreference.com/.../to_string
en.cppreference.com/.../stol
There's more (e.g., wstring support), see "Numeric conversions" here:
en.cppreference.com/.../basic_string
Legal Note:
Restrictions: