Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DateTimePicker and Null

2,021 views
Skip to first unread message

Frederic Gelinas

unread,
Nov 6, 2001, 2:29:13 PM11/6/01
to
I succeed in creating a dbAware version of the DateTimePicker (originally
called DbDateTimePicker). There is still a problem. When the field
contains a null value, it shows the date equivalent to 0 (1899-12-30).
I would like my component to display a blank when the data is null. How can
I make the DateTimePicker not to display a date at all but a blank string?


serge gubenko

unread,
Nov 6, 2001, 3:38:46 PM11/6/01
to

"Frederic Gelinas" <fgel...@si.qc.ca> wrote in message
news:3be838f9_2@dnews...

Hi, Frederic

You can set a string of blanks (actually one would be enough) to the
Format property of the DateTimePicker:

Format:=' ';

or

Format:=' . . ';

and you should get an empty editing field.

Best regards, Serge Gubenko


Frederic Gelinas

unread,
Nov 6, 2001, 5:11:20 PM11/6/01
to
> You can set a string of blanks (actually one would be enough) to the
> Format property of the DateTimePicker:

Well, I'm sure you haven't tried it...

There isn't a format property in a TDateTimePicker. There is a DateFormat
property of this type. TDTDateFormat = (dfShort, dfLong);


serge gubenko

unread,
Nov 6, 2001, 5:12:07 PM11/6/01
to

"Frederic Gelinas" <fgel...@si.qc.ca> wrote in message
news:3be85ef6$1_2@dnews...
Hi,

Here's what Delphi 6 help says about Format:
___________________________________
TDateTimePicker.Format
{...}
Specify format for date-time string.

property Format: String;

Description

Format specifies custom format string for the date-time display, overriding
the control panel strings. A custom format can include both date and time
fields, but time fields are not editable unless the Kind property is
dtkTime....

{..}
____________________________________

regards


serge gubenko

unread,
Nov 7, 2001, 1:12:14 AM11/7/01
to

"Frederic Gelinas" <fgel...@si.qc.ca> wrote in message
news:3be85ef6$1_2@dnews...

Looks like Format is a new property to D6, so if you're working with
elder version you should something like this:

var
XFormat: string;
begin
XFormat:=' ';
SendMessage(DateTimePicker1.Handle, DTM_SETFORMAT,
0, integer(PChar(XFormat)));
{or}
SendMessage(Handle, DTM_SETFORMAT, 0, integer(PChar(XFormat)));
{if you're going to set format in the internal methods of TDateTimePicker
descendant class}

DTM_SETFORMAT constant is declared in the Commctrl unit

regards


Jesús Avilés Martínez

unread,
Nov 7, 2001, 3:36:47 AM11/7/01
to
Frederic Gelinas wrote:

I think this is because you are storing and reading the DateTime field
asociated with DBDateTimePicker in DateTime format. When you store a
blank date, you have to do something like this:

FieldByName('DateTimeFieldName').Clear

and when you read the field, do it like this:

sDateTime := FieldByName('DateTimeFieldName').AsString;
if sDateTime = '' then ...

so you can see if it´s a blank date.

--
JAM - Relájate y disfruta...

Frederic Gelinas

unread,
Nov 7, 2001, 9:45:18 AM11/7/01
to
Thanks Serge! I'm on the right way of getting it. Ther is still a problem.
Here is my DataChange procedure:

procedure TJvDBDateTimePicker.DataChange(Sender: TObject);
var
lsFormat:string;
begin
if FDataLink.Field <> nil then
Date := FDataLink.Field.AsDateTime
else if csDesigning in ComponentState then
Date := Date;
if Assigned(FDataLink.Field) then
if FDataLink.Field.IsNull then
lsFormat:=' ';
// always send the message: lsFormat will be ''
SendMessage(Handle, DTM_SETFORMAT, 0, integer(PChar(lsFormat)));
end;

This works great. I get an empty string for null value. But when I input
data by dropping down the calendar, nothing appears. The data is correctly
affected, but it doesn't appear in the control. If I post the dataset at
this moment, the data appears and then everything is correct until I get
another null value. Then, you have to post to get the data displayed.

Any hint?


Frederic Gelinas

unread,
Nov 7, 2001, 10:49:08 AM11/7/01
to
There is also a problem of editing... When the value is null, nothing is
displayed, but the user can't type anything in the DateTimePicker. Do I
have to do something special in the OnEnter event to getback the capability
of editing the date using the keyboard?

serge gubenko

unread,
Nov 7, 2001, 4:35:46 PM11/7/01
to

"Frederic Gelinas" <fgel...@si.qc.ca> wrote in message
news:3be947eb$1_2@dnews...

> Thanks Serge! I'm on the right way of getting it. Ther is still a
problem.
> Here is my DataChange procedure:
>
{..}

