我要做一个日期输入的小界面,不用DateTimePicker等。。
只用edit控件,我如何能限制edit内容的数字是1~12之间或者1~31之间呢?

解决方案 »

  1.   

    strMonth:String;//为全局变量
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    var
      iMonth : Integer;
    begin
      if key in ['0'..'9'] then
        strMonth := Edit1.Text;
      iMonth := StrToInt(strMonth);
      if (iMonth = 0) and (iMonth > 12) then
        begin
          ShowMessage('您输入的月份不对,请重新输入');
          Exit;
        end;
    end;
      

  2.   

    strMonth,strDay :String;//为全局变量
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    var
      iMonth : Integer;
    begin
      if key in ['0'..'9'] then
        strMonth := Edit1.Text;
      iMonth := StrToInt(strMonth);
      if (iMonth = 0) and (iMonth > 12) then
        begin
          ShowMessage('您输入的月份不对,请重新输入');
          Exit;
        end;
    end;procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
    var
      iMonth : Integer;
    begin
      if key in ['0'..'9'] then
        strDay := Edit1.Text;
      iMonth := StrToInt(strMonth);
      if (iMonth = 0) and (iMonth > 31) then
        begin
          ShowMessage('您输入的日期不对,请重新输入');
          Exit;
        end;
    end;
      

  3.   

    if (strtointdef(edit.text,0)<1) or (strtointdef(edit.text,0)>12) then
      showmessage('只能输入1~12!');
    在onChange事件上面写以上代码。
    不过,有个TFilterComboBox控件,在FileCtrl单元,可视化控件在samples下面。
    可以控制最大值,最小值
      

  4.   

    设置  edit1.MaxLength:=2;
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);//只能输入1-12
    begin
      if (key in ['0'..'9']) then
      begin
        if (length(edit1.Text)=0) and (key='0') then
            key:=chr(0);
        if (length(edit1.Text)=1) and not(key in ['0','1','2']) then
            key:=chr(0);
      end
      else
        key:=chr(0);end;