winform 组合框清除空条目

winform combobox clear empty entries

我的 WinForms-Combobox 有问题。 我用 BackgroundWorker 填充 Box。 当我调用 comboBox.Items.Clear() 时,组合框的下拉列表的大小仍然像组合框中的项目一样。但是没有文本。当我 运行 后台工作人员再次填充组合框时,每个项目有 2 个条目。当我清除列表并再次 运行 时,有 3 个,依此类推。 好像他们根本没有被清除。

private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (!backgroundWorker.IsBusy)
            {
                var sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
                sqlConnectionStringBuilder.DataSource = textBoxDataSource.Text;
                sqlConnectionStringBuilder.UserID = textBoxUserId.Text;
                sqlConnectionStringBuilder.Password = textBoxPassword.Text;
                sqlConnectionStringBuilder.InitialCatalog = textBoxInitialCatalog.Text;

                backgroundWorker.WorkerReportsProgress = true;
                backgroundWorker.DoWork += Read;
                backgroundWorker.ProgressChanged += Populate;
                backgroundWorker.RunWorkerCompleted += Finish;
                backgroundWorker.RunWorkerAsync(sqlConnectionStringBuilder);
            }
        }

private void Read(object sender, DoWorkEventArgs e)
        {
            var sqlConnectionStringBuilder = e.Argument as SqlConnectionStringBuilder;

            using (var context = new HadesContext(sqlConnectionStringBuilder.ConnectionString))
            {
                var items = context.Items.ToList();

                for (int i = 0; i < items.Count; i++)
                    backgroundWorker.ReportProgress(0, items[i].Name}                
            }
        }

private void Populate(object sender, ProgressChangedEventArgs e)
        {
            progressBarProgress.Value = e.ProgressPercentage;
            comboBoxItems.Items.Add(e.UserState.ToString());
        }

我通过检查值是否已经在集合中解决了重复问题:

if (!myComboBox.Items.Contains(myItem))
     myComboBox.Items.Add(myItem);

顺便说一下,您可以一一尝试:

myComboBox.Items.Remove(myItem);

你的问题是这一行:

backgroundWorker.DoWork += Read;

每次按下按钮,您都会注册一个额外的事件!所以当你第二次按下它时,第一个事件被触发,在它完成读取数据后,第二个事件(你刚刚注册的)被触发并再次读取数据。这就是为什么您的数据会在每次点击时乘以您的点击量。

解决方案是在读取作业完成后再次取消注册,或者(我更愿意)将事件注册放入在开始时调用一次的 Form 的构造函数中:

public Form1()
{
    InitializeComponent();

    backgroundWorker.WorkerReportsProgress = true;
    backgroundWorker.DoWork += Read;
    backgroundWorker.ProgressChanged += Populate;
    backgroundWorker.RunWorkerCompleted += Finish;
}

只保留 SqlConnectionStringBuilder 的行和按钮单击事件中 RunWorkerAsync 的调用:

private void buttonConnect_Click(object sender, EventArgs e)
{
    if (!backgroundWorker.IsBusy)
    {
        var sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
        sqlConnectionStringBuilder.DataSource = textBoxDataSource.Text;
        sqlConnectionStringBuilder.UserID = textBoxUserId.Text;
        sqlConnectionStringBuilder.Password = textBoxPassword.Text;
        sqlConnectionStringBuilder.InitialCatalog = textBoxInitialCatalog.Text;


        backgroundWorker.RunWorkerAsync(sqlConnectionStringBuilder);
    }
}

编辑:

至于诡异的下拉长度ComboBox你可以设置ComboBox.IntegralHeight property to false after you have cleared the item list. This will result in the small dropdown. Disclaimer is that at the next fill it will not open entirely but with a scroll bar. May be this answer可以帮助你更进一步