>
> This works great. I get an empty string for null value. But when I input
> data by dropping down the calendar, nothing appears. The data is
correctly
> affected, but it doesn't appear in the control. If I post the dataset at
> this moment, the data appears and then everything is correct until I get
> another null value. Then, you have to post to get the data displayed.
>

Hi, Frederic

Seems, you should return empty format string to your control
when user drops down a calendar:
___________________________________________
TMyDateTimePicker = class(TDateTimePicker)
protected
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
procedure SetFormat(const AValue: string);
end;

procedure TMyDateTimePicker.CNNotify(var Message: TWMNotify);
begin
inherited;
with Message do begin
case NMHdr^.code of
DTN_DROPDOWN:
SetFormat('');
DTN_CLOSEUP:
if DateTime=0 then SetFormat(' ');
end;
end;
end;

procedure TMyDateTimePicker.SetFormat(const AValue: string);
begin
SendMessage(Handle, DTM_SETFORMAT, 0, integer(PChar(AValue)));
end;
_____________________________________________
Best regards, Serge Gubenko


serge gubenko

unread,
Nov 7, 2001, 4:54:33 PM11/7/01
to

"Frederic Gelinas" <fgel...@si.qc.ca> wrote in message
news:3be956e5_2@dnews...

Hi,

You can try to set format in the handlers of WM_SetFocus,
WM_KillFocus and WM_KeyDown messages.
Here's an example:
____________________________________________
TMyDateTimePicker = class(TDateTimePicker)
protected
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus(var Message: TWMSetFocus); message WM_KILLFOCUS;
procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;


procedure SetFormat(const AValue: string);
end;

procedure TMyDateTimePicker.WMSetFocus(var Message: TWMSetFocus);
begin
inherited;
SetFormat('');
end;

procedure TMyDateTimePicker.WMKillFocus(var Message: TWMSetFocus);
begin
inherited;


if DateTime=0 then SetFormat(' ');
end;

procedure TMyDateTimePicker.WMKeyDown(var Message: TWMKeyDown);
begin
SetFormat('');
inherited;

Christopher Latta

unread,
Nov 8, 2001, 2:48:43 AM11/8/01
to
When I did this I used the checkbox to indicate a null value.

Christopher Latta

Frederic Gelinas <fgel...@si.qc.ca> wrote in message

news:3be838f9_2@dnews...

Frederic Gelinas

unread,
Nov 8, 2001, 9:44:01 AM11/8/01
to
Great! it works at 95%... There is still something not working.
When I call your SetFormat function in the KeyDown, it causes a problem when
editing. The user can't typemore than one character. Beginning to type
2001 in the year replaces only one character.
Removing the setFormat in the KeyDown leads in another problem. Until I
don't DropDown the calendar, it is impossible to enter a date, naturally. I
need a way to SetFormat in the keyDown, without causing the strange
behavior.

BTW, A BIG thanks for your help!


serge gubenko

unread,
Nov 8, 2001, 4:05:50 PM11/8/01
to

"Frederic Gelinas" <fgel...@si.qc.ca> wrote in message
news:3bea991c_1@dnews...

Hi, Frederic

Seems, we need to change the SetFormat procedure.
Here's how it might look like:

TMyDateTimePicker = class(TDateTimePicker)
protected
FFormat: string;
{..}


procedure SetFormat(const AValue: string);
end;

procedure TMyDateTimePicker.SetFormat(const AValue: string);
begin
if FFormat<>AValue then begin
FFormat:=AValue;


SendMessage(Handle, DTM_SETFORMAT, 0, integer(PChar(AValue)));
end;

end;

Best regards, Serge Gubenko


Frederic Gelinas

unread,
Nov 9, 2001, 9:01:18 AM11/9/01
to
One thing to say: Thanks a lot!
I (you) finally get it to work properly!


Juha Manninen

unread,
Nov 19, 2001, 11:36:50 AM11/19/01
to
Frederic Gelinas wrote:

> One thing to say: Thanks a lot!
> I (you) finally get it to work properly!
>

Hi,


So, how does the final code look like ?
I was just going to ask about the same thing when i saw your message.
Are you able to share the component ?

There are many similar components in Torry's and other places but they
all have the same problem in their behaviour.

Juha Manninen

Frédéric Gélinas

unread,
Nov 19, 2001, 1:15:56 PM11/19/01
to
Sent by email...

--
Frederic Gelinas
www.si.qc.ca
"Juha Manninen" <juha.m...@datex-nospam-ohmeda.com> a écrit dans le
message news: 3BF93522...@datex-nospam-ohmeda.com...

Juha Manninen

unread,
Nov 19, 2001, 2:23:45 PM11/19/01
to
Frédéric Gélinas wrote:

> Sent by email...
>


Sorry, I think there was the 'nospan' included in my address.
Can you please resend it to : juha.m...@datex-ohmeda.com
Thanks in advance.

Juha

0 new messages