Help. Value of .Text not updating on "_KeyPress" event

Mugsy's RapSheet 196 Reputation points
2023-09-28T19:12:46.7033333+00:00

I'm trying to parse input to a textbox in real time.

I'm trying to restrict input to digits from 1 to 9 (no zeros). When the form loads, it pre-fills with the last used entry.

But if the user backs over it to change it, the value of ".Text" is still the initial loaded entry.

Private Sub tbxID_KeyPress(sender As Object, e As KeyPressEventArgs) Handles tbxID.KeyPress
     If e.KeyChar.ToString = vbBack Then e.Handled = False
     If e.KeyChar.ToString < "1" Or e.KeyChar.ToString > "9" Then e.Handled = True ' Anything but a number, ignore.
     ....
     ' Read the value of tbxID.Text. If backed over, it should return "" but doesn't. Still returns org value.

I tried "tbxID.Update()" and "tbxID.Refresh()", but neither had any effect (or is that "affect"?)

TIA

Developer technologies VB
{count} votes

Accepted answer
  1. KOZ6.0 6,655 Reputation points
    2023-09-29T02:15:53.32+00:00

    The contents of the input key are not reflected in the KeyPress event. You need to predict the string after it is reflected.

    Use SelectionStart and SelectionLength properties to separate the text to the left and right. When you enter a normal character, reflected text will be left + input character + right.

    Private Sub tbxID_KeyPress(sender As Object, e As KeyPressEventArgs) _
                                    Handles tbxID.KeyPress
        If e.KeyChar = vbBack Then
            e.Handled = False
            Return
        End If
        If e.KeyChar < "1"c Or e.KeyChar > "9"c Then
            e.Handled = True
            Return
        End If
        Dim tbox As TextBox = DirectCast(sender, TextBox)
        Dim prevText As String = tbox.Text
        Dim leftStr As String = prevText.Substring(0, tbox.SelectionStart)
        Dim rightStr As String =
            prevText.Substring(tbox.SelectionStart + tbox.SelectionLength)
        Dim reflectedText As String = leftStr & e.KeyChar & rightStr
    End Sub
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Dewayne Basnett 1,381 Reputation points
    2023-09-29T16:20:00.67+00:00

    Replace KeyPress with KeyDown. This stops backspace key and Del key.

        Private Sub tbxID_KeyDown(sender As Object, e As KeyEventArgs) Handles tbxID.KeyDown
            Dim tb As TextBox = DirectCast(sender, TextBox)
            If e.KeyCode = Keys.Back Then
                'stop backspace 
                tb.Text = tb.Text & " " 'add a space at end
                tb.Select(tb.TextLength - 1, 1) 'select the space
            ElseIf e.KeyCode = Keys.Delete Then
                'stop del
                tb.Text = tb.Text & " " 'add a space at end
                tb.Select(tb.TextLength - 1, 0) 'select before space
            ElseIf e.KeyCode > Keys.D0 AndAlso e.KeyCode <= Keys.D9 Then
                e.Handled = True
            End If
        End Sub
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.