用户输入控制仍不理想

ahu 2000-01-24 05:27:00
刚才已按各高手的指点去做,但实际情况不理想。
用key in ['0'..'9']程序报错(type incompatible,word and char),改用key in [48..57](0到9的ascii码),但在输入时每按backspace键一次就提示一次,很烦。能不能在keyexit事件中判断一
下输入的是否为数值型(类似其他语言中isnumber这种函数)。strtoint好像不是最理想的,if strtoint(edit1.text)=0不允许!
盼指教!!
...全文
233 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
光明山人 2000-01-25
  • 打赏
  • 举报
回复
同意kxy与yjq,我一向用OnKeyPress,这样就可以用'0'..'9'判断了,

另外yjq有一点说错了, ahu用的应当是OnKeyDown,所以才不能用字符型,因为在OnKeyDown中参数是Key: Word。另外,你的字符集合写成['0'..'9', '.', #8]也可以,既少累些又防止漏数,另外你没有把TAB包含进来喔。我跟yjq写if的习惯不一样,可能我会在if中加not反向判断,避免then中的空语句。
#48..#57实际上等同于'0'..'9'。

有一件事我还不明白,keyexit是什么事件,我怎么没见过?好象只有OnExit吧。
Delphi中没有提供判断是否数字串的函数,可以用StrToInt或StrToIntDef做一个,或干脆就一个字符一个字符地判断,但还得判断是否超界,很麻烦。用StrToIntDef时,可以将默认值设为一个空白值:Integer($80000000),这样就不用担心是否与正常使用值冲突了。
tiger 2000-01-25
  • 打赏
  • 举报
回复
我写了一个控件digitEdit, 很好用的.
unit DigitEdit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

const
DIGITS = [^H, '0'..'9'];

type
TDigitEdit = class(TEdit)
private
fChr : Char;
{ Private declarations }
protected
procedure Change; override;
procedure SetChr(Value : char);
{ Protected declarations }
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure CreateParams(var Params:TCreateParams);override;
{ Public declarations }
published
property Chr : Char read fChr write SetChr default #0;
{ Published declarations }
end;

procedure Register;

implementation

procedure TDigitEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style:= Params.Style or ES_RIGHT;
end;

procedure TDigitEdit.SetChr(Value : char);
begin
if fChr <> value then fChr := Value;
end;

constructor TDigitEdit.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
end;

Destructor TDigitEdit.Destroy;
begin
inherited Destroy;
end;

procedure TDigitEdit.Change;
var
i, Posi : Integer;
aText : string;
begin
inherited Changed;
if csDesigning in ComponentState then Exit;
aText := Text;
Posi := SelStart;
for i := 1 to Length(aText) do
if not ((aText[i] in DIGITS) or (aText[i] = fChr))then
Delete(aText, i, 1);
Text := aText;
// SelStart := Length(Text);
SelStart := Posi;
end;

procedure Register;
begin
RegisterComponents('Standard', [TDigitEdit]);
end;

end.

ahfei 2000-01-25
  • 打赏
  • 举报
回复
既然程序中用了StrToInt,就是说要输入的是一个整数,那么用StrToInt去判断就是最简单的,而且是绝对可行的!不要把问题复杂化吧!
如果输入的可能是小数,那么用key in '1','2','3','4','5','6','7','8','9','0','.',#8] 也是不够的,否则我输入
'-100'不能通过,输入'100.01.02',却能通过!
渤海海峡 2000-01-24
  • 打赏
  • 举报
回复
haha:上面的程序有一个小问题:
if key=#13 下一个控件.setfocus else ...
渤海海峡 2000-01-24
  • 打赏
  • 举报
回复
我一直这么用,没问题。
procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key in ['1','2','3','4','5','6','7','8','9','0','.',#8] then
begin
end else
begin
if key=#13 then Edit1.SetFocus else
key:=#0; //过滤掉其他的输入
end;
end;
除了数字和点以及删除键,其他的全部过滤。
在说数字输入的edit很多呀。(比如rxlib的)

另:你的ascii表示有问题。应该是[#48..#57]
ahfei 2000-01-24
  • 打赏
  • 举报
回复
try
i:=StrToInt(Edit1.Text);
是整数,这里做你的处理
except
//不是整数
ShowMessage("您输入的不是整数,请重新输入!");
Edit1.Text:='';
Edit1.SetFocus;
end;
kxy 2000-01-24
  • 打赏
  • 举报
回复
用key in ['0'..'9']程序报错(type incompatible,word and char),
选错了事件,
procedure (Sender: TObject; var Key: Word; Shift: TShiftState) of object;
OnKeyDown Key : word;
procedure (Sender: TObject; var Key: Char) of object;
OnKeyPress Key : Char
StrToInt你判断一下嘛,Edit.Text = ''就什么都不作.
sky__horse 2000-01-24
  • 打赏
  • 举报
回复
又:
MaskEdit控件的EditMask属性就是管这个事儿的
sky__horse 2000-01-24
  • 打赏
  • 举报
回复
if strtointdef(edit1.Text,-9999)=-9999 then
///处理字符窜
else
///处理数字;
说明: 首先需要用户不会输入-9999,否则需要换一个数
repus 2000-01-24
  • 打赏
  • 举报
回复
在Edit的exit事件中用Val来判别好了。
WHQ 2000-01-24
  • 打赏
  • 举报
回复
是要在EDIT控件中只允许输入数据吗?那就把它的属性加上ES_NUMBER,但这样连小数点也不能输了。

5,386

社区成员

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

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