Jump to content

Textfields not accepting UMLAUT (ÄÜÖ) when using SetText()


buzzdx
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

 

Link to comment
Share on other sites

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:

 

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

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 ^^

Link to comment
Share on other sites

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.

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...