C# 异步更改ListBox值

C# 异步更改ListBox值,c#,listbox,C#,Listbox,我正处于这样的境地,我想知道这是否可能 我想根据TextBox值更改ListBox控件的项 意味着当我更改textbox值时,ListBox的listitems会异步更改(无需按下按钮或其他操作) 可能吗?如果可能的话,请给我一些参考资料、教程或其他东西 我希望下面的代码能帮助您: protected void TextBox1_TextChanged(object sender, EventArgs e) { if(TextBox1.Text == "One") li

我正处于这样的境地,我想知道这是否可能

我想根据TextBox值更改ListBox控件的项

意味着当我更改textbox值时,ListBox的listitems会异步更改(无需按下按钮或其他操作)


可能吗?如果可能的话,请给我一些参考资料、教程或其他东西

我希望下面的代码能帮助您:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

    if(TextBox1.Text == "One")
        listBox1.Items.Add("One");

    if(TextBox1.Text == "two")
        listBox1.Items.Add("two");

}

我希望下面的代码将帮助您:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

    if(TextBox1.Text == "One")
        listBox1.Items.Add("One");

    if(TextBox1.Text == "two")
        listBox1.Items.Add("two");

}

您不必异步执行该操作。只需使用TextBox.TextChanged事件。

您不必异步执行该操作。只需使用TextBox.TextChanged事件。

并不能完全满足您的需要,但我希望下面的案例能够帮助您
案例1:如果您有一个包含多个项目的列表框,并且希望选择的项目与文本框中的文本相匹配。如果是这种情况,下面的代码应该执行此操作

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.TextLength >= 1)
        {
            int index = listBox1.FindString(textBox1.Text);//Search any items that match Textbox's text.
            listBox1.SelectedIndex = index;//Highlight the match
        }
        else
        {
            listBox1.SelectedIndex = 0;
        }
    }
将上面的代码添加到文本框的TextChangedEvent中,并确保使用您拥有的控件重命名代码中的控件。如果不是这样,请参阅下面的控件

案例2:你有一个文本框,你想将文本框的文本添加到列表框中。我想告诉你的是,下面的代码假设当你在文本框聚焦时按下回车键时,它的文本(如果有的话)应该添加到列表框中。在文本框的keydown事件中添加下面的代码,并确保重命名控件。如果是这种情况,下面的代码将帮助您

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        string item = textBox1.Text;
        if (textBox1.Text.Length >= 1)
        {
            if (e.KeyCode == Keys.Enter)//If Enter key is pressed while textbox is focused.
            {
                listBox1.Items.Add(item);
            }
        }
    }

希望这对您有所帮助。

并非完全满足您的需求,但我希望下面的案例能够帮助您
案例1:如果您有一个包含多个项目的列表框,并且希望选择的项目与文本框中的文本相匹配。如果是这种情况,下面的代码应该执行此操作

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.TextLength >= 1)
        {
            int index = listBox1.FindString(textBox1.Text);//Search any items that match Textbox's text.
            listBox1.SelectedIndex = index;//Highlight the match
        }
        else
        {
            listBox1.SelectedIndex = 0;
        }
    }
将上面的代码添加到文本框的TextChangedEvent中,并确保使用您拥有的控件重命名代码中的控件。如果不是这样,请参阅下面的控件

案例2:你有一个文本框,你想将文本框的文本添加到列表框中。我想告诉你的是,下面的代码假设当你在文本框聚焦时按下回车键时,它的文本(如果有的话)应该添加到列表框中。在文本框的keydown事件中添加下面的代码,并确保重命名控件。如果是这种情况,下面的代码将帮助您

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        string item = textBox1.Text;
        if (textBox1.Text.Length >= 1)
        {
            if (e.KeyCode == Keys.Enter)//If Enter key is pressed while textbox is focused.
            {
                listBox1.Items.Add(item);
            }
        }
    }

希望这对您有所帮助。

连接
TextChanged
事件并更改您的列表框值。您已完成查找
TextChanged
事件并更改列表框值。谢谢你的帮助。@Ankhbayarbayarsaikan如果答案对你有帮助,请投上一票,以便其他有相同问题的人能轻松找到解决方案。谢谢你的帮助。@Ankhbayarbayarsaikan如果答案对你有帮助,请投上一票,以便其他有相同问题的人能轻松找到解决方案。