Solved

CultureInfo

Posted on 2009-05-15
21
433 Views
Last Modified: 2013-11-26
How to change current culture from windows application?
0
Comment
Question by:Rajeshk_cgm
  • 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
 
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 100 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to open a link from vb.net app 3 9
Copy/Clone an object. 9 15
Achieve json result 2 34
Unable  to create new object 9 25
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…
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now