unit MyEdit;interfaceuses
  SysUtils, Classes, Controls, StdCtrls;type
  TMyEdit = class(TEdit)
  private
    { Private declarations }
  protected
   procedure KeyPress(var Key: Char); override;
   procedure DoExit;override;
   procedure Change;override;
    { Protected declarations }
  public
    { Public declarations }
  published
  constructor Create(AOwner:TComponent); override;
    { Published declarations }
  end;procedure Register;implementationconstructor TMyEdit.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
end;procedure TMyEdit.Change();
begin
         //我想放在这里面来判断,判断用户不能输入,
        //  意思就是 判断月份不能大于12end;procedure TMyEdit.KeyPress( var Key:Char);
begin
   if   not(key   in['0'..'9',#8,#13])   then   //若输入的不是数字键、删除键或回车键   
  begin
      Key:=#0;                            
  end;
   inherited KeyPress( Key);
end;procedure TMyEdit.DoExit;
begin
  if (Length(Text)=6)  or  (Length(Text)=4) then
  begin
      if Length(Text) = 6 then
    begin
      if StrToInt(Copy(Text,5,2))>12 then
      begin
        Text := '';
        SetFocus;
        Exit;
      end;
    end;
    if  Length(Text)= 4 then
    begin
      if StrToInt(Copy(Text,3,2)) > 12 then
      begin
        Text := '';
        SetFocus;
        Exit;
      end
      else
      begin
       if StrToInt(Copy(Text,1,2)) >30 then
         Text := '19'+Text
       else
         Text := '20'+Text;
      end;
    end;
  end
  else
  begin
     SetFocus;
  end;
  inherited DoExit;
end;procedure Register;
begin
  RegisterComponents('Standard', [TMyEdit]);
end;end.

解决方案 »

  1.   

    那么累干嘛?直接用datetimepicker不就好了
      

  2.   

    用Mask,或者OnExit的时候判断RzDateTimeEdit直接可以设置输入错误的时候提示其实我觉得不用改控件这么麻烦,只要最后保存之前判断下就行了,用TryStrToDate
      

  3.   


    var
      Date: TDateTime;
    begin
      if TryStrToDate(DateStr, Date) then
        ShowMessage('合法'); 
      else
        ShowMessage('不合法日期');
    end; 
      

  4.   

    学习了 TDateTime;beginif TryStrToDate(DateStr, Date)这个函数不错
      

  5.   

    第三方控件ok啦,DateTimePicker也可以啊
      

  6.   


    是啊,直接用datetimepicker多省事啊