Author Topic: Barcode Scanner and FormChange button  (Read 2499 times)

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Barcode Scanner and FormChange button
« on: September 08, 2015, 05:27:31 PM »
In the past, I was using the RTA module from Real Time Automation to read barcode scanning directly into CompactLogix, but with ADHMI,  I can now read or type in the barcode from PC, then pass it on to PLC.
First, I set the KeyPreview in the MainForm_Load, then using KeyPress event to catch the content of barcode and check for valid expression using regular expression.
It works fine until I added a FormChange button to go Form2, since many scanners come with the default setting of sending CR key, now after scanning the barcode, it then open Form2. I like to avoid reprogramming the scanner not to send CR key since it is default setting.
Any ideas of avoiding this situation?  TIA.


Code: [Select]
Private Sub MainForm_KeyPress(sender As Object, e As KeyPressEventArgs)

        e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper())
        Dim pattern As String = "[R]{1}[2]{1}[N]{1}[MPW]{1}[A-Z0-9]{4}"   '//R2NP1234

        Dim regex As Regex = New Regex(pattern)

        If ((e.KeyChar >= ChrW(48) And e.KeyChar <= ChrW(57)) Or (e.KeyChar >= ChrW(65) And e.KeyChar <= ChrW(90))) Then
            If ((barcodeSerial.Length = 0) And (e.KeyChar = "R")) Then
                barcodeSerial = barcodeSerial + e.KeyChar.ToString()
                Label2.Text = barcodeSerial.ToString()

            ElseIf ((barcodeSerial.Length > 0) And (barcodeSerial.Length < 8)) Then
                barcodeSerial = barcodeSerial + e.KeyChar.ToString()
                Label2.Text = barcodeSerial.ToString()
            End If
        End If
        If ((e.KeyChar = ChrW(8)) And (barcodeSerial.Length > 0)) Then           '//if mistake, use the backspace key
            barcodeSerial = barcodeSerial.Substring(0, barcodeSerial.Length - 1)
            Label2.Text = barcodeSerial.ToString()
        End If
        If (regex.IsMatch(barcodeSerial)) Then
            BasicLabel1.Text = barcodeSerial.ToString()
            EthernetIPforCLXCom1.Write("FromPC_Barcode", barcodeSerial)
        End If
       

    End Sub
« Last Edit: September 08, 2015, 05:29:07 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Barcode Scanner and FormChange button
« Reply #1 on: September 08, 2015, 05:56:36 PM »
You can suppress the action of the Enter key like this:
Code: [Select]
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
        If keyData = Keys.Enter Then
            Return True
        Else
            Return MyBase.ProcessCmdKey(msg, keyData)
        End If
    End Function

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: Barcode Scanner and FormChange button
« Reply #2 on: September 08, 2015, 08:25:51 PM »
Thank You Archie.  From what I read, KeyPress event is old school, ProcessCmdKey is better, I found this code , it works for me as well , but I do not understand the (6) and (1) purpose in the code below:
Code: [Select]
    Private scannedBarcode As String = String.Empty
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.A To Keys.Z
                Me.scannedBarcode &= keyData.ToString()
            Case Keys.NumPad0 To Keys.NumPad9
                Me.scannedBarcode &= keyData.ToString()(6)
            Case Keys.D0 To Keys.D9
                Me.scannedBarcode &= keyData.ToString()(1)
            Case Keys.Return, Keys.Enter
                Me.ValidateScannedBarcode()
                Me.scannedBarcode = String.Empty
                Return True
            Case Keys.Back AndAlso scannedBarcode.Length > 0
                Me.scannedBarcode = Me.scannedBarcode.Substring(0, scannedBarcode.Length - 1)
            'Case Keys.Escape
             '   Me.Close()
            Case Else
                'do nothing?
                Return MyBase.ProcessCmdKey(msg, keyData)
        End Select

    End Function

    Sub ValidateScannedBarcode()
        Dim pattern As String = "[R]{1}[2]{1}[N]{1}[MPW]{1}[A-Z0-9]{4}"   '//R2NP1234

        Dim regex As Regex = New Regex(pattern)
        If (scannedBarcode.Length = 8) AndAlso (regex.IsMatch(scannedBarcode)) Then
            If PLCIsConnected Then
                EthernetIPforCLXCom1.Write("FromPC.Barcode", scannedBarcode)
            End If
            Label1.Text = scannedBarcode.ToString()
        End If

    End Sub
« Last Edit: September 11, 2015, 08:35:35 AM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Barcode Scanner and FormChange button
« Reply #3 on: September 08, 2015, 08:51:04 PM »
When a key is pressed on the numeric keypad and "keydata" is converted to a string, it will return "numpadX" where X is the digit. So the 6 tells it to use the character in the 6th position of the string.

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: Barcode Scanner and FormChange button
« Reply #4 on: November 03, 2015, 12:29:46 PM »
Archie, the code below works when I typed it manually, but when using scanner, it only catch the numbers and lower case letters , but not capital letters. How do I fix this?


