procedure Tform1.Edit1KeyPress(Sender: TObject; var Key:
Char);
var
edt: TEdit;
str, strL, strR: string;
p: integer;
begin
// 獲取當前文本內容, 注意要去掉選中部分(因爲會被改寫).
edt := TEdit(Sender);
str := edt.text;
if Length(edt.SelText) <> 0 then
begin
strL := LeftStr(edt.text, edt.SelStart);
strR := RightStr(edt.text, Length(edt.text) - edt.SelStart - edt.SelLength);
str := strL + strR;
end;
// 限制輸入數字/小數點/退格鍵
if not (Key in [#8, #13, #127, '.',
'-', '0'..'9']) then Key := #0;
//限制只能輸入一個小數點
if Key = '.' then
begin
p := Pos('.', edt.Text);
if p > 0 then Key
:= #0;
end;
//限制只能在第一位輸入且只能輸入一個'-'號
if Key = '-' then
begin
if edt.SelStart > 0 then Key
:= #0;
p := Pos('-', edt.Text);
if p > 0 then Key
:= #0;
end;
end;
//要uses StrUtils單元
如果程序裏有很多的TEdit要做此限制,當然不必給每個控件寫代碼,把事件指定到同一個過程就行了。
Delphi中限制文本框(TEdit)只能輸入數字
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
FastReport 程序員手冊
dgmars19841123
2018-08-27 17:38:30
FireMonkey的窗口Handle轉爲Windows窗口的Handle
newsxy
2018-08-27 17:20:40
Delphi XE6 學習筆記(9)手動填充ListView及ListBox
newsxy
2018-08-27 17:20:19
Delphi與VB有什麼區別?
java大豆
2018-08-27 17:20:19
Delphi XE5 將Delphi Code從WinOS 遷移到iOS與Android
newsxy
2018-08-27 17:20:07
保存流 出現錯誤:[FireDAC][Phys]-329. Cannot generate update query. WHERE condition is empty
newsxy
2018-08-27 17:20:07
用Delphi從內存流中判斷圖片格式
newsxy
2018-08-27 17:20:04
datasnap的初步
newsxy
2018-08-27 17:20:03
在 Android 的文字編輯控件 (TEdit) 中, 如何按下 Enter 就隱藏虛擬鍵盤
newsxy
2018-08-27 17:20:03
Thread: DataSnap - detect disgraceful disconnect
newsxy
2018-08-27 17:20:01
Delphi xe 下快捷使用 FastMM 的內存泄露檢測功能
newsxy
2018-08-27 17:20:01
Delphi XE7 beta1的新功能之TListView滑動刷新數據功能
newsxy
2018-08-27 17:20:00
Delphi使用AdoQuery調用Mysql存儲過程
小白2016plus
2018-08-27 16:38:49
Delphi 發佈ActiveX控件 數字簽名(轉)
小白2016plus
2018-08-27 16:38:49
C#首席設計師Anders Hejlsberg專訪
自由的企鹅
2018-08-27 16:01:10