Putting float values in an editbox
Putting float values in an editbox
I have data stored in my classes as floats. I want to put this data into an edit box to allow it to be altered.
I have managed to get the data from the editbox into my class but I cant do it the other way round.
i know the editbox reqires a const wchar_t but I cant seem to get my floats into one.
Ive tried casting them to strings, and to ints, and even to ints then strings but I always get errors and it shows up in my editbox as squares not numbers.
If anyone can provide a quick example of how to convert a float into a const wchar_t for use in an editbox I would be very grateful.
Thanks
John
I have managed to get the data from the editbox into my class but I cant do it the other way round.
i know the editbox reqires a const wchar_t but I cant seem to get my floats into one.
Ive tried casting them to strings, and to ints, and even to ints then strings but I always get errors and it shows up in my editbox as squares not numbers.
If anyone can provide a quick example of how to convert a float into a const wchar_t for use in an editbox I would be very grateful.
Thanks
John
Code: Select all
// set scale
gui::IGUIElement* root = env->getRootGUIElement();
core::vector3df scale;
core::stringc s;
s = root->getElementFromId(901, true)->getText();
scale.X = (f32)atof(s.c_str());
s = root->getElementFromId(902, true)->getText();
scale.Y = (f32)atof(s.c_str());
s = root->getElementFromId(903, true)->getText();
scale.Z = (f32)atof(s.c_str());
if (Model)
Model->setScale(scale);
post some code and I'll make it work.
Well, if I got the original message correct the way from wchar_t to float was solved, but not the other way round. Should work with swprintf which lets you format a wide char string similar to sprintf for usual characters. Try
where wchar_buf is a previously allocated memory range and floatvar contains your floats. You can then add this string to the GUI elements.
Code: Select all
swprintf(wchar_buf, 256, "%f", floatvar)
Thanks for the help so far guys.
Yeah hybrid you got it right. I want to put floats in the editbox. I have already done it the other way round (by nicking the code from meshviewer).
I still cant get it to work though. I suspect I am not using the wchar_t properly.
Here is the test code:
It fails to compile with this error
: error C2664: '_snwprintf' : cannot convert parameter 3 from 'char [3]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
Thanks alot for the help
John
Yeah hybrid you got it right. I want to put floats in the editbox. I have already done it the other way round (by nicking the code from meshviewer).
I still cant get it to work though. I suspect I am not using the wchar_t properly.
Here is the test code:
Code: Select all
float floatvar = 12;
wchar_t *s = new wchar_t();
swprintf(s, 256, "%f", floatvar);
: error C2664: '_snwprintf' : cannot convert parameter 3 from 'char [3]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
Thanks alot for the help
John
just a guess, I've never user wchar_t before and I'm not in front of a compiler, but it looks to me like you need an array...
Code: Select all
float floatvar = 12;
wchar_t s[256];
swprintf(s, 255, "%f", floatvar);
Im getting the same problem trying to put a string into an editbox.
I gues im missing something about C++ string formatting.
If anyone can point me in the direction of a good online resource about this topic, I would be grateful.
Also if someone can please fix my code so it works, Im sure I would learn something that way too.
Thanks
John
I gues im missing something about C++ string formatting.
If anyone can point me in the direction of a good online resource about this topic, I would be grateful.
Also if someone can please fix my code so it works, Im sure I would learn something that way too.
Thanks
John