Code: [Select]
    Private scannedBarcode As String = String.Empty
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.A To Keys.Z
                Me.scannedBarcode &= keyData.ToString()
            Case Keys.NumPad0 To Keys.NumPad9
                Me.scannedBarcode &= keyData.ToString()(6)
            Case Keys.D0 To Keys.D9
                Me.scannedBarcode &= keyData.ToString()(1)
            Case Keys.Return, Keys.Enter
                Me.ValidateScannedBarcode()
                Me.scannedBarcode = String.Empty
                Return True
            Case Keys.Back AndAlso scannedBarcode.Length > 0
                Me.scannedBarcode = Me.scannedBarcode.Substring(0, scannedBarcode.Length - 1)
            'Case Keys.Escape
             '   Me.Close()
            Case Else
                'do nothing?
                Return MyBase.ProcessCmdKey(msg, keyData)
        End Select

    End Function

    Sub ValidateScannedBarcode()
        Dim pattern As String = "[R]{1}[2]{1}[N]{1}[MPW]{1}[A-Z0-9]{4}"   '//R2NP1234

        Dim regex As Regex = New Regex(pattern)
        If (scannedBarcode.Length = 8) AndAlso (regex.IsMatch(scannedBarcode)) Then
            If PLCIsConnected Then
                EthernetIPforCLXCom1.Write("FromPC.Barcode", scannedBarcode)
            End If
            Label1.Text = scannedBarcode.ToString()
        End If

    End Sub
[/quote]
« Last Edit: November 03, 2015, 02:10:31 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Barcode Scanner and FormChange button
« Reply #5 on: November 03, 2015, 05:35:15 PM »
Try this:
Code: [Select]
    Private scannedBarcode As String = String.Empty
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.Return, Keys.Enter
                Me.ValidateScannedBarcode()
                Me.scannedBarcode = String.Empty
                Return True
            Case Keys.Back AndAlso scannedBarcode.Length > 0
                Me.scannedBarcode = Me.scannedBarcode.Substring(0, scannedBarcode.Length - 1)
            Case Else
                'do nothing?
                Return MyBase.ProcessCmdKey(msg, keyData)
        End Select

    End Function

    Protected Overrides Sub OnKeyPress(e As Windows.Forms.KeyPressEventArgs)
        MyBase.OnKeyPress(e)

        If e.KeyChar >= "A" And e.KeyChar <= "z" Then
            scannedBarcode &= e.KeyChar
            e.Handled = True
        End If
    End Sub

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: Barcode Scanner and FormChange button
« Reply #6 on: November 03, 2015, 06:15:17 PM »
LOL, I had to resort back to the KeyPress event. Your code does work , but it needs to catch the numbers as well.
Thanks, Archie.

Code: [Select]
Protected Overrides Sub OnKeyPress(e As Windows.Forms.KeyPressEventArgs)
        MyBase.OnKeyPress(e)

        If e.KeyChar >= "A" And e.KeyChar <= "z" Then
            scannedBarcode &= e.KeyChar
            e.Handled = True
        End If
        If e.KeyChar >= "0" And e.KeyChar <= "9" Then
            scannedBarcode &= e.KeyChar
            e.Handled = True
        End If
    End Sub
« Last Edit: November 03, 2015, 07:31:21 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: Barcode Scanner and FormChange button
« Reply #7 on: November 04, 2015, 09:38:38 AM »
It's very strange, I copy your code to a brand new project v399a, put a label  in there to see the scannedbarcode . tested . It worked. 
then I added some AAHMI controls, no addtional code. try again and it's stop working...
I put a break point inside sub OnKeyPress   and it  never jump there.

I removed all AAHMI controls  and it worked again.

Added one MessageDisplayByValue. still work.

Added one BasicButton.  stop working!
« Last Edit: November 04, 2015, 09:45:16 AM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Barcode Scanner and FormChange button
« Reply #8 on: November 04, 2015, 09:55:55 AM »
Check KeyPreview property on the MainForm and make sure it is True

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: Barcode Scanner and FormChange button
« Reply #9 on: November 04, 2015, 10:52:15 AM »
Yup! I added KeyPreview=True in the formload. Thanks again!
« Last Edit: November 04, 2015, 10:54:47 AM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: Barcode Scanner and FormChange button
« Reply #10 on: November 04, 2015, 04:42:49 PM »
 I found another way to use ProcesscmdKey , it will scan numbers, lowercase as well uppercase.
Code: [Select]
Private scannedBarcode As String = String.Empty
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
      Select Case keyData
           Case Keys.Shift + Keys.A To Keys.Shift + Keys.Z
                Me.scannedBarcode &= keyData.ToString()(0)
            Case Keys.A To Keys.Z
                Me.scannedBarcode &= keyData.ToString()
            Case Keys.NumPad0 To Keys.NumPad9
                Me.scannedBarcode &= keyData.ToString()(6)
            Case Keys.D0 To Keys.D9
                Me.scannedBarcode &= keyData.ToString()(1)
            Case Keys.Return, Keys.Enter
                Me.ValidateScannedBarcode()
                Me.scannedBarcode = String.Empty
                Return True
            Case Keys.Back AndAlso scannedBarcode.Length > 0
                Me.scannedBarcode = Me.scannedBarcode.Substring(0, scannedBarcode.Length - 1)
             Case Else
                'do nothing?
                Return MyBase.ProcessCmdKey(msg, keyData)
        End Select
End Function
« Last Edit: November 04, 2015, 04:45:09 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================