Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a list data in textbox. Example:
35480745295701728, 416433146831396068 || Carrien
35480709562394675, 379026073325960916 || Lean
96154183216121221, 515151515159611872 || Sunailo
18151851515151818, 815618181215187187

I want to search and show a line on windows form C#. Please help!
Example: search 35480709562394675. It will be show: 35480709562394675, 379026073325960916 || Lean

Thank you very much!

What I have tried:

System.IO.StreamReader ObjReader;
                ObjReader = new System.IO.StreamReader(File_name);

do
       {                    

         Textline = Textline + ObjReader.ReadLine();
         if (Textline.Contains(flag) == true)
         {
             for (int i = 0; i < txtInput.Lines.Length; i++)
               {
                //wrong somethings
               }
         else
         {
            lblOutput.Text = txtInput.Text + " : NOT FOUND";
         }
      }

      while (ObjReader.Peek() != -1);
      ObjReader.Close();
Posted
Updated 4-Jun-19 8:01am

It's difficult to work out exactly what that code is supposed to do, but it's somewhat muddled at best.
You are adding each line you read to TextLine, regardless of what it contains:
Textline = Textline + ObjReader.ReadLine();
You then check that to see if contains a value:
if (Textline.Contains(flag) == true)
If it does, you loop through each character in an unrelated and unchanging string:
for (int i = 0; i < txtInput.Lines.Length; i++)
And ... do nothing with it:
{
 //wrong somethings
}
If it doesn't, you set the output to a "Not Found" message, but showing somethgin your didn't search for:
lblOutput.Text = txtInput.Text + " : NOT FOUND";
And then you go round and to it again for the next line!
If you are trying to find a line that contains a specific value, then sit down and think about it:
1) Set a flag to say "not found".
2) Loop through each line
2.1) If the line contains the text then
2.1.1) Set the "found flag"
2.1.2) Save the line
2.1.3) Exit the loop (hint: break is handy here)
3) After the loop, check the "found" flag
3.1 If it's set, show the line
3.2 If it's still "not found", report an error.

Me? I'd use File.ReadAllLines[^] and then use a foreach loop to make it easier to read.
 
Share this answer
 
Comments
phannhatthanh1510 4-Jun-19 9:58am    
Thank you for your idea. I'm using ReadAllLine and can get correct line

Example full-data:
442226082783011 || 444224591985152 || 04/06/2019 ||
367450779076725 || 455122459198567 || 04/06/2019 ||
756504089869778 || 422364058593394 || 04/06/2019 ||
643453064359261 || 943214058593364 || 04/06/2019 ||
735359076240626 || 432311338593159 || 04/06/2019 || Lainraet
365202075218392 || 424324058593073 || 04/06/2019 ||
535675097553486 || 534525059269665 || 04/06/2019 ||
968563087172661 || 423523338795260 || 04/06/2019 ||

Code:
string path = @"E:\Data.txt";
string[] readText = File.ReadAllLines(path);
foreach (string s in readText)
{
richTextBox1.Text += s.ToString();
if ((s.Contains(txtInput.Text) == true))
{
lblOutput.Text = "found";
break;
}
else
{ lblOutput.Text = "Not Found";
}
}

Search: 365202075218392 and output results:
442226082783011 || 444224591985152 || 04/06/2019 ||
367450779076725 || 455122459198567 || 04/06/2019 ||
756504089869778 || 422364058593394 || 04/06/2019 ||
643453064359261 || 943214058593364 || 04/06/2019 ||
735359076240626 || 432311338593159 || 04/06/2019 || Lainraet
365202075218392 || 424324058593073 || 04/06/2019 ||

I want to search and only show the last line: 365202075218392 || 424324058593073 || 04/06/2019 ||
What should I do? please help me
OriginalGriff 4-Jun-19 10:10am    
Start by reading what I said. Does your code do that, or do what the code you started with did?
Thank you very much! It's working with this

string File_name = @"E:\Data.txt";
            string flag = txtInput.Text.Trim(); 
            string[] readText = File.ReadAllLines(File_name);
            foreach (string s in readText)
            {               
                if ((s.Contains(txtInput.Text) == true))
                {
                    richTextBox1.Text = s.ToString();
                    break;                   
                }
                else
                {
                    richTextBox1.Text = "Not Found";
                }
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900