I have a doubt regarding the exception in tmaskedit component in delphi.i am using tmaskedit with editmask as date option.i am using three maskedits with these same property.but when i am trying not valid date or giving empty data,exception is caused which i am not able to handle.i dont knw where to catch the exception for this prblem

Recommended Answers

All 3 Replies

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask;

type
  TForm1 = class(TForm)
    MaskEdit1: TMaskEdit;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function  IsDateCorrect(S: string;  var DT: TDateTime): Boolean;
var
  B:      Boolean;
begin
  B := True;
  try
    try
      DT := StrToDate(S);
    except
      B := False;
      ShowMessage('Incorrect Date');
    end;
  finally
    Result := B;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  DT:     TDateTime;
begin
  if IsDateCorrect(MaskEdit1.Text,  DT) then
    Memo1.Lines.Add(DateToStr(DT))
  else
    Memo1.Lines.Add('Incorrect Date !');
end;

end.

i am the same jacobchry.icudnt logon to my account thats y another account..this code wen we use cause exception,if we give a wrong date or empty date
..the edit mask option of the maskedit button is set to date.the input mask is !99/99/0000;1;_..i want to handle the exception..please help me with this

jacobchry, and jacobchry1, please compile the code below and run the .exe file = there will not raise any exception.
But if you run the code below under Delphi, the exception will raise, sorry, under Delphi it could not be avoid ....

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask;

type
  TForm1 = class(TForm)
    MaskEdit1: TMaskEdit;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function  IsDateCorrect(S: string;  var DT: TDateTime): Boolean;
var
  B:      Boolean;
begin
  B := True;
  try
    try
      DT := StrToDate(S);
    except
      B := False;
    end;
  finally
    Result := B;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  DT:     TDateTime;
begin
  if IsDateCorrect(MaskEdit1.Text,  DT) then
    Memo1.Lines.Add(DateToStr(DT))
  else
    Memo1.Lines.Add('Error ....');
end;

end.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.