CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2016
    Posts
    13

    when split msg variable to more lines it not give me values in lines?

    Problem

    When split msg variable to more lines it not give me values in lines ?
    msg variable in debug give me text scanning bellow

    Details

    i work in windows form c# vs 2015

    i using bar code reader to read qr code

    bar code scanner working as USB keyboard and define as HID Drivers

    if i replace msg variable with text box it working in read data and spiting

    SUCCESS so that

    what is the problem in splitting below code ?

    text scanning :

    30 General Conference of Arab Pharmaceutical Unions

    UserName : khalid ramzy

    Country : Saudia

    Membership : part

    Serial : 1014

    my code


    Code:
    public partial class Form1 : Form
        {
            DateTime _lastKeystroke = new DateTime(0);
            List<char> _barcode = new List<char>(10);
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
                if (elapsed.TotalMilliseconds > 100)
                    _barcode.Clear();
    
                // record keystroke & timestamp
                _barcode.Add(e.KeyChar);
                _lastKeystroke = DateTime.Now;
    
                // process barcode
                if (e.KeyChar == 13 && _barcode.Count > 0)
                {
    
                        string msg = new String(_barcode.ToArray());
    
                    string[] lines = msg.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
    
                    if (lines.Length > 5)
                    {
                        string msg1 = lines[1].Substring(lines[1].IndexOf(":") + 1);
                        label3.Text = msg1;
                        string msg2 = lines[2].Substring(lines[2].IndexOf(":") + 1);
                        label4.Text = msg2;
                        string msg3 = lines[3].Substring(lines[3].IndexOf(":") + 1);
                        label5.Text = msg3;
                        string msg4 = lines[4].Substring(lines[4].IndexOf(":") + 1);
                        label6.Text = msg4;
                        label2.Text = lines[5].Substring(lines[5].IndexOf(":") + 1);
                    }
                        _barcode.Clear();
                }
            }
        }
    }
    msg variable in debug have values of scanning qr code

    but problem now

    How to split them to lines as above ?
    Last edited by 2kaud; February 24th, 2017 at 09:12 AM. Reason: Added code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: when split msg variable to more lines it not give me values in lines?

    I would recommend that you create a console test app with a hardcoded 'sample' barcode string so you can step through and debug your code in order to get your parsing code working.

    That being said, is the barcode format exactly what you have posted above? If so, I'm counting 9 lines of data, not 5 (assuming the first line begins with "30 General..."). When you split, each line will be returned in the array even if the line is empty. You can change this behavior by specifying a different StringSplitOptions value other than None.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured