Home > Software engineering >  Search Multiple Values through one TextBox
Search Multiple Values through one TextBox

Time:07-04

How to achive searching for multiple values in a textbox.

Ex: =,Using

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();
    }

CodePudding user response:

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 strtemp = textBox1.Text.Replace(Environment.NewLine, "~");
            string[] names = strtemp.Split(' ');
            //richTextBox1.Clear();   
            foreach (string name in names)
            {
                //richTextBox1.Text = name;
                while (index < richTextBox1.Text.LastIndexOf(name))
                {
                    //searches the text in a RichtextBox control for a string within a range of text  
                    richTextBox1.Find(name, index, richTextBox1.TextLength, RichTextBoxFinds.None);
                    //selection color.This is added automatically when it's selected
                    richTextBox1.SelectionBackColor = Color.Yellow;
                    //After a match is found the index is increased so the search won't stop at the same
                    index = richTextBox1.Text.IndexOf(name, index)   1;
                }
            }
  • Related