8

When my application run with a specified culture. Don't close the application, user changes system's culture, ex: change number decimal separator from "." to ",". How to my application can catch this event. Thanks.

Notes: C# 2.0, Windows Form.

Leo Vo
  • 9,980
  • 9
  • 56
  • 78

2 Answers2

14

You can handle the SystemEvents.UserPreferenceChanged event:

void SystemEvents.UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
    // Regional settings have changed
    if (e.Category == UserPreferenceCategory.Locale)
    {
        ...
    }
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Thanks. But how to get new regional settings? – Leo Vo Jan 17 '11 at 09:33
  • 4
    I solve my problem with the code: CultureInfo.CurrentCulture.ClearCachedData(); – Leo Vo Jan 17 '11 at 09:40
  • @Lu Lu, I didn't know about that method... learn something every day ;) – Thomas Levesque Jan 17 '11 at 09:47
  • 1
    When user change system's culture. The CultureInfo.CurrentCulture is old settings, you must call CultureInfo.CurrentCulture.ClearCachedData() to clear old settings and get new settings. – Leo Vo Jan 17 '11 at 10:24
  • 2
    I ran into a problem with ClearCachedDate and found this SO that solves the problem. I had to spin up a task and return the results of the cache clear. http://stackoverflow.com/questions/1370533/cultureinfo-clearcacheddata-does-not-work-it-randomly-sometimes-works-sometimes – Justin Pihony Oct 29 '13 at 19:42
3

If you want to trach system language changes then you have SystemEvents object which contains UserPreferenceChanged event you can attach to.

Sample:

Microsoft.Win32.SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);

If you want to track input language changes (like changes in system try when you choose between languages), then you can use: System.Windows.Forms.InputLanguage.CurrentInputLanguage

Sample:

string inputLanguage = System.Windows.Forms.InputLanguage.CurrentInputLanguage.LayoutName;
HABJAN
  • 9,212
  • 3
  • 35
  • 59