添加或删除项目时,ComboBox.DropDownHeight 值不会更改

问题描述 投票:0回答:2

在 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
时,明显的下拉区域会发生明显变化。
另一位网友提问

移除所有物品后组合框尺寸问题

提供了一个关于如何在删除项目后调整明显下拉区域大小的有点令人困惑的答案。那么

DropDownHeight
属性的用途是什么?是什么改变了
ComboBox
的明显下拉区域?

c# winforms combobox
2个回答
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();
}

0
投票

从属性面板将 autoheight 属性更改为 false,然后尝试更改高度。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.