• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

CultureInfo

How to change current culture from windows application?
0
Rajeshk_cgm
Asked:
Rajeshk_cgm
  • 11
  • 10
1 Solution
 
abelCommented:
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
 
Rajeshk_cgmAuthor Commented:
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
 
Rajeshk_cgmAuthor Commented:
if i need to change the whole system's culture means what should i do?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
abelCommented:
> 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
 
Rajeshk_cgmAuthor Commented:
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
 
abelCommented:
> 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
 
Rajeshk_cgmAuthor Commented:
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
 
abelCommented:
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
 
Rajeshk_cgmAuthor Commented:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US", false);

this statement is giving error because the 'CreateSpecificCulture' has only one argument
0
 
abelCommented:
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
 
abelCommented:
> 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
 
Rajeshk_cgmAuthor Commented:
did u checked the above coding(indian currency display)by using FormatCurrency syntax?

label1.Text = formatcurrency(12.38,2) ?
0
 
abelCommented:
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
 
abelCommented:
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
 
Rajeshk_cgmAuthor Commented:
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
 
abelCommented:
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
 
Rajeshk_cgmAuthor Commented:
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
 
abelCommented:
You're welcome, glad we found a way, always trying to help :)
0
 
Rajeshk_cgmAuthor Commented:
Ya thats the spirit of experts like u :) ...

Dude do you have any idea regarding OPOS printers?
0
 
abelCommented:
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
 
Rajeshk_cgmAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 11
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now