-1

I am trying to make a search box for my mini project, the way it's supposed to work is that you type in a certain name and it searches the text file for that word, and if no results are found it displays No results.


Text File Includes:

Example1 0

Example2 1

Example2 2

Example4 3

Example5 4


I want to be able to search for the name and it show both the name and age found.

In the text box when button is clicked: Example5 <--- (What I'm searching for)

Some pseudocode:

private void DynamicButton_Click(object sender, EventArgs e)
{
    Text = this.dynamicTextBox.Text;
    // This is how I want it to work:
    // search names.txt for Text 
    // Get the entire line it was found on
    if (found) {
        MessageBox.Show(result);
    }
    else
    {
        MessageBox.Show("No results");
    }
}

End result: A MessageBox that says Example5 4

Community
  • 1
  • 1
Roto
  • 25
  • 1
  • 1
  • 7

3 Answers3

0

Try something like this:

private void DynamicButton_Click(object sender, EventArgs e)
{
    // Assign text local variable
    var searchText = this.dynamicTextBox.Text;

    // Load lines of text file
    var lines = File.ReadAllLines("names.txt");

    string result = null;
    foreach(var line in lines) {
        // Check if the line contains our search text, note this will find the first match not necessarily the best match
        if(line.Contains(searchText)) {
            // Result found, assign result and break out of loop
            result = line;
            break;
        }
    }

    // Display the result, you could do more such as splitting it to get the age and name separately
    MessageBox.Show(result ?? "No results");
}

Note that for this answer to work you will need to import System.IO

J0sh0nat0r
  • 211
  • 2
  • 8
0

You can use File.ReadLines:

private void dynamicButton_Click(object sender, EventArgs e)
{
    string path = "../../text.txt";
    var lines = File.ReadLines(path).ToArray();
    var found = false;
    for (int i = 0; i < lines.Length; i++)
    {
        if (lines[i].Contains(dynamicTextBox.Text))
        {
            label1.Text = i + 1 + "";
            found = true;
        }
    }
    if (!found)
        label1.Text = "Not Found";
}

And in action: enter image description here

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
0

Do it like this:

string[] lines = File.ReadAllLines("E:\\SAMPLE_FILE\\sample.txt");
            int ctr = 0;
            foreach (var line in lines)
            {
                string text = line.ToString();
                if (text.ToUpper().Contains(textBox1.Text.ToUpper().Trim()) || text.ToUpper() == textBox1.Text.ToUpper().Trim())
                {
                    //MessageBox.Show("Contains found!");
                    ctr += 1;
                }

            }
            if (ctr < 1)
            {
                MessageBox.Show("Record not found.");
            }
            else
            {
                MessageBox.Show("Record found!");
            }

or you can use this Expression to minimize your code:

string[] lines = File.ReadAllLines("E:\\SAMPLE_FILE\\sample.txt");         
var tolist = lines.Where(x => (x.ToUpper().Trim() == textBox1.Text.ToUpper().Trim())).FirstOrDefault();
if (tolist != null)
 {
      MessageBox.Show("Record found.");
 }
 else
 {
     MessageBox.Show("Record not found.");
 }
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30