看看这里源代码,已由我调试成功!
不过值得说明的是:在向MaskEdit1中输入正数后,不得将光标移到最前端,如果那样,
则可以输入0。当然一般情况是不可能的,或者你将MaskEdit1.ReadOnly :=true改为
MaskEdit1.text:=''这样就不会出现以上特殊情况!!!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask;
type
TForm1 = class(TForm)
MaskEdit1: TMaskEdit;
procedure MaskEdit1KeyPress(Sender: TObject; var Key: Char);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.MaskEdit1KeyPress(Sender: TObject; var Key: Char);
begin
if (MaskEdit1.Text='') and (key=#48) then //判断第一个数不能为0
MaskEdit1.ReadOnly :=true
//判断第一个数为0结束
else
begin
if key=#8 then //判断如果是退格键,则可以使用!
MaskEdit1.ReadOnly:=false
else
if (key<#48) or (key>#58) then
MaskEdit1.ReadOnly :=true
else
MaskEdit1.ReadOnly :=false;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MaskEdit1.Text :='';
end;
end.