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;
是不是对的呀~!~!
这是我自己以前用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
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;
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;
这个方法有问题,可以输入无数个小数点,可以自己写点东西来限制小数点的位置和个数