Board index » delphi » Need a Working Mask Edit for a Date

Need a Working Mask Edit for a Date

Being an old Clipperhead. I was experimenting with date settings in my
application, and stumbled upon a problem.  If the user toggles the date
setting to YYYY/MM/DD, the default date edit mask of !99/99/00;1;_ will
not allow them to key in the century.  If the date is not in the default
century, they're SOL.  Without the TField edit mask, the user must key
in the date seperators, or an exception will occurr.

How do I use the advantages of the masks, while still allowing the user
to enter a date in any allowable format?  As an aside, is it more
appropriate to leave such 'international settings' such as date, to the
default set in the control panel, or have local settings within the app?

 

Re:Need a Working Mask Edit for a Date


Quote
Larry Hengen wrote:

> Being an old Clipperhead. I was experimenting with date settings in my
> application, and stumbled upon a problem.  If the user toggles the date
> setting to YYYY/MM/DD, the default date edit mask of !99/99/00;1;_ will
> not allow them to key in the century.  If the date is not in the default
> century, they're SOL.  Without the TField edit mask, the user must key
> in the date seperators, or an exception will occurr.

> How do I use the advantages of the masks, while still allowing the user
> to enter a date in any allowable format?  As an aside, is it more
> appropriate to leave such 'international settings' such as date, to the
> default set in the control panel, or have local settings within the app?

You'll need more than that, try the following :
Type in a date,
Advance to the next control with TAB,
SHIFT-TAB back again (the entire date is selected),
Press the BACKSPACE to empty the value,
Try leaving the field with TAB.

Delphi will tell you that you have entered an illegal field-value.

I haven't found the solution to this problem, so if anyone has, please
let me know!

Delphi is great!
Spread the word.

Greetz,
Nic.

Re:Need a Working Mask Edit for a Date


Here's the code for a date edit field I wrote, and it's been working OK
lately.  If you use it and find problems, please let me know...

Note that I found the whole key is to shift the mask field around.  If you
try to save '  /  /    ' to a database, you get a convert error, so that's
why I created this component.

type TChEdit = class (TMaskEdit)
end;

type TChDateEdit = class (TChEdit)
        private
                fYear, fMonth, fDay: word;
                Formatting: boolean;
        protected
                procedure Da{*word*249}ter(Sender: TObject); virtual;
                procedure DateChange(Sender: TObject); virtual;
                procedure DateExit(Sender: TObject); virtual;
                procedure SetYear (value: word); virtual;
                procedure SetMonth (value: word); virtual;
                procedure SetDay (value: word); virtual;
        public
                constructor Create (AOwner: TComponent); override;
                procedure FormatDate;
                procedure LoadDate;
                property Year: word read fYear write SetYear;
                property Month: word read fMonth write SetMonth;
                property Day: word read fDay write SetDay;
end;

...

{
        TChDateEdit

Quote
}

constructor TChDateEdit.Create (AOwner: TComponent);
        begin
                inherited;
                width := 70;
                EditMask := '!99/99/9999;1; ';
                text := FormatDateTime('mm/dd/yyyy', Now);
                OnEnter := Da{*word*249}ter;
                OnChange := DateChange;
                OnExit := DateExit;
        end;

procedure TChDateEdit.Da{*word*249}ter(Sender: TObject);
        begin
                inherited;
                LoadDate;
        end;

procedure TChDateEdit.DateChange(Sender: TObject);
        begin
                inherited;
                LoadDate;
        end;

procedure TChDateEdit.DateExit(Sender: TObject);
        begin
                DateChange (self);  //auto-format on exit
        end;

{
        This function takes the year, month & day properties to re-create the
  text property

Quote
}

procedure TChDateEdit.FormatDate;
        begin
                inherited;
                Formatting := true;
                if ((year = 0) or (month = 0) or (day = 0)) then
                        text := ''
                else
                        text := FormatDateTime('mm/dd/yyyy', EncodeDate (year, month, day));
                Formatting := false;
        end;
{
        This function splits the passed in text value into year, month
  and day and stores it in the properties
Quote
}

