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

DBEdit and Time field display problem

1,132 views
Skip to first unread message

Dave Blake

unread,
Jul 2, 2005, 6:41:40 AM7/2/05
to
I'm trying to use a DBEdit control linked to a TDateTimeField to edit time
data as hours and minutes, the date part of the value being irrelevant.
Consequently I set the underlying DateTimeField to have
displayformat = HH:MM
EditMask = !00:00;1;_

When the DBEdit receives focus the displayed value (in the InplaceEditor)
behaves strangely. For example 00:00 displays as 99:__, and 10:44 displays
as 10:00 (minutes set to zero).

Looking more closely this seems to be a bug in the PadInputLiterals routine
of the Mask unit, but I don't know enough to fix it. Would the experts like
to have a look at this behaviour, I'm using D5 so maybe there is a fix in
later versions?

Otherwise any advice on a simple data aware control for entering time as
hours and minutes (not DateTimePicker as that has already given users
problems).

Dave


Dave Blake

unread,
Jul 3, 2005, 4:07:51 AM7/3/05
to
Well it looks like FormatMaskText can not convert a datetime string to just
hours and minutes.
FormatMaskText("!00:00;1;_", "12/02/2005 10:37:03") produces 10:00.
FormatMaskText("00:00;1;_", "12/02/2005 10:37:03") produces 12/02/2005
10:37.

The underlying routines can not cope with removing both leading and trailing
extranious text. Same routines are used by all decendants of
TCustomEditMask, hence InplaceEditor of DBGrid, DBEdit etc. They will all
display weird things if you use a mask to get text from the middle of a
string. Sounds like a (yet another) longstanding bug in Mask unit to me

So mask use is out. How do I enter hours and minutes to my DateTime field?

Dave


Brian Cook

unread,
Jul 3, 2005, 12:34:50 PM7/3/05
to

You may be able to use DisplayFormat on the TField descendent.

Or OnGetText / OnSetText on the TField plus a Mask.

I've never needed to do what you're trying to do so I can't offer
anything more concrete.

Good luck, Brian

Dave Blake

unread,
Jul 4, 2005, 3:16:42 AM7/4/05
to

> > So mask use is out. How do I enter hours and minutes to my DateTime
> field?
>
> You may be able to use DisplayFormat on the TField descendent.
> Or OnGetText / OnSetText on the TField plus a Mask.

Thanks for your reply Brian.
TField.DisplayFormat = "HH:MM" used to display existing values, but EditMask
will not work to control data entered by user. EditMask processing uses the
text property, not the displaytext, hence gets a datetime string like
"12/03/2005 10:57:03" and can not remove both leading (date) and trailing
(seconds) extraneous characters. You can not get hours and minutes from a
full datetime string using a mask.

Not sure what to do with TField.OnGetText / OnSetText. Perhaps what I could
do is control the way the user edits the hours and minutes on the
TDBEdit.OnKeyPress. I feel like I am reinventing the wheel!

Dave


Dave Blake

unread,
Jul 4, 2005, 5:22:12 AM7/4/05
to
What I've learnt about using DBEdit to edit just time data from a
TDateTimeField as hours and minutes (in case someone else wants to know).

As Datatype = ftDateTime the field text property contains date and time,
this gives problems.
a) No matter how the underlying field displayformat is set, DBEdit shows
full date and time whenever is has focus.
b) You can not get hours and minutes from a full datetime string using a
mask, hence EditMask does not work.
c) A date string can be entered instead of hours and minutes.

Instead use field OnGetText and OnSetText events.

procedure TForm1.TimeFieldGetText(Sender: TField; var Text: String;
DisplayText: Boolean);
begin
Text := FormatDateTime('hh:nn', sender.AsDateTime);
end;

procedure TForm1.TimeFieldSetText(Sender: TField; const Text: String);
var
t: TDatetime;
begin
try
t := StrToTime(text); //Prevents date entry as time
sender.asdatetime := t;
except
on e : exception do begin
sender.FocusControl;
raise;
end;
end;
end;

Dave Blake


0 new messages