Get line below specific line in textBox multiline (c#)

LeaV 81 Reputation points
2023-09-28T08:41:01.3366667+00:00

Hello everyone.  I want to find the value from texBox1 single line in textBox2 multiline and to get the line below the found line from textBox2 in textBox3.

For example:

If the content in a textBox2 multiline looks like this:

11111111111111
first name
22222222222222
second name
33333333333333
third name
44444444444444
fourth name
55555555555555
fifth name

If the value in textBox1 is:

3333333333333

I would like to get below line in textBox3 (the line from textBox2):

third name

Capture

I tried the following, see the code below. 

This code finds me that line from textBox1 to textBox2, but I need to get the line below the found line to texBox3. 

I am asking for your help. Thanks in advance.

private void searchlinebelow() 
{
  if (textBox2.Text != null)
                {
                    foreach (string line in textBox2.Lines)
                    {
                        if (line == textBox1.Text))
                        {
                            textBox3.Text = // Something like "line.ToString("\r\n");"
                            // In textBox3 I always want a line below "string line"
                        }
                    }
                }
   }
Developer technologies | Windows Forms
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-09-28T09:56:12.6566667+00:00

    Hi @LeaV , Welcome to Microsoft Q&A,

    You have multiple ways to do it, such as using string.split.

    Here you can use textbox.lines.tolist() to directly convert the data into a list.

    Then use indexof to know the corresponding position.

    private void button1_Click(object sender, EventArgs e)
    {
        List<string> list = new List<string>();
        list = textBox2.Lines.ToList();
        string SearchString = textBox1.Text;
        String result = "";
        foreach (string s in list)
        {
            if (s.Contains(SearchString))
            {
                result = list[list.IndexOf(s) + 1];
            }
        }
        textBox3.Text = result;
    }
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. KOZ6.0 6,735 Reputation points
    2023-09-28T11:15:07.2933333+00:00

    foreach is a great method. However, in this case, I think it is better to enumerate using an index.

    string[] lines = textBox2.Lines;
    for (int i = 0; i < lines.Length; i++) {
        string line = lines[i];
        if (line == textBox1.Text) {
            if (i + 1 < lines.Length) {
                textBox3.Text = lines[i + 1];
                break;
            }
        }
    }
    
    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.