How To: Work around a Users Region Setting (Date issue)???

  • Thread starter Thread starter JamesBV
  • Start date Start date
J

JamesBV

My PC's Region setting is set to "English (Canada)"... cause I am in Canada
(eh?) (: I'm using VB.net, standard edition (v1?)

So I wrote my application based on this Region. I've both Short and Long
dates being displayed (including a DateTimePicker).

BUT... at work, there are some machines which the Region is the ol' default of
"English (United States)".

Canadian:
Short = dd/MM/yy
Long = MMMM d, yyyy

USA:
Short = M/d/yyy
Long = dddd, MMMM dd, yyyy

What my issue is is that my Date displays (both for long and short) are either
OK or Screwed up depending upon the Region display. This because my labels,
textboxes, etc. have 'fixed' widths.

Bah! Changing the PC's Regions is NOT an option (I won't go into that).

So how can I have my app display what I want?

I know I can 'build' the date string. Such for a Label, I can have:
myMonth & myDay & myYear... etc.

But the DateTimePicker is got me stumped.

Prefferably, there must be an easier way to do this so that 'globally' my app
works for English (Canadian) and not screw up the Users PC Region setting.

Thanks

James
 
Why don't you use Formatting?

1. String.Format("mmDDyyyy",myDate)
2. myDate.ToString("mmDDyyyy")

Remember that month should be DD and not dd. I have done that once before
and was writing minutes instead of months!! Bad eh? This is case sensitive.
Mind you I believe it is a by-design bug. ;-)
 
You can manually set the current thread's UI Culture to whatever you want.
System.Threading.CurrentUICulture (something around there).
-mike
MVP
 
Why don't you use Formatting?

1. String.Format("mmDDyyyy",myDate)
2. myDate.ToString("mmDDyyyy")

Hmmmm... okay. But will this allow me to change the DateTimePicker display?
I'm not sure if it will.

I changed my Region Date to USA standard.

I then tried putting MMMM d, yyyy in the Properties "Custom Format" line...
and then changed the "Format" line to 'Custom'. But the display still showed
USA format.

Other than that... the other stuff (labels, textboxes) work fine.

Idea?

Thanks.

James
 
James,

DateTimePicker is one of those dodgy controls (e.g you cannnot set
background colour for the text) and I am not surprised if it behaves badly.
But, I tested that on my machine (UK setting) with "MMMM d, yyyy" in format
property and setting the format to custom and it did show "November 12,
2003". Are you trying to use "Value" property in code? I am using VB.NET
2003 and that might cause the difference (very unlikely though)

Michael's approach is very professional and polished (he is MVP after all
;-)):
System.Threading.Thread.CurrentThread.CurrentUICulture = New
System.Globalization.CultureInfo("en-CA")

You might as well use that.

Cheers
Ali
 
background colour for the text) and I am not surprised if it behaves badly.
But, I tested that on my machine (UK setting) with "MMMM d, yyyy" in format

I don't know what I did.. but just before finding this 2nd reply of yours. I
got it to work!! Must have done something wrong the first time. (sigh)
Michael's approach is very professional and polished (he is MVP after all
;-)):
System.Threading.Thread.CurrentThread.CurrentUICulture = New
System.Globalization.CultureInfo("en-CA")

True. Where does this go? In Form Load? I placed it there but nothing
happened. Obviously this needs more work.

Thanks!

James
 
You can manually set the current thread's UI Culture to whatever you want.
System.Threading.CurrentUICulture (something around there).

Alireza gave me the following:

System.Threading.Thread.CurrentThread.CurrentUICulture = New
System.Globalization.CultureInfo("en-CA")

Just got to place it, use it properly (of which I have no idea... never have
had the 'pleasure' of Threading before) (:

Thanks!

James
 
If you want your whole app to be in a certain culture, you can set the
thread's culture on startup of your app and leave it.

If it's only for a particular piece of code, then you should save the
original culture, set the new one, then revert when you are done using it.

The *real way* is to figure out how to make your app respond correctly no
matter what culture it's running on. Manual override is a hackish solution.
But if it's just for an internal system that doesn't have international
requirments, it *probably* won't matter (unless some VP from Korea comes
over and runs it and it crashes ;)).

-mike
MVP
 
If you want your whole app to be in a certain culture, you can set the
thread's culture on startup of your app and leave it.

Actually it is only for work here in Canada. I just wanted to overcome the
PC's which have the USA date/time Region setting because I did my code based
on my PC (being Canadian Region setting).
requirments, it *probably* won't matter (unless some VP from Korea comes
over and runs it and it crashes ;)).

LOL...

What I ended up doing (because it only affected 5 lines) was to 'force' the
Code Format. Tried it with both Regions set for my PC and I think I've got it
licked.

But it would have been nicer (of course) to have set it in one spot. Maybe in
the future when I go International (:

Thanks again for your Time!

James
 
Back
Top