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

WINDOWS API FUNCTION CALL........

Hi All,

QUESTION :
1. Should I create a CUSTOM COMPONENT or should I use the WINAPI ??
2. This One gets the points,  How do I change the Machines Regional Settings Shortdateformat ??

BACKGROUND :
I need to display all dates in my app in dd/mm/ccyy format (company standard).

This is fine for any display (ie labels etc..) as I simply call the Delphi function FormatDateTime to format it correctly. E.g. FormatDateTime(‘dd/mm/yyyy’, TDATETIME);…

I however have a problem with the datetimepicker component.
The DateTimePicker uses the machines local shortdateformat to determine how the date is displayed (ie Regional Settings ShortDateFormat).  I tried changing the Delphi global variable ShortDateFormat to “dd/mm/yyyy’ but it had no effect on the components display.  

I’ve kinda look through the windows API functions calls to find something that will allow me to change the machines local shortdateformat, and haven’t really found anything that makes sense to me, something about LOCALES ???. Iam pretty new to using the Win API directly and would greatly appreciate someone pointing me in the right direction ???  The problem here is that if the user changes the machines local shortdateformat while my app is running I will have to trap the system message indicating that it has changed and then change it back to the format I require…. (mmmmm !!, not to kosher).

My another alternative would be to create my own component by inheriting TDATETIMEPICKER and forcing it to display the date as ‘dd/mm/yyyy’.

Perhaps I should consider using both as parts of the solution, changing the machines local shortdateformat using WinAPI functions and create a custom component, but refrain from trapping the system message should the user change the format while my app is running ????

WHAT DO YOU THINK ??

And for the points can some please tell me how or which function to use to change the regional settings shortdateformat !!  pretty please ??

Y2K Issue regarding FormatDateTime, for Delphi 3.0.
NOTE : in delphi 3.0 calling formatdatetime with the machines local shortdateformat year part set as a 2 digit year (ie “YY”) and a date with a year of 2000+ will return an incorrect date..ie 1900.. instead of 2000..  To correct, this change Delphi’s global variable ShortDateFormat to a 4 year digit.
e.g. ShortDateFormat := 'DD/MM/YYYY';
The problem has been corrected in Delphi 5.....

Thanking everyone in advance !!!!

Derrick Nel……..

0
neld
Asked:
neld
1 Solution
 
kretzschmarCommented:
hi neld,

take a look to simonet's components

http://www.bhnet.com.br/~simonet/omnitools.htm

meikl
0
 
LischkeCommented:
Hi Derrick,

I would suggest that you don't change the system's date format but do a simple call to:

DateTime_SetFormat(DateTimePicker1.Handle, 'ddMMyyy');

This format will be kept by the picker control even if the user changes the system's date format.

Ciao, Mike
0
 
JoeBoothCommented:
If you only want to change the format for your Delphi application, simply add the following code in the form create of your first form.


ShortDateFormat := 'mm/dd/yyyy';

All subsequent Delphi dates in your application with use this format..

If you want to change it on your computer, use the Windows API call

SetLocaleInfo()

The first parameter is the locale number to set, the second is which element to change (you probably want
LOCALE_SSHORTDATE) and the third is a wide string containing the new format.

If you need the LOCALE ID, use the
MAKELCID() macro with the constant LOCALE_SYSTEM_DEFAULT or
LOCALE_USER_DEFAULT
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
cechetoCommented:
I think the previous comment solve your issue.

An other aproach is to change the registry directly.

The value is under HKEY_CURRENT_USER\Control Panel\International and the name of the parameter is sShortDate.

Anyway I haven't test if it works for the program after you change that value or it takes the value on initialization and doesn't work until you re-run the program
0
 
LischkeCommented:
I don't believe that the applications short date format will solve the problem we are discussing here because the datetimepicker doesn't used this format and the VCL never changes the format the picker is using. Hence changing the short format has no effect on the picker.

Changing the systems locale information just to have a specific application looking a certain way is really ugly. What would you think when a cool program would change your entire desktop settings including the wallpaper just to meet its own layout? This is in no case good programming!

Ciao, Mike
0
 
neldAuthor Commented:
I Agree that it is in no case good programming to fiddle with a users personal settings, however the standard employed by our company to avoid Y2K issues is that all dates conform to the mm/dd/yyyy format.  All machines regional settings shortdate must comply to this standard.

What I have done is create a custom component inheriting from TDateTimePicker thereby providing all the existing functionality of the component.  I have overridden the components create constructor, inherited its create and add the Windows API SetLocaleInfo call which changes the shortdate format to dd/mm/yyyy, thereby ensuring that the machines regional settings comply with the standard as specified by my company.

I seem to have a much clearer understanding of the API now :-)

Thanks for your comment Mike, its much appreciated.......

Derrick
0
 
neldAuthor Commented:
To JoeBooth,

Thanks for the API call, the following line of code works for everything as you suggest, with the exception of the DateTimePicker :-)
ShortDateFormat := 'dd/mm'yyyy';

In Delphi 3.0 at least, haven't tried it in Delphi 5.  JoeBooth answered my initial question so I'll allocate the points to you... TA

To Cecheto,

Changing the Registry directly is not an option as it totally screws up all the dates in your app.  I tried it before I posted the initial question :->
SetLocaleInfo() as suggested by JoeBooth works perfectly.

To Mike Lischke,

The following code intrigued me :
DateTime_SetFormat(DateTimePicker1.Handle, 'ddMMyyy');

Firstly my app wouldn't compile until I added COMMCTRL in my uses clause, then it did and it worked beautifully.  Mike if it is possible could you please gimme a little explaination of what it does ???

I'll also post another question so that you too can collect the points aswell as I think that this solution of yours is really the best.  The users personal settings are not affected and the code provides the required solution.

Thank You Mike !!!

To everyone who commented thanks a stax all of them were appreciated....

OkLoveYouByeBye...(Mindy animaniacs)
Derrick







0
 
neldAuthor Commented:
Exactly what I asked for.

Thanks....
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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