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];
}
}
}
}
}
Loading
Roei BarPosted Aug 17, 2009, 4:20 PM
if you liked this code, dont forget to mark it as "Accpeted Answer"
Kirtan PatelPosted Aug 17, 2009, 4:40 PM
Here is Fully Tested Code I have Programmed without any counter looping and any FileOpen :)
Code is Very Simple and Compact :)
please dont forget to mark "Do you like answer" it will give me some credits :)
private void btnSearch_Click(object sender, EventArgs e)
{
// Define File Path
string filepath = Environment.CurrentDirectory +@"\Data.txt";
//Get All lines in Array
string[] Data = File.ReadAllLines(filepath, Encoding.UTF8);
string SearchText = txtSearch.Text.Trim();
if (SearchText != "")
{
foreach (string line in Data)
{
// If Value Found in Line
if (line.Contains(SearchText) == true)
{
string[] words = line.Split(',');
//Show it in text boxes
textBox2.Text = words[1].Trim();
textBox3.Text = words[2].Trim();
}
}
}
}
matthew kingPosted Aug 17, 2009, 4:31 PM