Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

CultureInfo

Posted on 2009-05-15
21
Medium Priority
?
450 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

580 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