gpt4 book ai didi

C# - 设置 ComboBox 的高度

转载 作者:行者123 更新时间:2023-11-30 18:38:31 30 4
gpt4 key购买 nike

我想将 ComboBox(来自 Windows 窗体 Control s)的高度设置为 X在运行时

我有

  • DrawMode 属性设置为 OwnerDrawVariable,以指定项目是手动绘制的;
  • IntegralHeight 属性设置为 false,以避免 Control 自动调整自身大小;
  • ComboBoxItemHeight 属性也设置为 X

我还覆盖了组合框项目的 DrawItemMeasureItem 事件(参见下面的代码)

但是,只有当 DropDownStyle 设置为 Simple 时,在运行时设置 ComboBox 的 Height 才有效。

我以两种方式执行此操作,以编程方式(修改 ComboBoxHeightSize)属性,并使用 PropertyGrid 控制我在我的应用程序上拥有的内容。

当我设置 DropDownDropDownList ComboBox 的高度时,我注意到 Windows 会自动将其高度更改为另一个值:Y(经过一些调试后,由于某种原因我注意到它是 X + 6)。

我错过了什么?为什么会这样?

这是来自处理程序的代码:

private void DrawItemHandler(object sender, DrawItemEventArgs e)
{
//Create a new font to write the item
Font ComboItemFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular);

e.DrawBackground();
e.DrawFocusRectangle();

//Write the item's value
e.Graphics.DrawString(((ComboBox) sender).Items[e.Index].ToString(),
ComboItemFont,
new SolidBrush(Color.Black),
e.Bounds);

//Update the source's font to match the current font
((Control) sender).Font = ComboItemFont;
}

private void MeasureItemHandler(object sender, MeasureItemEventArgs e)
{
//Do nothing
}

最佳答案

 namespace WinForms 
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.Size = new Size(10,10);
}
}
}

您还可以在 Visual Studio 的属性 View 中更改 Size(Width,Height)

关于C# - 设置 ComboBox 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11123614/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com