I have made an application which saves and loads data from a text file. What I want to do is search for a string, display that string as well as the two strings after in the appropriate places of the WPF. I think it is finding the string okay because the counter is displaying correctly however none of the strings are displaying. Here is my search method:
public void Search(string searchTerm)
{
var lineCount = File.ReadLines("products.txt").Count();
string line;
int counter = 0;
System.IO.StreamReader file = new System.IO.StreamReader("products.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains(searchTerm))
{
break;
}
counter++;
}
textBlock.Text = counter.ToString();
string[] allLines = File.ReadAllLines("products.txt");
allLines[counter] = productNameBlock.Text;
allLines[counter + 1] = customerNameBlock.Text;
allLines[counter + 2] = firmwareBlock.Text;
}
any ideas?
I think you flipped the assignment:
productNameBlock.Text = allLines[counter];
customerNameBlock.Text = allLines[counter + 1];
firmwareBlock.Text = allLines[counter + 2];
Second, you're not using lineCount
so just remove that extra reading line.
Why didn't you use the search function I provided you in your last question ? Could have done this easily. Anyway...
public void Search(string searchTerm)
{
var allLines = File.ReadAllLines("products.txt");
int nonMatchingLineCount = allLines.Where(line => !line.Contains(searchTerm)).Count();
textBlock.Text = nonMatchingLines.Count().ToString();
productNameBlock.Text = allLines[counter];
customerNameBlock.Text = allLines[counter + 1];
firmwareBlock.Text = allLines[counter + 2];
}
Seems like you have this reversed
allLines[counter] = productNameBlock.Text;
allLines[counter + 1] = customerNameBlock.Text;
allLines[counter + 2] = firmwareBlock.Text;
What if you did this instead?
productNameBlock.Text = allLines[counter];
customerNameBlock.Text = allLines[counter + 1];
firmwareBlock.Text = allLines[counter + 2];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.