Click here to Skip to main content
15,941,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

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

        }
Posted
Updated 25-Jul-16 19:19pm
v3

The problem is that it's doing exactly what you told it to!
Every time the text changes, you try to get the data and add it - you don't care if the data exists already. And it's a bigger problem than you probably think: suppose I type
SQL
SELECT A1, B2, A3 FROM TABLE1
and then I realise it should have been "A2" not B2, so I go back and change it by editing the existing:
SQL
SELECT A1, A2, A3 FROM TABLE1
What values should be in your ListBox?
A1, A2, and A3

Or
A1, A2, B2, and A3

Or
A1, B2, A3, A1, A2, and A3

And what if I go back and change all of them for a "second line"? Should the previous ones be removed?

I don't know what your app is, or what exactly it's trying to do - but it needs to be aware that users make mistakes, and it shouldn't generally add things unless the user has indicated they are correct by a positive action.
If you want to add them "on the fly" then you need to keep a list of "added items" and remove them all before you add more until the user indicates he is happy.
 
Share this answer
 
Comments
Ankush Soni 26-Jul-16 1:29am    
Thank you for this great explanation. I got my mistake. :)
add this
C#
if (!listBox1.Items.Contains(x))
listBox1.Items.Add(x);


or

C#
private void textBox1_TextChanged_2(object sender, EventArgs e)
     {
         listBox1.Items.Clear();
.
.
.
 
Share this answer
 
v2
Comments
Patrice T 26-Jul-16 1:19am    
If textbox is edited, list can change.
would be better to delete old list.
Karthik_Mahalingam 26-Jul-16 1:22am    
yes you are rite
Ankush Soni 26-Jul-16 1:29am    
Thanks. Worked:)
Karthik_Mahalingam 26-Jul-16 2:01am    
welcome :)
What about deleting the old listBox1 items before adding the new ones ?
C#
substr = m.Value;
string[] sub = substr.Split(',');
// delete old items here
foreach (string x in sub)
{
    listBox1.Items.Add(x);
}
 
Share this answer
 
v2

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