登录
首页 电脑编程
回帖 发帖
正文

主题:在VB中如何让textbox控件只接受数字输入

点击:6627 回复:8

请教大家:
温度控制参数输入,但不知如何让textbox控件只接受数字输入,我查不到相关的属性来限制这个控件的可接受的数据类型!请大家多多指点!
03-10-25 17:12
Private Sub Text1_Change()
Dim s$
s = ".0123456789"
With Text1
   If Len(.Text) Then
   '在每个输入字符中查找在s中的字符标本,若找不到, 则去掉
       If InStrRev(s, Mid(.Text, .SelStart, 1)) = 0 Then
           .SelStart = .SelStart - 1  '以前一字符为起点
           .SelLength = 1  '选择字数
           .SelText = ""   '删除所选字符
       End If
   End If
End With
End Sub
03-10-25 18:57
Thank you!
03-10-30 13:27
这样能防止输入两个小数点吗?
04-09-04 10:06
这样不能防止输入两个小数点!
04-09-14 11:19
等输入完毕后检测是否可以转化为数字量,如果不行让他重输
04-09-21 10:07
我在delphi中写过这样的程序,可以参考一下
04-09-21 13:38
procedure TFin_AdsumRegisterfm.EditTotalKeyPress(Sender: TObject;
 var Key: Char);
begin
 if not (key in['0'..'9','-','.',#8,#13]) then key:=#0;
 if (key='.') and (Pos('.',(sender as TEdit).text)>0) then Key:=#0;
end;
响应了keypress事件
04-09-21 13:41
,你试一下下面的!
Private Sub Text1_Change()
   If Not IsNumeric(Text1.Text) And Len(Text1.Text) <> 0 Then
       Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
          MsgBox "必须输入数字!", vbOKOnly, "输入数据类型错误"
              Text1.SelStart = Len(Text1.Text)
         ElseIf Len(Text1.Text) = 0 Then
              Text1.Text = ""
          Text1.SelStart = Len(Text1.Text)
   End If
End Sub
04-09-27 09:55

工控新闻

更多新闻资讯