procedure TChDateEdit.LoadDate;
        begin
                inherited;
                try
                        if not Formatting then begin
                                Formatting := true;
                                if text = '' then begin
                                        fYear := 0;
                                        fMonth := 0;
                                        fDay := 0;
                                        if focused then
                                                EditMask := '!99/99/9999;1; ';
                                end else if text = '  /  /    ' then begin
                                        fYear := 0;
                                        fMonth := 0;
                                        fDay := 0;
                                        if not focused then
                                                EditMask := '';
                                end else if focused then
                                        //do no more--it'll get formatted on exit.
                                else begin
                                        DecodeDate (StrToDate (text), fYear, fMonth, fDay);
                                        EditMask := '!99/99/9999;1; ';
                                        FormatDate;
                                end;
                                Formatting := false;
                        end;
                except
                        fYear := 0;
                        fMonth := 0;
                        fDay := 0;
                end;
        end;

procedure TChDateEdit.SetYear (value: word);
        begin
                fYear := value;
                FormatDate;
        end;

procedure TChDateEdit.SetMonth (value: word);
        begin
                fMonth := value;
                FormatDate;
        end;

procedure TChDateEdit.SetDay (value: word);
        begin
                fDay := value;
                FormatDate;
        end;

Re:Need a Working Mask Edit for a Date


Here's the code for a date edit field I wrote, and it's been working OK
lately.  If you use it and find problems, please let me know...

Note that I found the whole key is to shift the mask field around.  If you
try to save '  /  /    ' to a database, you get a convert error, so that's
why I created this component.

type TChEdit = class (TMaskEdit)
end;

type TChDateEdit = class (TChEdit)
        private
                fYear, fMonth, fDay: word;
                Formatting: boolean;
        protected
                procedure Da{*word*249}ter(Sender: TObject); virtual;
                procedure DateChange(Sender: TObject); virtual;
                procedure DateExit(Sender: TObject); virtual;
                procedure SetYear (value: word); virtual;
                procedure SetMonth (value: word); virtual;
                procedure SetDay (value: word); virtual;
        public
                constructor Create (AOwner: TComponent); override;
                procedure FormatDate;
                procedure LoadDate;
                property Year: word read fYear write SetYear;
                property Month: word read fMonth write SetMonth;
                property Day: word read fDay write SetDay;
end;

...

{
        TChDateEdit

Quote
}

constructor TChDateEdit.Create (AOwner: TComponent);
        begin
                inherited;
                width := 70;
                EditMask := '!99/99/9999;1; ';
                text := FormatDateTime('mm/dd/yyyy', Now);
                OnEnter := Da{*word*249}ter;
                OnChange := DateChange;
                OnExit := DateExit;
        end;

procedure TChDateEdit.Da{*word*249}ter(Sender: TObject);
        begin
                inherited;
                LoadDate;
        end;

procedure TChDateEdit.DateChange(Sender: TObject);
        begin
                inherited;
                LoadDate;
        end;

procedure TChDateEdit.DateExit(Sender: TObject);
        begin
                DateChange (self);  //auto-format on exit
        end;

{
        This function takes the year, month & day properties to re-create the
  text property

Quote
}

procedure TChDateEdit.FormatDate;
        begin
                inherited;
                Formatting := true;
                if ((year = 0) or (month = 0) or (day = 0)) then
                        text := ''
                else
                        text := FormatDateTime('mm/dd/yyyy', EncodeDate (year, month, day));
                Formatting := false;
        end;
{
        This function splits the passed in text value into year, month
  and day and stores it in the properties
Quote
}

procedure TChDateEdit.LoadDate;
        begin
                inherited;
                try
                        if not Formatting then begin
                                Formatting := true;
                                if text = '' then begin
                                        fYear := 0;
                                        fMonth := 0;
                                        fDay := 0;
                                        if focused then
                                                EditMask := '!99/99/9999;1; ';
                                end else if text = '  /  /    ' then begin
                                        fYear := 0;
                                        fMonth := 0;
                                        fDay := 0;
                                        if not focused then
                                                EditMask := '';
                                end else if focused then
                                        //do no more--it'll get formatted on exit.
                                else begin
                                        DecodeDate (StrToDate (text), fYear, fMonth, fDay);
                                        EditMask := '!99/99/9999;1; ';
                                        FormatDate;
                                end;
                                Formatting := false;
                        end;
                except
                        fYear := 0;
                        fMonth := 0;
                        fDay := 0;
                end;
        end;

procedure TChDateEdit.SetYear (value: word);
        begin
                fYear := value;
                FormatDate;
        end;

procedure TChDateEdit.SetMonth (value: word);
        begin
                fMonth := value;
                FormatDate;
        end;

procedure TChDateEdit.SetDay (value: word);
        begin
                fDay := value;
                FormatDate;
        end;

Other Threads