将子字符串从文本框添加到列表框

问题描述:

我有一个文本框,用户在其中输入一些字符串。现在,我从主字符串中提取子字符串并将其显示到列表框中。

例如:

如果用户输入:SELECT A1,A2,A3 FROM TABLE1

然后我需要A1,A2和A3作为我的列表框的单独项目。



我能够轻松地做到这一点,但是有一个小的正在发生的问题。

一旦用户输入单词FROM,值就会被添加到列表框中,如果用户在FROM(即使是空格)后键入任何内容,值再次添加。

我只需要一次值。我错过了什么?请帮助



我尝试过的事情:



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:

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

        }

添加此

add this
if (!listBox1.Items.Contains(x))
listBox1.Items.Add(x);











or

private void textBox1_TextChanged_2(object sender, EventArgs e)
     {
         listBox1.Items.Clear();
.
.
.


问题在于它正在按照你告诉它做的!

每次文本更改时,您尝试获取数据并添加它 - 您不关心数据是否已存在。这是一个比你想象的更大的问题:假设我输入

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
SELECT A1, B2, A3 FROM TABLE1

然后我意识到应该是A2而不是B2,所以我回去通过编辑现有的来改变它:

and then I realise it should have been "A2" not B2, so I go back and change it by editing the existing:

SELECT A1, A2, A3 FROM TABLE1

ListBox中应包含哪些值?

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.


如何删除旧的 listBox1 添加新项目之前的项目?

What about deleting the old listBox1 items before adding the new ones ?
substr = m.Value;
string[] sub = substr.Split(',');
// delete old items here
foreach (string x in sub)
{
    listBox1.Items.Add(x);
}