dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] Keyascii equivalent in VB.Net? Act on characters as they are entered

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    [RESOLVED] Keyascii equivalent in VB.Net? Act on characters as they are entered

    I'm not a daily programmer so please be gentle with your beatdown

    I have to recreate some very old VB6 code in VB.Net I had code that reads the keypress of a text box. When the input stream sees a "," it clears the textbox as that indicates a new barcode is about to be entered. Then when a "carriage return" ascii char(13) is read in the text box input stream (marking the end), it calls another sub. FYI the text box is filled via a usb barcode scanner programmed to append a CR at the end of the scan. How can I do the code below in VB.Net?

    Code:
    Private Sub igu_txt_KeyPress(KeyAscii As Integer) 'reset igu_txt.text and currentigu if you see a ","
            Dim Numbers As Integer
            Numbers = KeyAscii
            If (Numbers = 44) Then
                igu_txt.Text = ""
                LastIGU = CurrentIGU
                CurrentIGU = ""
                KeyAscii = 0
            End If
            If (Numbers = 13) Then
                Call PrintLabel
            End If
        End Sub
    Thanks for the help!
    Dave
    Last edited by Dnoob; Jan 10th, 2018 at 04:47 PM.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    372

    Re: Keyascii equivalent in VB.Net? Act on characters as they are entered - VB.Net

    Code:
      Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles TextBox1.KeyPress
    
        If e.KeyChar = "," Then
          ' Add code to handle a comma pressed
          e.Handled = True  ' This is the rough equivalent of KeyAscii = 0 above.
        ElseIf e.KeyChar = ControlChars.Cr Then
          ' Add code to handle Enter being pressed
          e.Handled = True  ' This is the rough equivalent of KeyAscii = 0 above.
        End If
    
      End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    Re: Keyascii equivalent in VB.Net? Act on characters as they are entered - VB.Net

    Thank you so much!

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: [RESOLVED] Keyascii equivalent in VB.Net? Act on characters as they are entered

    Also, sort-of-generally:

    If you have a Char and need to know the numeric value that represents it, use ChrW(). It's... not quite accurate to call this ASCII even though for English letters it will have the same value as ASCII. The correct answer's a good bit more complex.
    Code:
    Dim keyValue As Integer = ChrW(e.KeyChar)
    If keyValue = 13 Then
        ' Enter
    End If
    But I very much prefer how OptionBase1 did it, remembering "ControlChars.Cr" is easier than "13" in my opinion. In general, it's sort of tough to figure out certain keypresses from the .NET Keyboard events.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width