使用自定义的控件,继承一个textbox 下面是全部代码
Public Class NumericTextbox
    Inherits System.Windows.Forms.TextBox#Region " Windows Form Designer generated code "    Public Sub New()
        MyBase.New()        'This call is required by the Windows Form Designer.
        InitializeComponent()        'Add any initialization after the InitializeComponent() call    End Sub    'UserControl1 overrides dispose to clean up the component list.
    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    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub#End Region
    Private Sub NumericTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        Dim KeyAscii As Integer
        KeyAscii = Asc(e.KeyChar)        Select Case KeyAscii
            Case 48 To 57, 8, 13
            Case 45
                If Me.SelectionStart <> 0 Then
                    KeyAscii = 0
                End If
            Case 56
                If InStr(Me.Text, ".") <> 0 Then
                    KeyAscii = 0
                End If            Case Else
                KeyAscii = 0
        End Select        If KeyAscii = 0 Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End Sub
End Class

解决方案 »

  1.   

    textbox_keypress(sender,e)
    {
    if (//判断无效键盘值) e.handler = true; //取消按键
    }
      

  2.   

    private void KeyPress( object sender, KeyPressEventArgs e )
    {
       e.Handled = !char.IsDigit(e.KeyChar);
    }
      

  3.   

    在keypress事件中
    if(char.isnumber(e.keychar)==true || e.char==".")
               是数字
      

  4.   

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
    if(!(System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(),
    @"^(-?\d+)(\.\d+)?$") || e.KeyChar == Convert.ToChar(8) || e.KeyChar == '.'))
    {
    e.Handled=true;
    }
    base.OnKeyPress (e);
    }