Board index » delphi » TField-Validating Date Problem

TField-Validating Date Problem

Hi:

  I got this off mers since I was searching for a solution and
found my own post from Sept. 14 2k

Still looking for answer.

Comment from Howard Miller
Funny looking Delphi! That's not that C stuff is it?

Yes. I posted here since posts take longer if ever in CBuilder
NG's.

                         "Robert F. Tulloch" wrote:

                         > HI:
                         >
                         > Code below, fired from
TField->OnSetText works fine as long as there
                         > is date in field. If no date in field
and erroneous date entered,
                         > Sender->FocusControl() returns focus to
the offending control but the
                         > mask 99/99/9999 is lost and you can't
enter a date. As a matter of fact,
                         > all you can enter is one number and of
course the mask does not display.
                         > You cannot reset the offending text as
that fires the validate again.
                         > Not so if the field had a date edited
to an erroneous date.
                         >
                         > Why would this be and how to work
around it? Thanks.
                         >
                         > Best regards
                         >
                         >
//---------------------------------------------------------------------------
                         > void __fastcall
TInfoDM::validateDateField(TField *Sender,
                         > const AnsiString Text)
                         > {
                         > if (Trim(Text) == "/ /")
                         > Sender->Clear();
                         > else
                         > {
                         > try
                         > {
                         > StrToDate(Text);
                         > Sender->AsString = Text;
                         > }
                         > catch(EConvertError *E)
                         > {
                         >
                         > String DisplayMessage =
Sender->DisplayText + " is an invalid
                         > date";
                         >
Application->MessageBox(DisplayMessage.c_str(),
                         > "validate Date Field",
                         > MB_OK | MB_ICONSTOP);
                         > Sender->FocusControl();
                         > }
                         > }
                         > }

 

Re:TField-Validating Date Problem


Quote
"Robert F. Tulloch" wrote:

Hi:

   I got this off mers since I was searching for a solution and
 found my own post from Sept. 14 2k

 Still looking for answer.

 Comment from Howard Miller

Quote
> Funny looking Delphi! That's not that C stuff is it?

 Yes. I posted here since posts take longer if ever in CBuilder
NG's.

 HI:

Code below, fired from
TField->OnSetText works fine as long as there is date in field. If
no date in field
and erroneous date entered, Sender->FocusControl() returns focus
to
the offending control but the mask 99/99/9999 is lost and you
can't
enter a date. As a matter of fact, all you can enter is one number
and of
course the mask does not display.
 You cannot reset the offending text as  that fires the validate
again.
 Not so if the field had a date edited to an erroneous date.

Why would this be and how to work around it? Thanks.

                           Best regards

//---------------------------------------------------------------------------
void __fastcall TInfoDM::validateDateField(TField *Sender, const
AnsiString Text)
{
 if (Trim(Text) == "/ /")
   Sender->Clear();
 else
   {
    try
      {
       StrToDate(Text);
       Sender->AsString = Text;
      }
    catch(EConvertError *E)
      {
       String DisplayMessage = Sender->DisplayText + " is an
invalid date";
       Application->MessageBox(DisplayMessage.c_str(), "validate
Date Field",
                               MB_OK | MB_ICONSTOP);
       Sender->FocusControl();
      }
  }

Quote
}

Re:TField-Validating Date Problem


Hi:

  Sorry. A little thought goes a long way:

//---------------------------------------------------------------------------
void __fastcall TMainDM::ValidateDateField(TField *Sender,
                         const AnsiString Text)
{
   if (Trim(Text) == "/  /")
     {
      Sender->Clear();
     }
   else
     {
      try
        {
         StrToDate(Text);
         Sender->AsString = Text;
        }
      catch(EConvertError *E)
        {
         String DisplayMessage = Text + " is an invalid date";
         Application->MessageBox(DisplayMessage.c_str(),
                                 "Validate Date Field",
                                 MB_OK | MB_ICONSTOP);
         Sender->Value = Text;
<-------------------------------------
         Sender->FocusControl();
        }
     }

Quote
}

Other Threads