1. var i:double; begin try i:=strtofloat(youredit.text); except showmessage('error'); end; 2.在edit的onbuttonpress(好像是这么写的)事件中 begin if not (key in ['0'..'9','.',#8]) then //0-9,小数点,和backspace以外的键 key:=#0 end; 这个方法有问题,可以输入无数个小数点,可以自己写点东西来限制小数点的位置和个数
错了,真丢脸,是onkeypress,最近看书看傻了,5555555
//KeyPress里写: if not (Key in ['0'..'9', '.', #7, #8]) then begin Key := #0; ShowMessage('只能输入数字!'); end;
procedure TForm.Edit1Change(Sender: TObject); begin try Edit1.Text:=FloatToStr(StrToFloat(Edit1.Text)); except Showmessage('Error!'); end; end;procedure Tchjsd.Edit4KeyPress(Sender: TObject; var Key: Char); begin if not (key in ['0'..'9',#8]) then begin key:=#0; Showmessage('Error'); end; end;
//KeyPress里写: if not (Key in ['0'..'9', #8]) then begin Key := #0; ShowMessage('只能输入数字!'); end;
#8是BACKSPACE #13是ENTER 其实就是这个键的键值,我也是菜鸟,我只知道这些了 给你个小程序,你就知道其他的键的键值. 也是在edit的onkeypress里写的 begin label1.caption:=inttostr(ord(key)); end;我懒得调试了,这句话应该没错吧 应该是这样, ^_^
这是我自己以前用VB写的,考虑得很周全。只是可能逻辑有点混乱。 Private Sub Text1_KeyPress(KeyAscii As Integer) If ((KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 46) And KeyAscii <> 8 Then KeyAscii = 0 End If If KeyAscii = Asc(0) Then If Key_0 = False Then KeyAscii = 0
End If If KeyAscii = 46 Then Select Case Key_D Case "前面加零" Text1.Text = "0." SendKeys "{end}" KeyAscii = 0 Case "已经有了" KeyAscii = 0 Case "不可再加", "不可加" KeyAscii = 0 End Select End If End Sub Private Function Key_0() As Boolean If Len(Text1.Text) = 0 Then Key_0 = False Else Key_0 = True End If If Text1.SelText <> "" Then If InStr(1, Text1.Text, Text1.SelText) = 1 Then Key_0 = False End If End If End Function Private Function Key_D() As String Dim TempStr As String Dim TempI As Integer Dim TempI1 As Integer
'文本框里面一无所有 If Len(Text1.Text) = 0 Then Key_D = "前面加零" Exit Function End If
If Text1.Text = Text1.SelText Then Key_D = "前面加零" Exit Function End If '文本框里面没有"."号 TempI = InStr(1, Text1.Text, ".") ' MsgBox TempI If TempI <= 0 Then TempStr = Text1.SelText If TempStr <> "" Then '文本框里面没有点号,但是选中了开头的数字。 TempI1 = InStr(1, Text1.Text, TempStr) If TempI1 <= 1 Then Key_D = "不可加" Exit Function End If End If Exit Function End If
'文本框里面已经有了,而且没有被选中的文本 If TempI > 0 And Text1.SelText = "" Then Key_D = "已经有了" Exit Function End If
'文本框里面有了,有被选中的文本。 TempStr = Text1.SelText ' MsgBox TempStr If TempStr <> "" Then TempI1 = InStr(Text1.Text, TempStr) ' MsgBox TempI1 If TempI1 <= 1 Then Key_D = "不可加" Exit Function End If ' TempI = InStr(1, Text1.Text, ".") ' If TempI <= 0 Then Exit Function
TempI = InStr(1, TempStr, ".") If TempI > 0 Then Exit Function
Key_D = "不可再加" End If End Function
不用ctrl+v啊,用右键菜单也可以粘贴啊 还要用粘帖的功能啦~!~! 再贴一段代码,实现粘贴功能:procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if (ssctrl in shift) and ((key=118) OR (key=86)) then edit1.PasteFromClipboard; end; 是不是对的呀~!~!
begin
key := #0;
showmessage('error');
end;
if (not (Key in ['0'..'9'])) and (key<>#8) then
Key :=#0;
TRY
STRTOFLOAT(EDIT1.TEXT)
EXCEPT
SHOWMESSAGE();
END;
或用MaskEdit
var
i:double;
begin
try
i:=strtofloat(youredit.text);
except
showmessage('error');
end;
2.在edit的onbuttonpress(好像是这么写的)事件中
begin
if not (key in ['0'..'9','.',#8]) then //0-9,小数点,和backspace以外的键
key:=#0
end;
这个方法有问题,可以输入无数个小数点,可以自己写点东西来限制小数点的位置和个数
if not (Key in ['0'..'9', '.', #7, #8]) then
begin
Key := #0;
ShowMessage('只能输入数字!');
end;
try
i:=strtofloat(youredit.text);
except
showmessage('error'); //显示出错
youredit.Text:='';
youredit.SetFoucs; //设置焦点,要用户重输
end;
begin
try
Edit1.Text:=FloatToStr(StrToFloat(Edit1.Text));
except
Showmessage('Error!');
end;
end;procedure Tchjsd.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
begin
key:=#0;
Showmessage('Error');
end;
end;
if not (Key in ['0'..'9', #8]) then
begin
Key := #0;
ShowMessage('只能输入数字!');
end;
#13是ENTER
其实就是这个键的键值,我也是菜鸟,我只知道这些了
给你个小程序,你就知道其他的键的键值.
也是在edit的onkeypress里写的
begin
label1.caption:=inttostr(ord(key));
end;我懒得调试了,这句话应该没错吧 应该是这样, ^_^
我至少看见6次了这个题目,没有一个完整的
建议把这个题目放进精华区
---------------
屏蔽Ctrl+V
屏蔽鼠标右键菜单
屏蔽非数字字符
只允许输入一个 .
else
key:=#0;
STRTOFLOAT(EDIT1.TEXT)
EXCEPT
SHOWMESSAGE();
edit1.setfocus;
END;
Private Sub Text1_KeyPress(KeyAscii As Integer)
If ((KeyAscii < Asc(0) Or KeyAscii > Asc(9)) And KeyAscii <> 46) And KeyAscii <> 8 Then
KeyAscii = 0
End If
If KeyAscii = Asc(0) Then
If Key_0 = False Then KeyAscii = 0
End If
If KeyAscii = 46 Then
Select Case Key_D
Case "前面加零"
Text1.Text = "0."
SendKeys "{end}"
KeyAscii = 0
Case "已经有了"
KeyAscii = 0
Case "不可再加", "不可加"
KeyAscii = 0
End Select
End If
End Sub
Private Function Key_0() As Boolean
If Len(Text1.Text) = 0 Then
Key_0 = False
Else
Key_0 = True
End If
If Text1.SelText <> "" Then
If InStr(1, Text1.Text, Text1.SelText) = 1 Then
Key_0 = False
End If
End If
End Function
Private Function Key_D() As String
Dim TempStr As String
Dim TempI As Integer
Dim TempI1 As Integer
'文本框里面一无所有
If Len(Text1.Text) = 0 Then
Key_D = "前面加零"
Exit Function
End If
If Text1.Text = Text1.SelText Then
Key_D = "前面加零"
Exit Function
End If
'文本框里面没有"."号
TempI = InStr(1, Text1.Text, ".")
' MsgBox TempI
If TempI <= 0 Then
TempStr = Text1.SelText
If TempStr <> "" Then '文本框里面没有点号,但是选中了开头的数字。
TempI1 = InStr(1, Text1.Text, TempStr)
If TempI1 <= 1 Then
Key_D = "不可加"
Exit Function
End If
End If
Exit Function
End If
'文本框里面已经有了,而且没有被选中的文本
If TempI > 0 And Text1.SelText = "" Then
Key_D = "已经有了"
Exit Function
End If
'文本框里面有了,有被选中的文本。
TempStr = Text1.SelText
' MsgBox TempStr
If TempStr <> "" Then
TempI1 = InStr(Text1.Text, TempStr)
' MsgBox TempI1
If TempI1 <= 1 Then
Key_D = "不可加"
Exit Function
End If
' TempI = InStr(1, Text1.Text, ".")
' If TempI <= 0 Then Exit Function
TempI = InStr(1, TempStr, ".")
If TempI > 0 Then Exit Function
Key_D = "不可再加"
End If
End Function
还要用粘帖的功能啦~!~!
再贴一段代码,实现粘贴功能:procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (ssctrl in shift) and ((key=118) OR (key=86)) then
edit1.PasteFromClipboard;
end;
是不是对的呀~!~!
procedure TForm1.Edit1Change(Sender: TObject);
begin
try StrtoFloat(edit1.Text)
except
messagebox(handle,'请检查输入数字!','出错警告',mb_iconwarning);
end;
end;