Edit输入限制问题

tomzhou 2003-12-03 09:27:34
比如我想限制edit中只能输入float类型的浮点数

因此只能输入数字和小数点

我在onkeyup事件中写了以下代码, 190是字符'.'的ascii码
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (((Key < Ord('0')) or (Key > Ord('9'))) and (Key <> 190)) and (Key <> VK_BACK) then begin
Key:=0;
end
else if Key=190 then begin
if Pos('.',(Sender as TCustomEdit).Text)=1 then begin
Key:=0;
end;
end;
end;

但是Key:=0;这条语句似乎不起作用,但是函数说明中Key是var的,应该是可以赋值的。

我将这段代码放到onkeydown事件中,Key:=0可以起作用,但是无法获得这次输入字符后的Edit.text的值,只能获得输入该字符前的text

该问题要如何解决
...全文
148 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
tomzhou 2003-12-04
  • 打赏
  • 举报
回复
谢谢各位了

但是答复都有点问题

再keypress事件中 edit.text是上次的值,其中不包含本次输入的字符
再keydown keyup事件中 又无法修改key

我现在是再onkeypress事件中这样写的

if (((Key < '0') or (Key > '9')) and (Key <> '.')) and (Key<> '-') and
(Key <> Char(VK_BACK)) and (Key <> Char(VK_Left)) and
(Key <> Char(VK_Right)) and (Key <> Char(VK_Delete)) then begin
Key:=#0
end
else if Key='.' then begin
if (Sender as TCustomEdit).SelStart = 0 then
Key:=#0
else if Pos('.',(Sender as TCustomEdit).Text)>0 then
Key:=#0;
end
else if Key='-' then begin
if (Sender as TCustomEdit).SelStart <> 0 then
Key:=#0
else if Pos('-',(Sender as TCustomEdit).Text)>0 then
Key:=#0;
end;

加上了处理负数的代码

liutongliang1 2003-12-03
  • 打赏
  • 举报
回复
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (key = '.' )and (edit1.Text = '') then
key := #0;
if not (key in ['0'..'9','.',#8]) then
key := #0;

end;
google1106 2003-12-03
  • 打赏
  • 举报
回复
上面的都太复杂了,只要在OnKeyPress写下面一句就可以了
if not (key in ['0'..'9','.',char(8)]) then key := char(0)

其中'.'是小数点,char(8)是为了输入错了可以删除,即可以使用BackSpace键
xuantian2002 2003-12-03
  • 打赏
  • 举报
回复
在ONCHANGE 里获得EDIT.TEXT的值,在ONKEYPRESS里做字符串扫描判断
noil0125 2003-12-03
  • 打赏
  • 举报
回复
if (key in ['0']) and (pos('.',Edit1.Text)<1)and(pos('0',Edit1.Text)>0) then key:=#0;
这句应该改成
if (key in ['0']) and (pos('.',Edit1.Text)<1)and(copy(Edit1.Text,1,1)='0') then key:=#0; //改
noil0125 2003-12-03
  • 打赏
  • 举报
回复
rocedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['.','0'..'9',#8]) then key:=#0;
if (key in ['.']) and (pos('.',Edit1.Text)>0) then key:=#0;
if (key in ['.']) and(length(Edit1.Text)<1) then key:=#0;
if (key in ['0']) and (pos('.',Edit1.Text)<1)and(pos('0',Edit1.Text)>0) then key:=#0;
end;
tw_cshn 2003-12-03
  • 打赏
  • 举报
回复
procedure TW_ACPP51.Keyp(var Key: Char);
var
E:TEdit;
i:integer;
MYForm: TcustomForm;
begin
if Key = #13 then
begin
MYForm := GetParentForm( Self );
if not (MYForm = nil ) then
SendMessage(MYForm.Handle, WM_NEXTDLGCTL, 0, 0);
end;
if Not (Key in ['0','1','2','3','4','5','6','7','8','9','.',#8]) then
begin
Abort;
exit;
End;
if self.ActiveControl.ClassName = 'TEdit' then
begin
E:=TEdit(self.ActiveControl);
if (E.Text='') and (Key='.') then
begin
Abort;
exit;
end;
i:=Pos('.',E.Text);
if (Length(E.text)-i>=2) and (E.SelStart>=i) and (i<>0) and (key<>#8) then
abort;
end;
end;
在ONKEYPRESS中调用上过程
procedure TW_ACPP51.Edit5KeyPress(Sender: TObject; var Key: Char);
begin
inherited;
Keyp(key);
end;

tomzhou 2003-12-03
  • 打赏
  • 举报
回复
edit的第一个位置不能输入小数点, 不能输入超过一个小数点

不用第三方控件, 不用tmaskedit
jiayodo 2003-12-03
  • 打赏
  • 举报
回复
keyup事件响应时其键已经输入了,应该在keydown或keypress事件中写
cll007 2003-12-03
  • 打赏
  • 举报
回复
private
iDecimalPoint:integer;
procedure TForm1.Edit1Change(Sender: TObject);
begin

if pos('.',(sender as tedit).Text )=0 then
iDecimalPoint:=0;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case key of
'0'..'9',#8: ;
'.': if length((sender as TEdit).Text)>=1then
if iDecimalPoint<1 then
inc(iDecimalPoint)
else
key:=#13
else
key:=#13;
else key:=#13;
end;
end;
---------
存在的问题:
1.如果复制字符串到Edit中,此法无效;
2.如果先输入345,然后用鼠标使345高亮显示,然后输入 . ,第一个位置仍然可以输入 . ;
建议:
自己重写一个Edit控件

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