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
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
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.
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");