Question

I create a simple window application with one button

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol);
    }
}
  • First time I click "button 1", it show "$"
  • I open control panel > Region and Language to change currency symbol from "$" to "€"
  • Click "button 1" again, it also show "$" ==> my expected result is "€"

It seem that CultureInfo.CurrentCulture does not change at run time. Can anyone provide me other var to get current window currency format

Était-ce utile?

La solution

Run this first:

CultureInfo.CurrentCulture.ClearCachedData();

Then do your messagebox.

I'm just curious as to why you need live updates from the culture info? I think the average user would set that once in Windows, and load their applications.

Autres conseils

Edit: Detach the event handler before the program exits. I changed the code to reflect this necessity. See the documentation for UserPreferenceChanged.

You can listen for the culture change. Register to Microsoft.Win32.SystemEvents.UserPreferenceChanged and do what you need.

public partial class Form1 : Form {
  public Form1() {
    InitializeComponent();
    SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
  }

  void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) {
    if (e.Category == UserPreferenceCategory.Locale) { 
      CultureInfo.CurrentCulture.ClearCachedData(); 
    }
  }

  private void button1_Click(object sender, EventArgs e) {
    MessageBox.Show(CultureInfo.CurrentUICulture.NumberFormat.CurrencySymbol); 
  }

  private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
    SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged;
  }
}

CultureInfo Documentation

SystemEvents Documentation

Culture is set per thread. The main thread is created when the application starts, hence it doesn't change later when you change your OS settings while the application still runs.

From MSDN documentation on CultureInfo class.

Culture and Threads

When a new application thread is started, its current culture and current UI culture are defined by the current system culture, and not by the current thread culture. The following example illustrates the difference. It sets the current culture and current UI culture of an application thread to the French (France) culture (fr-FR). If the current culture is already fr-FR, the example sets it to the English (United States) culture (en-US). It displays three random numbers as currency values and then creates a new thread, which, in turn, displays three more random numbers as currency values. But as the output from the example shows, the currency values displayed by the new thread do not reflect the formatting conventions of the French (France) culture, unlike the output from the main application thread.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top