buzzdx Posted September 1, 2021 Share Posted September 1, 2021 Hi, I just tried to use the SetText method on a textfield with a value containing "ä". This results in an exception being thrown: std::range_error. The following code reproduces the error: auto txttarget = CreateTextField(515, 5, 400, 24, detailpanel); txttarget->SetText("ä"); It's the same for ö and ü. Am I doing something wrong or is this a bug? Quote Link to comment Share on other sites More sharing options...
Josh Posted September 2, 2021 Share Posted September 2, 2021 Try this: txttarget->SetText(L"ä"); Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
buzzdx Posted September 2, 2021 Author Share Posted September 2, 2021 So the problem is I'm not using the right char set? In my application the user enters something into a textbox. Then it gets stored into a string, gets modified and put back into the textfield, which is where the exception is thrown. Do I need to use wstring instead of string, or your WString class? I tried to convert the modified sting variable to your WString type like: txt->SetText(Wstring(stringvar)); but that didn't work either. Quote Link to comment Share on other sites More sharing options...
buzzdx Posted September 2, 2021 Author Share Posted September 2, 2021 So I got it working now. The problem was indeed that I was using std::string to work on the field data. Simply converting it by creating a new WString from the string then did not work either. The solution is to just use wstring and not string to get and modify the data and finally update the data in the textfield. If you, for some reason, have to use string somewhere in the code and need to convert it to wstring you can use this code: std::wstring S2WS(const std::string& s) { int len; int slength = (int)s.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len); std::wstring r(buf); delete[] buf; return r; } Quote Link to comment Share on other sites More sharing options...
Josh Posted September 3, 2021 Share Posted September 3, 2021 There's no reason to ever not use std::wstring / WString. std::string lacks the capacity for funny characters. You can convert a WString to UTF8 and back, but as I have explained earlier, UTF8 is evil and all of its proponents should be burned at the stake for heresy and crimes against humanity: Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
buzzdx Posted September 3, 2021 Author Share Posted September 3, 2021 Well, to be honest I never had this problem before and have been working with std::string like .. forever basically. I'm not sure what you mean by funny chars. If you mean ä, ü, ö etc., it usually works just fine in my other applications which use wxWidgets for UI and also databases like SQLite or MySQL. If you mean like smileys or something, then yeah, std::string can't use that. Anyway, problem solved, thanks ? While writing this I read the post you linked xD Nicely written ^^ Quote Link to comment Share on other sites More sharing options...
Josh Posted September 4, 2021 Share Posted September 4, 2021 You're probably using UTF-8 strings, which are basically a compressed string format. The problem is that common operations like search and replace do not work very well since you can't predict the byte position of a character in the string without reading the entire string up to that character. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.