views:

307

answers:

2

Hi,

Does one of you know if its possible to override the CurrentCulture and the CurrentUICulture for a specific control in Winforms? So that this one specific controls uses a different culture?

tia Martin

+1  A: 

You can achieve that by instantiating the CultureInfo of your choice and pass that as a parameter to formatting functions (such as ToString). Just don't assign it to Thread.CurrentThread.CurrentCulture or Thread.CurrentThread.CurrentUICulture, since that will change the culture for the application as such.

In your code:

CultureInfo myCulture = CultureInfo.GetCultureInfo("en-US");
Console.WriteLine(DateTime.Now.ToString(myCulture));

If your code executes external code, and you want to force this code to use your internally chosen culture, you can do that by creating a new thread, assign the culture to that thread and then have that thread execute the code. Just make sure to pay attention to the threading issues that comes with that approach.

Thread sample:

string formattedDate = string.Empty;
Thread t = new Thread(delegate()
{
    // call external code without specifying culture
    formattedDate = DateTime.Now.ToString();  
    waitHandle.Set();
});
t.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
t.Start();
waitHandle.WaitOne(); // wait for the thread to finish
Console.WriteLine(formattedDate);

As a last point; I don't know why you want to override the CurrentCulture, but I would suggest that you think twice before doing so. Users are used to seeing dates an numbers formatted according to their locale; changing that could be confusing, especially if it happens in just one part of the UI.

Fredrik Mörk
A: 

I don't think you can do it for single control, but for the current thread you can switch like this:

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("DE");
DevComponents - Denis Basaric
Hi, I know how to set it for the entire thread, but this doesn't help me, as I need to change it for a special control on my form.
Martin Moser