Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CultureInfo

Posted on 2009-05-15
21
Medium Priority
?
444 Views
Last Modified: 2013-11-26
How to change current culture from windows application?
0
Comment
Question by:Rajeshk_cgm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
21 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24394394
The culture for your application or the culture for the whoel system? For 50p, I go for the application:

// change culture to english/us:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

Open in new window

0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24394416
how long will this changed culture last? till my application is closed?
 will it directly change the windows culture? or applicable only for my application?
0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24394447
if i need to change the whole system's culture means what should i do?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 39

Expert Comment

by:abel
ID: 24394703
> how long will this changed culture last? till my application is closed?
yes, from the moment you set it till the moment your thread ends or you set it differently

> will it directly change the windows culture? or applicable only for my application?
only for your app.

> if i need to change the whole system's culture means what should i do?
sorry, but that's a (very) difficult question to answer. You'll have to take into account security and different workings of different Windows OS's. Here's one of many ways for Vista: http://www.microsoft.com/globaldev/vista/vista_tools/vista_command_line_international_configuration.mspx
0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24409152
if only for my application means can you say what procedure should i follow?
i already tried out using 'Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");' but its getting affected only once.when i again check it the culture gets changed to the original one that is set in windows
 
0
 
LVL 39

Expert Comment

by:abel
ID: 24409635
> but its getting affected only once

Indeed, and it lasts till the thread ends. If you are in a windows application you'll have to do that for each thread you create. If you are in an asp.net application, it will last till the request ends, so you should put it early in the request chain (each request is handled by one thread).

Maybe you are also using localized strings, in which case you should also set the CurrentUICulture the same way.

> when i again check it the culture gets changed to the original one that is set in windows
I must admit that I don't really follow. In my application, when I set it, I can use it as long as I want. Can you give an example of how you set it and when it is unset?


0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24409923
i'm using it in windows application. i have set it in the startup form of my application. when i click another form(billing form - i will be displaying the invoice details in this form) ,while loading the currency values are getting formatted as per the culture i have set (eg: Indian rupees) . but when i reload the form again the currency values are getting formatted in 'US dollars' (ie according to the culture setup in windows)
0
 
LVL 39

Expert Comment

by:abel
ID: 24410111
Hmm, interesting, I have to check. In normal operation the forms run in the same thread. I did find, however, that on older windows version, the currency is not always set correctly (according to MS docs). If that is the same in your case, you can remedy that with the following line:

           // change culture to english/us:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US", false);

Regardless what I find, the workaround will be to place the setting in the constructor or the Load event of the form.
0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24410129
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US", false);

this statement is giving error because the 'CreateSpecificCulture' has only one argument
0
 
LVL 39

Accepted Solution

by:
abel earned 400 total points
ID: 24410140
I'm not sure why it doesn't work for you. I just placed the code for Indian culture inside the static void Main and open / closed many forms. Each form showed the Indian type (I cannot read it, but I think that's what it did).

// code in the extra form:
 
private void Q24411753_Load(object sender, EventArgs e)
{
    // test the default for this thread (should be indian)
    label1.Text = "Without culture: " + (12.34).ToString("C");
 
    // test the specific culture, this always works:
    label2.Text = "With specific culture: " + (12.34).ToString("C", new CultureInfo("hi-IN"));
}
 
// code in the Program.cs file:
[STAThread]
static void Main()
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

Open in new window

ScreenShot288.png
0
 
LVL 39

Expert Comment

by:abel
ID: 24410154
> this statement is giving error because the 'CreateSpecificCulture' has only one argument
Apologies, when I tried it myself, I used the line you see above, but then with an extra useroverride parameter set to false, it should've been this instead:

Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN", false);
but I doubt it will work differently.




0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24410204
did u checked the above coding(indian currency display)by using FormatCurrency syntax?

label1.Text = formatcurrency(12.38,2) ?
0
 
LVL 39

Expert Comment

by:abel
ID: 24410931
No, but FormatCurrency is the same as ToString("C"). But hold on..... Yes, now I did. Same result.

I don't know how your code looks like, but it seems to me that there's some part in your code that (re)sets the currentculture. If you want to be absolutely sure, do not use FormatCurrency (because you cannot give it a Culture), but use the ToString("C") method. It works the same, but the ToString("C") has a second argument where you can add the culture you want to use.
0
 
LVL 39

Expert Comment

by:abel
ID: 24410949
What version of .NET are you using?

I've seen some programmers set a global culture and use that global object all around the code. I don't like that approach, but if for some local bug in your system of .NET version (all service packs installed?) the culture does not work anymore (on vista a user can specify it per application, but you can override it, which is what you did: the "false" parameter)., it is a workaround that at least gives success.
0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24411006
ya when i use ToString("C") method it works fine for me , i'm using visual studio 2005 with .net framework 2.0 with service pack 2 & windows vista
0
 
LVL 39

Expert Comment

by:abel
ID: 24411207
Great that it works with ToString("C"). Strange that there is an apparent bug with FormatCurrency then, which does not show up with VS 2008. To understand the bug, can you confirm whether this is correct: "the FormatCurrency does not take into account the CurrentCulture when overridden with a SpecificCulture after form is opened, closed  and opened again".

-- Abel --
0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24411265
yes thats true its not overriding the specific culture . it just accepts the current culture in the form load (ie) for very first time alone.i think at that time its referring the thread thats has been created during the application startup...

really i'm very much thankful to u .... thanks a lot dude :) ...
0
 
LVL 39

Expert Comment

by:abel
ID: 24413210
You're welcome, glad we found a way, always trying to help :)
0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24418366
Ya thats the spirit of experts like u :) ...

Dude do you have any idea regarding OPOS printers?
0
 
LVL 39

Expert Comment

by:abel
ID: 24419197
I assume you mean this: http://en.wikipedia.org/wiki/OPOS. I fail to see the connection to this question (but that's not always important) and I'm afraid I do not know it...
0
 
LVL 2

Author Comment

by:Rajeshk_cgm
ID: 24419315
this question & OPOS don't have any relation.

ya you are right i was saying about that OPOS only(the one u got in that link)...
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question