How to receive event when user changes system's culture
Asked Answered
B

2

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.

Bonbon answered 17/1, 2011 at 9:17 Comment(0)
D
14

You can handle the SystemEvents.UserPreferenceChanged event:

void SystemEvents.UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
    // Regional settings have changed
    if (e.Category == UserPreferenceCategory.Locale)
    {
        ...
    }
}
Disillusion answered 17/1, 2011 at 9:22 Comment(5)
Thanks. But how to get new regional settings?Bonbon
I solve my problem with the code: CultureInfo.CurrentCulture.ClearCachedData();Bonbon
@Lu Lu, I didn't know about that method... learn something every day ;)Disillusion
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.Bonbon
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. #1371033Volnak
M
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;
Multiplicate answered 17/1, 2011 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.