1

I have a textbox in which the user enters some string. Now, I extract a substring from the main string and display it into a listbox.

For example: If the user enters: SELECT A1,A2,A3 FROM TABLE1 then I need A1,A2 and A3 as separate items of my listbox.

I am able to do that easily, however there's a small problem that is occurring.

As soon as the user finishes typing the word "FROM", the values get added to the listbox and if the user types anything after "FROM"(even a space), the values get added again.

I need the values only once. What am I missing? Please help

private void textBox1_TextChanged(object sender, EventArgs e)
    {

        string substr;
        string str = textBox1.Text.ToString();
        Match m = Regex.Match(str, @"(?<=.\s+).+?(?=\s+from)", RegexOptions.IgnoreCase);
        try
        {
            if (m.Success)
            {
                substr = m.Value;
                string[] sub = substr.Split(',');
                foreach (string x in sub)
                {
                    listBox1.Items.Add(x);
                }
            }
        finally
        {
            listBox1.EndUpdate();
        }

    }

2 Answers 2

2

textBox1_TextChanged Fires on every character being written or removed from the TextBox when the user is typing. You can clear the ListBox every time you want to do this action (The very first action of your above method) :

listBox1.Items.Clear();

I really suggest that you go with another approach, executing a somehow heavy logic on frequent events like TextChanged is not a very good idea. Probably you need a List and each time you compare the results and add only the new ones. Or you can execute this logic on a button click or maybe when the text box loses focus.

Anyway, unless you experience bad performance, you can keep on doing what you doing now.

Sign up to request clarification or add additional context in comments.

Comments

0

You can perform this operation in enter press event in textbox, so user first insert whole text then if he/she press enter button then operation will perform

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
  {
            if (e.KeyCode == Keys.Enter)
            {
                string substr;
                string str = textBox1.Text.ToString();
                Match m = Regex.Match(str, @"(?<=.\s+).+?(?=\s+from)", RegexOptions.IgnoreCase);
               try
               {
                   if (m.Success)
                   {
                       substr = m.Value;
                       string[] sub = substr.Split(',');
                       foreach (string x in sub)
                       {
                           listBox1.Items.Add(x);
                       }
                   }
               }
               finally
               {
                   listBox1.EndUpdate();
               }
            }
  }

and you can perform this operation using a button click as well. Same logic will work there too

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.