删除所有项目后的组合框大小问题 [英] Combo Box Size Issue After All Items Are Removed

查看:20
本文介绍了删除所有项目后的组合框大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序包含一个 ComboBox,用户可以从中删除项目.当程序启动时,它会从从配置文件中读取的字符串列表中填充 ComboBox.

My application contains a ComboBox that the user can delete items from. When the program starts up it populates the ComboBox from a list of strings read in from a configuration file.

这是添加项目的代码:

// version list is an array of strings
foreach (string version in versionList)
{
  versionComboBox.Items.Add(version);
}
if (versionComboBox.Items.Count > 0)
{
    versionComboBox.SelectedIndex = 0;
}

这是填充后组合框的屏幕截图:

Here is a screenshot of the combo box after it's been populated:

如果用户单击删除按钮,程序会使用以下代码从组合框中删除所选项目:

If the user clicks the Delete button the program removes the selected item from the ComboBox using the following code:

if (versionComboBox.SelectedIndex >= 0)
{
    versionComboBox.Items.Remove(versionComboBox.SelectedItem);
}
if (versionComboBox.Items.Count > 0)
{
    versionComboBox.SelectedIndex = 0;
}

这是移除一些项目后组合框的屏幕截图:

Here is a screenshot of the combo box after a few items have been removed:

我遇到的问题是,当删除最后一个项目时,ComboBox 会将自身调整为最初填充时的大小.ComboBox 中没有任何项目,但它会自行调整大小.

The problem I am having is when the last item is removed the ComboBox resizes itself to the size it was when it was initially populated. There aren't any items in the ComboBox but it sizes itself as if there were.

这是删除所有项目后的屏幕截图:

Here is a screenshot after all the items have been removed:

如您所见,尺寸太大.我认为在清除所有项目后,它会如下所示:

As you can see the size is too big. I would think that after all the items were cleared it would look like the following:

关于为什么会发生这种情况的任何想法?

Any ideas as to why this is happening?

推荐答案

要清除组合框,您可以添加:

To clear your combo box you can add this:

if (versionComboBox.Items.Count == 0)
{
    versionComboBox.Text = string.Empty;
    versionComboBox.Items.Clear();
    versionComboBox.SelectedIndex = -1;
}

另一种方法是操作数据源中的项,每次都重新绑定控件(让您不必担心很多).

Another approach is to manipulate the items in the data source and rebind the control each time (a lot less for you to worry about).

这篇关于删除所有项目后的组合框大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