procedure TForm1.Edit1Change(Sender: TObject); var v: double; code: integer; numstr: string; begin numstr:= edit1.text; val(numstr,v,code); if code > 0 then begin if length(numstr)>1 then Delete(numstr,code,1) else if numstr <> '-' then Delete(numstr,code,1); end; edit1.text:= numstr; end;
dcount1,len1:integer; count1:integer; flagd1,flag1,flag2:boolean; 公用变量 onshow事件中初始化 count1:=0; dcount1:=0; len1:=0; flag1:=true; flag2:=true; flagd1:=false; procedure TdefxForm.StringGrid1KeyPress(Sender: TObject; var Key: Char); var i:integer; flag3,flag4:boolean; begin //判断输入的值是否正确 if not(key in ['0'..'9','.',#13,#37,#38,#39,#40,#8,#46]) then begin application.MessageBox('请输入数值!','提示信息',mb_ok+mb_iconinformation); key:=#0; exit; end; count1:=count1+1; if (count1=1)and(key='0') then begin flag3:=true; flag4:=false; flag1:=false; end else begin flag4:=false; flag3:=false; end; if (key in ['0'..'9'])and(count1=2)and(flag1=false)then begin flag4:=true; flag3:=false; flag2:=false; end else begin flag4:=false; flag3:=true; end; if (flag2=false)and(flag1=false) then begin if (flag3=false)and(flag4=true) then begin key:=#0; count1:=1; end; end; if key='.' then begin dcount1:=dcount1+1; flagd1:=true; end; if flagd1=true then begin len1:=len1+1; end; end; procedure TdefxForm.StringGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if ((dcount1>1)or(len1>4))and(stringgrid1.col=2) then begin application.MessageBox('输入小数点错误或数值类型错误!','提示',mb_ok+mb_iconinformation); if stringgrid1.col=2 then stringgrid1.Cells[2,stringgrid1.row]:=''; flagd1:=false; len1:=0; count1:=0; flag1:=true; flag2:=true; dcount1:=0; exit; end; if ((dcount1>1)or(len1>3))and(stringgrid1.col=3) then begin application.MessageBox('输入小数点错误或数值类型错误!','提示',mb_ok+mb_iconinformation); if stringgrid1.Col=3 then stringgrid1.Cells[3,stringgrid1.row]:=''; flagd1:=false; len1:=0; count1:=0; flag1:=true; flag2:=true; dcount1:=0; exit; end; //按下回车 with StringGrid1 do begin if (key=13)or(key=40)or(key=38)or(key=37)or(key=39) then begin if cells[2,row]='' then cells[2,row]:='0'; if cells[3,row]='' then cells[3,row]:='0'; cells[4,row]:=format('%2f',[strtofloat(cells[2,row])*strtocurr(cells[3,row])]); GetEndData; dcount1:=0; len1:=0; count1:=0; flag1:=true; flag2:=true; flagd1:=false; end; end; end;
if ((key in ['0'..'9']) or (key = '-') or (key = '.')) then
begin
key := #0;
end;
try
strtofloat(edit1.Text);
except
messagebox(application.Handle,'无效数值','警告',mb_ok);
end;
var
v: double;
code: integer;
numstr: string;
begin
numstr:= edit1.text;
val(numstr,v,code);
if code > 0 then
begin
if length(numstr)>1 then Delete(numstr,code,1)
else if numstr <> '-' then Delete(numstr,code,1);
end;
edit1.text:= numstr;
end;
count1:integer;
flagd1,flag1,flag2:boolean; 公用变量
onshow事件中初始化
count1:=0;
dcount1:=0;
len1:=0;
flag1:=true;
flag2:=true;
flagd1:=false;
procedure TdefxForm.StringGrid1KeyPress(Sender: TObject; var Key: Char);
var
i:integer;
flag3,flag4:boolean;
begin
//判断输入的值是否正确
if not(key in ['0'..'9','.',#13,#37,#38,#39,#40,#8,#46]) then
begin
application.MessageBox('请输入数值!','提示信息',mb_ok+mb_iconinformation);
key:=#0;
exit;
end;
count1:=count1+1;
if (count1=1)and(key='0') then
begin
flag3:=true;
flag4:=false;
flag1:=false;
end
else
begin
flag4:=false;
flag3:=false;
end;
if (key in ['0'..'9'])and(count1=2)and(flag1=false)then
begin
flag4:=true;
flag3:=false;
flag2:=false;
end
else
begin
flag4:=false;
flag3:=true;
end;
if (flag2=false)and(flag1=false) then
begin
if (flag3=false)and(flag4=true) then
begin
key:=#0;
count1:=1;
end;
end;
if key='.' then
begin
dcount1:=dcount1+1;
flagd1:=true;
end;
if flagd1=true then
begin
len1:=len1+1;
end;
end;
procedure TdefxForm.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if ((dcount1>1)or(len1>4))and(stringgrid1.col=2) then
begin
application.MessageBox('输入小数点错误或数值类型错误!','提示',mb_ok+mb_iconinformation);
if stringgrid1.col=2 then
stringgrid1.Cells[2,stringgrid1.row]:='';
flagd1:=false;
len1:=0;
count1:=0;
flag1:=true;
flag2:=true;
dcount1:=0;
exit;
end;
if ((dcount1>1)or(len1>3))and(stringgrid1.col=3) then
begin
application.MessageBox('输入小数点错误或数值类型错误!','提示',mb_ok+mb_iconinformation);
if stringgrid1.Col=3 then
stringgrid1.Cells[3,stringgrid1.row]:='';
flagd1:=false;
len1:=0;
count1:=0;
flag1:=true;
flag2:=true;
dcount1:=0;
exit;
end;
//按下回车
with StringGrid1 do
begin
if (key=13)or(key=40)or(key=38)or(key=37)or(key=39) then
begin
if cells[2,row]='' then
cells[2,row]:='0';
if cells[3,row]='' then
cells[3,row]:='0';
cells[4,row]:=format('%2f',[strtofloat(cells[2,row])*strtocurr(cells[3,row])]);
GetEndData;
dcount1:=0;
len1:=0;
count1:=0;
flag1:=true;
flag2:=true;
flagd1:=false;
end;
end;
end;