• Advertisement
Sign in to follow this  

Preventing input language change in .net forms

This topic is 2257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I've been having another small problem with localizing a .net program.
The way my localization works is that it only changes the UI language, but the input still needs to always be English, but I have a few hundreds of input boxes in the program, and I don't want to go over each one and add exception checks for other languages, so I tried making a background worker that continuously checks if your input language is anything other than English while the program is running, and if not, set it back.
But I'm getting some weird results from it here.

//Makes sure that the input language is always English
private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
while(0 == 0){
String^ lang = InputLanguage::CurrentInputLanguage->
Culture->EnglishName;
if(lang->IndexOf("English") == -1){
InputLanguage^ il = InputLanguage::FromCulture(
gcnew CultureInfo("en"));
InputLanguage::CurrentInputLanguage::set(il);
}
}
}


This is C++ .NET btw, sorry if the syntax might be a little off for you C# and VB programmers.

Now, this works in detecting the language change, and the used actions are taken from a MSDN example for this purpose, but when the condition is reached, the next time the loop is ran, the input language detected is English, but when I write inside the program, and by looking on the language bar, the language is still the one changed to that isn't English.
I'm not really sure what to do here.

P.s. Also, another small issue, since this is on a different thread(Again, the thread is needed so I don't need to add a continuous check on every form in the program), I'm not really sure in checking, since it's not working yet, will this method prevent the user from changing the language on any program while this is still open? Should I set another operating condition that'll allow it only when this program has focus?

Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Hi!

Forms have an InputLayoutChanged event. I tried it in C# and it worked fine. So, no need for a background worker here. I tested switching the language to something other than English. When clicking in a text box on my form, it snapped right back to English in the language bar. This is what you want, right? (Sorry, my sample is in C#.)

private void Form1_Load(object sender, EventArgs e)
{
// initially set to English.
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("en"));
// whenever it changes, set back to English.
InputLanguageChanged += new InputLanguageChangedEventHandler(Form1_InputLanguageChanged);
}

void Form1_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)
{
// if currently not set to English -> set to English.
if (!InputLanguage.CurrentInputLanguage.Culture.EnglishName.Contains("English"))
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("en"));
}

Share this post


Link to post
Share on other sites
And that works for you?
Mine just keeps writing in the other language, doesn't go back to English, even when debugging shows its' been triggered.

Share this post


Link to post
Share on other sites
I just tried it in C++/CLI and it turned out the CultureInfo needed to be “en-US”.
Simply using “en” didn’t work (although it did in C#... strange…).

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
InputLanguage^ il = InputLanguage::FromCulture(
gcnew CultureInfo("en-US"));
InputLanguage::CurrentInputLanguage::set(il);
}

private: System::Void Form1_InputLanguageChanged(System::Object^ sender, System::Windows::Forms::InputLanguageChangedEventArgs^ e) {
String^ lang = InputLanguage::CurrentInputLanguage->Culture->EnglishName;
if(lang->IndexOf("English") == -1){
InputLanguage^ il = InputLanguage::FromCulture(
gcnew CultureInfo("en-US"));
InputLanguage::CurrentInputLanguage::set(il);
}
}

Share this post


Link to post
Share on other sites
Perfect, thanks.

Kind of weird though, I didn't need to use the full culture representation for localizing the forms ._.

Thanks alot!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement