0

How to achive searching for multiple values in a textbox.

Ex: Using~s

enter image description here

This code can achieve only a single search in the text box.

    private void button1_Click(object sender, EventArgs e)
    {
        int index = 0;
        string temp = richTextBox1.Text;
        richTextBox1.Text = "";
        richTextBox1.Text = temp;
        while (index < richTextBox1.Text.LastIndexOf(textBox1.Text))
        { 
            richTextBox1.Find(textBox1.Text, index, richTextBox1.TextLength, RichTextBoxFinds.None);
            richTextBox1.SelectionBackColor = Color.Yellow;
            index = richTextBox1.Text.IndexOf(textBox1.Text, index) + 1;
        }
        this.timer1.Start();
    }
Nishant Bangera
  • 176
  • 1
  • 1
  • 8

1 Answers1

0

This can be achieved by using the split keyword.

 private void button1_Click(object sender, EventArgs e)
        {
            int index = 0;
            string temp = richTextBox1.Text;
            richTextBox1.Text="";
            richTextBox1.Text = temp;
            this.timer1.Start();
            string[] names = textBox1.Text.Split('~');
            int count = 0;
                while (index < richTextBox1.Text.LastIndexOf(names[0]))
                {
                    richTextBox1.Find(names[0], index, richTextBox1.TextLength, RichTextBoxFinds.None);
                    richTextBox1.SelectionBackColor = Color.Yellow;
                    index = richTextBox1.Text.IndexOf(names[0], index) + 1;
                count++;
                }

                int count2 = 0;
                while (index < richTextBox1.Text.LastIndexOf(names[1]))
                {
                    richTextBox1.Find(names[1], index, richTextBox1.TextLength, RichTextBoxFinds.None);
                    richTextBox1.SelectionBackColor = Color.LightBlue;
                    index = richTextBox1.Text.IndexOf(names[1], index) + 1;
                    count2++;
                }
       }
Nishant Bangera
  • 176
  • 1
  • 1
  • 8