想控制输入TextBox的是数字,有OnKeyDown事件吗?

解决方案 »

  1.   

    参考:
    新手问题!如何在TXETBOX中控制只能输入数字! 
    http://community.csdn.net/Expert/topic/3077/3077617.xml?temp=.7569239
      

  2.   

    怎么样控制textbox只能录入数字(包括小数) 
    http://community.csdn.net/Expert/topic/3067/3067515.xml?temp=.7859156建议:
    提问题前先搜索一下
      

  3.   

    我自己做的一个类:
    Imports System
    Imports System.Windows.Forms
    Imports Microsoft.VisualBasic
    Public Class numtbx
        Inherits System.Windows.Forms.TextBox#Region " 组件设计器生成的代码 "    Public Sub New()
            MyBase.New()        ' 该调用是组件设计器所必需的。
            InitializeComponent()        '在 InitializeComponent() 调用之后添加任何初始化    End Sub    'Control 重写 dispose 以清理组件列表。
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub    '控件设计器所必需的
        Private components As System.ComponentModel.IContainer    '注意: 以下过程是组件设计器所必需的
        ' 可以使用组件设计器修改此过程。不要使用
        ' 代码编辑器修改它。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            components = New System.ComponentModel.Container
        End Sub#End Region
        Dim intdecimal As Int16 = 0
        Public Property DecimalLength() As Int16    '设置小数位
            Get
                DecimalLength = intdecimal
            End Get
            Set(ByVal Value As Int16)
                intdecimal = Value
            End Set
        End Property    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(pe)        '在此添加自定义绘画代码
        End Sub
        Protected Overrides Sub onkeypress(ByVal e As KeyPressEventArgs)
            If IsNumeric(e.KeyChar) Then
                If MyBase.Text.Trim.IndexOf(".") > -1 Then
                    If MyBase.Text.Trim.Length - MyBase.Text.Trim.IndexOf(".") - 1 >= intdecimal Then
                        e.Handled = True
                    End If
                End If
            Else
                Select Case e.KeyChar
                    Case "-"
                        If MyBase.Text.Trim.Length > 0 Then
                            e.Handled = True
                        End If
                    Case "."
                        If MyBase.Text.Trim.IndexOf(".") > -1 Then
                            e.Handled = True
                        End If
                    Case "{backspace}"  '自己找一下键值,我后来才发现,懒得改了
                        '
                    Case Else
                        e.Handled = True
                End Select
            End If
        End Sub
    End Class
      

  4.   

    上面的是编译通过的,可以设置小数位数,没有用<description>来描述
      

  5.   

    其实无须限制输入的是数字,只要在你要保存该文本框里面的数字时多一个校验过程就行了,或者在该文本框失去焦点是使用val(text1.text)将其转换成数字就行了。
      

  6.   

    这里是最好的代码:
    If KeyAscii < 46 Or KeyAscii > 57 And KeyAscii <> 47 Then
      If KeyAscii = 13 Then
       MsgBox "你按了回车键"
      End If
      If KeyAscii <> 8 Then KeyAscii = 0
    End If