Conversion to String - C++ Forms App

Jun 11, 2010 at 10:52pm
I am trying to program a game in a standard windows forms application in C++. I am having trouble which obviously wasn't an issue in VB. Basically it is a version of the countdown numbers game, and part of it is a random number is generated.

My problem is getting the textbox to show the random number as it will not convert to string. So far I have.

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

int target;

target=rnd();
textBox1->Text=target;

}

I won't even get to random seeding until I sort this problem out.

I tried things like ToString and so on, all to no avail.
Jun 11, 2010 at 11:16pm
Jun 11, 2010 at 11:34pm
You must first do this:
http://www.cplusplus.com/articles/numb_to_text/

NOTE: MAKE SURE TO USE WIDE THINGS BECAUSE WINDOWS FORMS WANTS WIDE CHARACTERS

Then, once you have an std::wstring, you can do this to convert it to a CLI string:
String ^systemstring = gcnew String(your_wstring.c_str());

FYI: I think you can delete your post/topic as long as no one has replied to it.
Last edited on Jun 11, 2010 at 11:35pm
Jun 12, 2010 at 9:36pm
Thanks for those links. Unfortunately none worked. Perhaps I should have said I am using Visual C++ Express 2008. The VB equlivalent would be textbox1.text=str(number) (number already declared and either valued or calculated). Obviously Dev C++ and equivalent in Visual is easy, but actually doing forms in C++ really complicates things.
Jun 12, 2010 at 9:40pm
Hm, this I the code I have recently used (tested and it works):

1
2
3
4
5
6
template <typename T>
std::wstring toWString(const T& t) { //conversion to wstring
	std::wstringstream str;
	str<<t;
	return str.str();
}


1
2
3
System::String^ WStringToSysString(const std::wstring str){
	return gcnew System::String(str.c_str());
}


And I used them like so:

1
2
int my_int = 0;
System::String^ str = WStringToSysString(toWString(my_int));


Does this work?
Last edited on Jun 12, 2010 at 9:41pm
Jun 13, 2010 at 11:38am
Does this go in the button code or somewhere in the source code files, or does it not matter?
Topic archived. No new replies allowed.