AndrzejD
AndrzejD

Reputation: 45

textbox with numbers only but also pasting, copying, and selecting

I'm quite new in VB and got stuck on (i think) easy problem. I have a text box thats allows only numbers:

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

    If Asc(e.KeyChar) <> 8 Then
        If Asc(e.KeyChar) < 49 Or Asc(e.KeyChar) > 57 Then
            e.Handled = True
        End If
    End If

End Sub

from 1 to 9. But this box doesnt allows me to paste, copy, and select text...how can i change it? I know that KeyCode for Control is 17, and for 'V' is 86, but have no idea how to use it... thanks for any help

Upvotes: 0

Views: 1914

Answers (3)

suvidha rane
suvidha rane

Reputation: 1

This worked for me , write the below code on textbox keypress event

Try

        If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) AndAlso (e.KeyChar <> Chr(22)) Then
            MsgBox("Please enter numeric values", MsgBoxStyle.Information)
            e.Handled = True
        End If
    Catch ex As Exception
        pObj.WriteErrorLog(Me.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message, ex, True)
    End Try

Upvotes: 0

Visual Vincent
Visual Vincent

Reputation: 18310

If you necessarily want a TextBox you could combine the KeyDown and KeyPress events in order to block anything that isn't numbers while manually allowing Copy, Paste, Cut, etc.

Imports System.Text.RegularExpressions

Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
    e.Handled = Char.IsNumber(e.KeyChar) = False AndAlso Char.IsControl(e.KeyChar) = False 'Verify if input is a number or a control character (such as Backspace).
End Sub

Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
    Dim TargetTextBox As TextBox = DirectCast(sender, TextBox)
    e.SuppressKeyPress = True 'Start by blocking all key presses.

    Select Case e.KeyData 'In order to not block some standard keyboard shortcuts.
        Case Keys.Control Or Keys.C 'Copy
            TargetTextBox.Copy()
        Case Keys.Control Or Keys.X 'Cut
            TargetTextBox.Cut()
        Case Keys.Control Or Keys.V 'Paste
            TargetTextBox.Paste()
            TargetTextBox.Text = Regex.Replace(TextBox2.Text, "[^\d]", "")
        Case Keys.Control Or Keys.A 'Select all.
            TargetTextBox.SelectAll()
        Case Else
            e.SuppressKeyPress = False 'Allow all other key presses to be passed on to the KeyPress event.
    End Select
End Sub

EDIT: Pardon the unintentional similar Regex, Arun Kumar.

Upvotes: 1

Arun Kumar
Arun Kumar

Reputation: 245

The issue with KeyPress is that it calls only one KeyPress at a time. Hence multiple selections such as Ctrl+V, Ctrl+C would not work. Instead of flagging it under KeyPress, call the TextChanged. Add the below mentioned and the issue should be resolved. Copy, Paste and Selecting would now work as normal.

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
    TextBox2.Text = System.Text.RegularExpressions.Regex.Replace(TextBox2.Text, "[^\d]", "")    'Removes all character except numbers
    TextBox2.Select(TextBox2.Text.Length + 1, 1)    'To bring the textbox focus to the right
End Sub

Upvotes: 1

Related Questions