procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Not (key in ['0'..'9','.']) then key :=#0;
end;
begin
if Not (key in ['0'..'9','.']) then key :=#0;
end;
解决方案 »
- JS怎么调用delphi activex里面的函数?
- delphi程序开始时Create分配内存,结束时不FreeAndNil,程序会不会自动释放内存空间?
- 如何建立带空格文件名
- 将execl的数据导入到ACCESS库中?
- ★★★★★为感谢兄弟姐妹在这里给我的一些帮助,今天散分多谢大家★★★★★
- 还是关于成绩库排序问题?
- 高分求解一个问题(分不够可以再加)在线等候
- 能否限制Edit中的字符输入格式?
- 小公告!关于table1.insert永远的问题
- 用OLE控件保存后的内容如何能还原回WORD文档? 请教
- 有对TNMFTP熟悉的请看一下
- 用 Microsoft Media Player 的ocx 做播放器,哪儿有相关的资料
if not (key in [#48..#57,#96..#105,#190]) then
begin
key:=#0;
beep;
end;
if not (key in ['0'..'9','.',#8]) then Key := #0;
其它有相同要求的Edit使用OnKeyPress事件指向前面那个Edit的OnKeyPress事件就行了,呵呵,别告诉我说你不知道怎么使多个Edit的某事件指向某个现有事件.此方法不足之处在于,没法识别用户输入了几个".',也没法识别用户贴贴过来的数据.优点是,简单!!
Windows, Messages, SysUtils, Classes, Controls, StdCtrls;type
TFloatEdit = class(TCustomEdit)
private
FDecimalDigit: Integer;
function GetValue: Real;
procedure SetValue(Value: Real);
procedure SetDecimalDigit(Value: Integer);
protected
procedure WMPaste(var Message: TWMPaste); message WM_Paste;
procedure KeyPress(var Key: Char); override;
public
constructor Create(AOwner: TComponent); override;
published
property DecimalDigit: Integer read FDecimalDigit write SetDecimalDigit;
property Value: Real read GetValue write SetValue;
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property CharCase;
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property ImeMode;
property ImeName;
property MaxLength;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('StrongSoft', [TFloatEdit]);
end;{ TFloatEdit }////////////////////////////// Public //////////////////////////////constructor TFloatEdit.Create(AOwner: TComponent);
begin
inherited;
SetValue(0);
FDecimalDigit := 2;
end;////////////////////////////// Protected //////////////////////////////procedure TFloatEdit.WMPaste(var Message: TWMPaste);
var
OldValue: Real;
temp: Extended;
iPointPos, iSelStart: Integer;
begin
OldValue := GetValue;
iSelStart := SelStart;
inherited;
if not TryStrToFloat(Text, temp) then
begin
Text := FloatToStr(OldValue);
SelStart := iSelStart;
end
else begin
iPointPos := Pos('.', Text);
if iPointPos <> 0 then
begin
if Length(Text) - iPointPos > FDecimalDigit then
begin
Text := Copy(Text, 1, iPointPos + FDecimalDigit);
SelStart := iSelStart;
end;
end;
end;
end;procedure TFloatEdit.KeyPress(var Key: Char);
var
iPointPos, iDecimalDigit: Integer;
begin
iPointPos := Pos('.', Text);
if Key in ['0'..'9', #8] then
begin
if Key = #8 then
begin
inherited;
Exit;
end;
if iPointPos <> 0 then
begin
iDecimalDigit := Length(Text) - iPointPos;
if (iDecimalDigit > FDecimalDigit - 1) and (SelStart + 1> iPointPos) then
begin
Key := #0;
Exit;
end
end;
inherited
end
else if (iPointPos = 0) and (Key = '.') then
inherited
else
Key := #0;
end;////////////////////////////// Private //////////////////////////////function TFloatEdit.GetValue: Real;
begin
if Text = '' then
Result := 0
else
Result := StrToFloat(Text);
end;procedure TFloatEdit.SetValue(Value: Real);
var
iPointPos: Integer;
begin
Text := FloatToStr(Value);
iPointPos := Pos('.', Text);
if iPointPos <> 0 then
Text := Copy(Text, 1, iPointPos + FDecimalDigit);
end;procedure TFloatEdit.SetDecimalDigit(Value: Integer);
var
iPointPos: Integer;
begin
if Value > 0 then
begin
iPointPos := Pos('.', Text);
FDecimalDigit := Value;
Text := Copy(Text, 1, iPointPos + FDecimalDigit);
end;
end;end.
if not (char in ['0'..'9',',']) then
exit;
end;
使用集合类型
begin
key:=#0;
perform(wm_nextdlgctl,0,0);
end;
我以编下列代码,但不能控制小数点在最前面以及最后面!
if not (((key>'0') and (key<'9')) or (key='.')) then
//只能输入数字以及小数点
begin
key := #0;
messagebeep(0);
end
else
if (pos('.', edit10.text) <> 0) then
if key='.' then
begin
key := #0;
messagebeep(0);
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Pos(key,'0123456789.'+#8+#10+#13) = 0 then
key :=#10;
end;