【问题标题】:ComboBox.DropDownHeight values doesn't change when adding or removing items添加或删除项目时,ComboBox.DropDownHeight 值不会改变
【发布时间】:2015-07-13 19:07:21
【问题描述】:

在 WFA 中运行以下代码时

public partial class Form1 : Form
{
    string[] items = { "A", "B", "C", "D", "E" };

    public Form1()
    {
        InitializeComponent();
        UpdateDropDownHeight();
    }

    private void UpdateDropDownHeight()
    {
        textBox1.Text = comboBox1.DropDownHeight.ToString();
    }

    private void button_populate_Click(object sender, EventArgs e)
    {
        for(int i = 0; i<items.Length; i++)
        {
            comboBox1.Items.Add(items[i]);
        }
        UpdateDropDownHeight();
    }

    private void button_clear_Click(object sender, EventArgs e)
    {
        comboBox1.Items.Clear();
        UpdateDropDownHeight();
    }
}

我注意到combobox1.DropDownHeight 值在将新项目添加到组合框中时永远不会改变。单击button_populate 时,明显的下拉区域明显改变。
另一个用户的问题

Combo Box Size Issue After All Items Are Removed

就如何在删除项目后调整明显的下拉区域大小提供了一个有点令人费解的答案。那么DropDownHeight 属性的用途是什么?是什么改变了ComboBox 的明显下拉区域?

【问题讨论】:

  • IntegralHeight 属性有时也会发挥作用。不过,在大多数情况下,大多数设计师都希望增加下拉菜单的高度,以便在默认情况下显示更多选择。我不确定您要解决什么问题。
  • 当我填充然后清除组合框中的项目并单击下拉菜单时,我在下拉菜单中看到 5 个空格。我想知道是否有一种简单的方法可以输入类似 comboBox1.dropDownHeight = value 来调整下拉菜单的可视范围。
  • 尝试将 IntegralHeight 设置为 false。
  • 完美。这完全实现了我想要实现的目标。我仍然不清楚为什么当 ComboBox 自动调整下拉区域的大小时 DropDownHeight 值没有改变。

标签: c# winforms combobox


【解决方案1】:

也不太确定你想解决什么问题,但是如果你想调整 DropDownHeight 的高度,这可以在下面实现。

private void UpdateDropDownHeight()
{
    int dropDownHeight = 0;
    for (int i = 0; i <= comboBox1.Items.Count; i++)
    {
        dropDownHeight = dropDownHeight + (comboBox1.ItemHeight);
    }
    comboBox1.DropDownHeight = dropDownHeight;
    textBox1.Text = comboBox1.DropDownHeight.ToString();
}

【讨论】:

  • 谢谢。这解决了我的问题。看来 DropDownHeight 属性仅在从我的代码中设置时才会更改。
  • 我没有收到您的代码。为什么要在循环中添加项目高度?我问这个是因为如果存在 Items.Count 超过 2 的情况,那么每次迭代 ItemsHeight 都会添加到 dropDownHeight。
猜你喜欢
  • 2023-04-01
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
相关资源
最近更新 更多