C# - 设置 ComboBox 的高度

标签 c# combobox height

我想将 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/

相关文章:

java - 包含 JCheckBoxes 的 JTable 的关键监听器

combobox - 微软访问: Display two columns in combo-box

jquery - jQuery 的 height() 和 width() 方法是否保持图像的比例?

css - 如何使 Bootstrap .card-img-top 在响应宽度下等高?

c# - 违反了哪些 SOLID 原则? (我应该怎么做才能解决这个 Controller 问题?)

C# Winform 应用程序无法在其他计算机上运行(启动时神秘挂起)

wpf - 使用 DataTemplate 将 View 连接到 ViewModel

c# - 使用 udpClient 连续接收消息

c# - 最好用的 URL 重写器是什么?

html - <td> 上的溢出属性不会创建滚动条