matthew king

matthew king

  • NA
  • 52
  • 0

finding data in a text file based upon a user input(textbox)

Aug 17 2009 3:15 PM

Sir/Ma'am

I have a form with three textboxes (textBox1 thru textBox3).  I also have one button.  My goal is for the user to enter a word into textBox1 and have the user click the button to populate the other two textboxes.  The txt file looks something like this:

Test1,   501, 1050
Test2,   101, 550
Test3,   56, 49
Test4,   57, 909

So, if the user entered Test1 into textBox1, 501 would appear in textBox2 and 1050 in textBox3.  If they entered Test3, 56 and 49 would populate the textBoxes 2 thru 3.  Now, I figured out how to read and parse the first line, but I have been unable to figure out how the enact the word search portion.  Here's what I got...not too impressive.


        private void button1_Click(object sender, EventArgs e)
        {
          
            // I need to work on the search/find            
            // This code will read a line from a text file....
            // I will input the word I'm searching for in this textbox
           //textBox1.Text = objReader.ReadToEnd();
           
            {
                char[] sep = new char[2];
                sep[0] = '\n';
                sep[1] = '\r';
                string[] lines = System.IO.File.ReadAllText(@"c:\test.txt",                                                  System.Text.Encoding.Default).Split(sep, System.StringSplitOptions.RemoveEmptyEntries);

                char[] seps = new char[1];
                //This seperates the values
                seps[0] = ',';
                for (int i = 0; i < lines.Length - 1; i++)
                {
                    string[] data = lines[i].Split(seps, System.StringSplitOptions.RemoveEmptyEntries);
                    textBox1.Text = data[0];
                    textBox2.Text = data[1];
                    textBox3.Text = data[2];
                    textBox4.Text = data[3];
                                                              
                }
            }
        }
    }
}

Answers (3)