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

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 ?

Recommended Answers

All 3 Replies

Arrays in C# have a zero-based index. That means lines[0] will get you the first line, lines[1] the second, and so on.

30 General Conference of Arab Pharmaceutical Unions
UserName : khalid ramzy
Country : Saudia
Membership : part
Serial : 1014

You posted 5 lines in your output example, but in your check if (lines.Length > 5) you won't start splitting until there are more than 5 lines. Note that label2.Text = lines[5].Substring(lines[5].IndexOf(":") + 1); will try to perform operations on a 6th line, which according to the example doesn't exist.

However, if you only changed those lines to go from 0-4 instead of 1-5 you would run into another problem:

There is no : in the first line, so when you want a substring to start on the index position of a : it won't be able to find that. Note that in the documentation for IndexOf it specifies

from the start of the current instance if that string is found, or -1 if it is not.

And that Substring will throw an exception if it gets passed -1:

ArgumentOutOfRangeException - startIndex is less than zero or greater than the length of this instance.

what i do to solve that and recieve data if possible

Well, where it says lines[1] you put in lines[0], replace lines[2] with lines[1] and so on.

Change the check for >5 to ==5.

And finally, don't split on : on the first line or it will throw an error. I don't think you meant to split that line anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.