lundi 31 décembre 2018

How to get out of a loop

I'm reading a text file. If a specific string is not in the file, I'm trying to report a message. I keep getting the same message over again, or, not at all depending on where I put it.

I have moved this (code just above counter++; near the bottom) "else lbOne.Items.Add(txtID.Text + " does not exist.");" around to every conceivable line with no luck. I know just enough to be dangerous, so could use some help with this.

    if (rButtonFind.Checked)
        {
            int counter = 0;
            string line;
            System.IO.StreamReader file = new System.IO.StreamReader(@"F:\Quality\CMM Fixtures\fixtures.txt");

            if (new FileInfo(@"F:\09 Quality\CMM Fixtures\fixtures.txt").Length == 0)
            {
                MessageBox.Show("There is no data in the file to search." + "\n" + "There must be at least one fixture" + "\n" + " to not see this message.");
            }

                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains(txtID.Text))
                    {
                        lbOne.Items.Add(line);
                    }
                    else
                    lbOne.Items.Add(txtID.Text + " does not exist.");
                    counter++;
                }           

                file.Close();                
            }         
        }

As I said, it either lists the "does not exist" message many times, or not at all.

Aucun commentaire:

Enregistrer un commentaire