none
allow user some keypress with keycode RRS feed

  • Question

  •  i want to be able to type only numbers, ,numpad, minus key, delete key and backpace and decimal.

    for somereason it does not work. i can only type the numbers above letters..

    would be happy for some knowladge :)

    i know this method is probably not your favor but this way i undestand..  

      If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9 And e.Modifiers <> Keys.Shift) Or (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) And e.KeyCode <> Keys.Left And e.KeyCode <> Keys.Right And e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Decimal And e.KeyCode <> Keys.Delete And e.KeyCode <> Keys.OemMinus Then
                e.SuppressKeyPress = False
            Else
                e.SuppressKeyPress = True
    
    
    
            End If

    thanks,

    Noam.

    Monday, October 15, 2018 8:24 AM

All replies

  • Hello,

    See if this helps to you
        Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            Try
                If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = "." And Not e.KeyChar = "-" Then
                    e.Handled = True
                End If
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub


    • Edited by Tech Aspirant Monday, October 15, 2018 9:05 AM
    • Proposed as answer by Alex Li-MSFT Monday, October 15, 2018 9:30 AM
    Monday, October 15, 2018 9:05 AM
  • got an error, probably becausei forget to put full script:

      For Each ct As Control In Me.Controls
                If (TypeOf ct Is TextBox) And ct.Name <> "mytextbox" Then
                    AddHandler ct.TextChanged, AddressOf TextChanged_Divuah
                    AddHandler ct.KeyDown, AddressOf KeyPress_Divuah 
                End If
            Next
    
    
    
    
        Private Sub KeyPress_Divuah(sender As Object, e As KeyPressEventArgs)
            'נטרול הקשות במקלדת מלבד מספרים
            For Each ct As Control In Me.Controls
                If (TypeOf ct Is TextBox) And ct.Name <> "mytextbox" Then
                    AddHandler ct.TextChanged, AddressOf TextChanged_Divuah
                    AddHandler ct.KeyDown, AddressOf KeyPress_Divuah
                End If
            Next
    
        End Sub


    2 errors:

    Severity Code Description Project File Line Source Suppression State
    Error BC31143 Method 'Private Sub KeyPress_Divuah(sender As Object, e As KeyPressEventArgs)' does not have a signature compatible with delegate 'Delegate Sub KeyEventHandler(sender As Object, e As KeyEventArgs)'. NoamOM C:\NoamOM-SQLite\NoamOM\3_Reports.vb 36 IntelliSense Active


    Severity Code Description Project File Line Source Suppression State
    Error BC31143 Method 'Private Sub KeyPress_Divuah(sender As Object, e As KeyPressEventArgs)' does not have a signature compatible with delegate 'Delegate Sub KeyEventHandler(sender As Object, e As KeyEventArgs)'. NoamOM C:\NoamOM-SQLite\NoamOM\3_Reports.vb 110 IntelliSense Active

    Monday, October 15, 2018 1:27 PM
  • Hi,

    KeyPress that triggers multiple TextBoxes

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For Each ct As Control In Me.Controls
                If (TypeOf ct Is TextBox) Then
                    AddHandler ct.KeyPress, AddressOf KeyPress_Divuah
                End If
            Next
    
        End Sub
    
        Private Sub KeyPress_Divuah(sender As Object, e As KeyPressEventArgs)
            MsgBox("123")
        End Sub
    
    End Class


    Best Regadrs,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, October 16, 2018 2:19 AM