请问有没有人做过Edit控件,要求只能输入整数和浮点数,急
解决方案 »
- Label显示标题不全?
- D7与Excel的交互
- 添加数据的解决办法!
- delphi 如何实现KAISER密文..急.......
- 有ShowModal只作用于窗体,而不作用于整个程序的吗?
- 谁能帮我看看该控件如何装才能成功
- 如何播放VOC格式的文件。
- 在DELPHI中先调用excel,再关闭excel,内存中excel无法释放??
- 我想实现这么一个问题,一个DBGrid一个Button,点击Button在DBGrid中增加一个空白行可以添加数据,但是原来的数据不能加入到数据库中。
- ALL? 如何在messagebox中出现“ALL”按钮
- vclskin和BusinessSkin的比较?
- 问个Rave报表的问题。
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','.',#8]) then exit;
end;
就可以实现你的要求了
var Key: Char);
begin
If Key in ['0'..'9',#8,'.'] then
begin
If Key='.' then
begin
With TEdit(Sender) do
begin
If Length(trim(Text))=0 then
Key:=#0
Else
begin
If Pos('.',Text)>0 then Key:=#0;
end;
end;
end;
end
Else Key:=#0;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (not (key in ['0'..'9','.',#8])) or ((key='.') and (pos('.',Edit1.Text)>0)) then key:=#0;
end;
if not((key in ['0'..'9',#8,'.']) ) then
key := #0;
就行了。
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (not (key in ['0'..'9','.',#8])) or ((key='.') and (pos('.',Edit1.Text)>0)) then key:=#0;
end;
==========================================
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
tmpString:String;
iSelStart:Integer;
begin
if Key in [#8,'1'..'9'] then exit;
iSelStart:=TEdit(Sender).SelStart;
case Key of
'0':
begin
tmpString:=Copy(TEdit(Sender).Text,1,iSelStart);
if Pos('.',tmpString)>0 then Exit;
case Length(tmpString) of
0:
Exit;
1:
if Pos('-',tmpString)>0 then Exit;
end;
if Length(StringReplace(StringReplace(tmpString,'0','',[rfReplaceAll]),'-','',[rfReplaceAll]))>0 then Exit;
end;
'.':
if ((Pos('.',TEdit(Sender).SelText)>0) and (Pos('.',TEdit(Sender).Text)>0) or (Pos('.',TEdit(Sender).Text)=0)) then Exit;
'-':
if (iSelStart=0) and ((Pos('-',TEdit(Sender).SelText)>0) and (Pos('.',TEdit(Sender).Text)>0) or (Pos('-',TEdit(Sender).Text)=0)) then Exit;
end;
Key:=#0;
end;
procedure TFormAddP90TR.Edit17KeyPress(Sender: TObject; var Key: Char);
var
p:integer;
tp:Tpoint;
begin
if key in['0'..'9','+','-','.'] then
begin
if key in['+','-'] then
//************判断是否为0~9以及是否为+,-号**************
begin
p:=pos('+',edit17.Text)+pos('-',edit17.Text);
if p>0 then key:=#0
else
begin
getcaretpos(tp);
if tp.X>1 then key:=#0;
end;
end
else
//*************判断是否为小数点****************
if key='.' then
begin
p:=pos('.',edit17.Text);
if p>0 then key:=#0; end;
end
else if key>#31 then
key:=#0;
end;
begin
if MyKess.SelLength =length(MyKess.Text) then
MyKess.Clear;
if (Key <> '1') and (Key <> '2') and (Key <> '3') and (Key <> '4') and (Key <>
'5') and (Key <> '6') and (Key <> '7') and (Key <> '8') and (Key <> '9') and
(Key <> '0') and (Key <> '.') and (Key <> '-') then
begin
if Key <> #8 then
Key := char(0);
end
else
begin
if Key = '.' then
begin
if pos('.', MyKess.Text) <> 0 then
Key := char(0)
end
else
if Key = '-' then
begin
if trim(MyKess.Text)<>'' then
Key := char(0)
end
else
begin
if (pos('.', MyKess.Text) <> 0) and (Length(copy(MyKess.Text, pos('.',
MyKess.Text) + 1, Length(MyKess.Text))) > Len - 1) then
Key := char(0)
else
//if Key='0' then
begin
if (trim(MyKess.text)='0') or (trim(MyKess.text)='-0') then
Key := char(0)
end;
end;
end;
end;调用:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
MyKeyPress(Edit1,2,Key);
end;
在onexit 事件里:procedure TForm1.Edit1Exit(Sender: TObject);
begin
if trim(edit1.text)='' then
edit1.text:='0';
if copy(trim(edit1.text),len(trim(edit1.text)),1)='.' then
edit1.text:=copy(trim(edit1.text),1,len(trim(edit1.text))-1);
end;
begin
if MyKess.SelLength =length(MyKess.Text) then
MyKess.Clear;
if (Key <> '1') and (Key <> '2') and (Key <> '3') and (Key <> '4') and (Key <>
'5') and (Key <> '6') and (Key <> '7') and (Key <> '8') and (Key <> '9') and
(Key <> '0') and (Key <> '.') and (Key <> '-') then
begin
if Key <> #8 then
Key := char(0);
end
else
begin
if Key = '.' then
begin
if pos('.', MyKess.Text) <> 0 then
Key := char(0)
end
else
if Key = '-' then
begin
if trim(MyKess.Text)<>'' then
Key := char(0)
end
else
begin
if (pos('.', MyKess.Text) <> 0) and (Length(copy(MyKess.Text, pos('.',
MyKess.Text) + 1, Length(MyKess.Text))) > Len - 1) then
Key := char(0)
else
//if Key='0' then
begin
if (trim(MyKess.text)='0') or (trim(MyKess.text)='-0') then
Key := char(0)
end;
end;
end;
end;调用:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
MyKeyPress(Edit1,2,Key);
end;
在onexit 事件里:procedure TForm1.Edit1Exit(Sender: TObject);
begin
if trim(edit1.text)='' then
edit1.text:='0';
if copy(trim(edit1.text),len(trim(edit1.text)),1)='.' then
edit1.text:=copy(trim(edit1.text),1,len(trim(edit1.text))-1);
end;