我自己做的一个类: 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
这里是最好的代码: 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
新手问题!如何在TXETBOX中控制只能输入数字!
http://community.csdn.net/Expert/topic/3077/3077617.xml?temp=.7569239
http://community.csdn.net/Expert/topic/3067/3067515.xml?temp=.7859156建议:
提问题前先搜索一下
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
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