首页 > 解决方案 > 对于第一个 TextBox txtName,它显示一个下拉列表,但对于另一个 TextBox,我没有得到任何下拉列表

问题描述

下面的代码是 .net windows 窗体应用程序,当用户在其中键入内容时,显示 2 个带有下拉建议的文本框。第一个 TextBox txtName 的代码正在工作,但另一个不是....我需要对此有所帮助。

private void txtName_TextChanged(object sender, EventArgs e)
    {
        connection = new SqlConnection();
        connection.ConnectionString = "Data Source=DESKTOP-H1PE2LT;Initial Catalog=HealthandWellness;Integrated Security=True";
        connection.Open();
        cmd = new SqlCommand("SELECT [Medicine Name] FROM Details", connection); 
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
            collection.Add(reader["Medicine Name"].ToString());
        }
    }


private void textBox1_TextChanged(object sender, EventArgs e)
    {
        connection = new SqlConnection();
        connection.ConnectionString = "Data Source=DESKTOP-H1PE2LT;Initial Catalog=HealthandWellness;Integrated Security=True";
        connection.Open();
        cmd = new SqlCommand("SELECT [Medicine Name] FROM Details", connection);
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            AutoCompleteStringCollection collection1 = new AutoCompleteStringCollection();
            collection1.Add(reader["Medicine Name"].ToString());
        }
    }

标签: c#

解决方案


要说其中一个有效而另一个无效,让我相信您已经在 gui 属性中设置了一些东西,因为我看不到您在哪里将此 stringcollection 设置为您的 autocompletecustomsource。话虽如此,让我们使用 2 个新文本框并设置一个演示,您可以在自己的机器上进行测试。

对于 AutoCompleteStringCollection 的构建,我将只使用一个列表,但您将使用您的查询。我将尝试指出您将在哪里使用这些方法。

由于您对两个文本框使用相同的数据,因此我们只需要创建一次集合。我们将在表单加载中执行此操作。

    private void Form1_Load(object sender, EventArgs e)
    {
        //this would be replaced with your query results from the database
        string[] input = { "able", "alpha", "baker", "bravo", "charlie", "charcoal", "dog", "delta", "eagle", "foxtrot" };
        List<string> pretendThisCameFromAQuery = new List<string>(input);

        //define an AutoCompleteStringCollection. Notice it is outside of the loop as you want to add to this with each iteration of the read() or in this case the foreach loop i'm using.
        AutoCompleteStringCollection collection = new AutoCompleteStringCollection();

        //here is where your While reader.read() would go
        foreach (string item in pretendThisCameFromAQuery)
        {
            collection.Add(item);
            //your code here would be something like
            //collection.Add(reader["Medicine Name"].ToString());
        }

        //so now we have a collection. Who wants to use it.

        //for txtName1, we'll set it up to just suggest.

        txtName1.AutoCompleteCustomSource = collection;
        txtName1.AutoCompleteMode = AutoCompleteMode.Suggest;
        txtName1.AutoCompleteSource = AutoCompleteSource.CustomSource;

        //for textbox2 we'll use suggestappend.
        //notice no new collection needed.
        textBox2.AutoCompleteCustomSource = collection;
        textBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }

请注意,此代码不在任何文本框事件中。特别是 TextChanged 事件,因为它会随着对文本框的每次更改而运行,我们不需要这样做,因为我们只需要填充集合并将其分配给我们想要使用集合之前的文本框需要使用下拉菜单。

同样,如果您运行此演示,我强烈建议您添加两个新的文本框,命名为我命名的,因为我真的觉得您在编写代码之外做了一些事情来让第一个工作。


推荐阅读