我想写一个这样的控件,然后放到窗体中,免得每个窗体都要写代码判断输入的是否是数字

解决方案 »

  1.   

    不用写控件,可以用一个如下的代码:Private Sub Text1_KeyPress(KeyAscii As Integer)
        InputNumeric KeyAscii, Text1
    End Sub'*****************************************************************
    '文本框只能输入数字Public Sub InputNumeric(KeyAscii As Integer, txtItem As TextBox)
        Select Case KeyAscii
            Case Asc("-") '允许负数
                If txtItem.SelStart = 0 Then
                  If Left(txtItem.Text, 1) = "-" Then
                      KeyAscii = 0
                      Beep
                  End If
                Else
                  KeyAscii = 0
                  Beep
                End If
            Case 8
                  '无变化,退格键不屏蔽
            Case Asc(" ") '32
                If txtItem.SelLength = 0 Then
                    KeyAscii = 0
                End If
            Case Asc(".") '46 '允许小数点
                If InStr(txtItem.Text, ".") Then
                    KeyAscii = 0
                End If
            Case Is < Asc(0) '48
                  KeyAscii = 0
            Case Is > Asc(9) '57
                  KeyAscii = 0
        End Select
    End Sub
    '*****************************************************************
      

  2.   

    With objTxt
        Select Case KeyAscii
            Case Asc("-")
                KeyAscii = 0
                If InStr(1, .Text, "-") = 0 And Val(.Text) <> 0 Then
                    .Text = "-" & .Text
                    .SelStart = Len(.Text)
                End If
            Case Asc(".")
                If InStr(1, .Text, ".") = 0 And .SelStart > 0 And Left(.Text, .SelStart) <> "-" Then
                Else
                    KeyAscii = 0
                End If
            Case 8
            Case Asc(0)
                If InStr(1, Left(.Text, .SelStart), ".") = 0 And InStr(1, Left(.Text, .SelStart), "-") = 0 And Val(Left(.Text, .SelStart)) = 0 And .SelStart > 0 Then KeyAscii = 0
            Case Is < Asc(0)
                KeyAscii = 0
                Beep
            Case Is > Asc(9)
                KeyAscii = 0
                Beep
        End Select
    End With
      

  3.   

    就在KeyPress事件中写入代码.
     lxcc(虫子|需要点勇气和信心)的方法不错.
      

  4.   

    Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
        If KeyAscii = 8 Then Exit Sub
        If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
    End Sub
      

  5.   

    为textbox附加ES_NUMBER风格就可以了(Const ES_NUMBER = &H2000&)Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Const GWL_STYLE = (-16)
    Const ES_NUMBER = &H2000&Public Sub SetNumber(NumberText As TextBox, Flag As Boolean)
        Dim curstyle As Long, newstyle As Long    curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)    If Flag Then
           curstyle = curstyle Or ES_NUMBER
        Else
           curstyle = curstyle And (Not ES_NUMBER)
        End If
        newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
        NumberText.Refresh
    End Sub