3

我想在 runtime 期间ComboBox将 a (从 Windows Forms Control s)的高度设置为X

我有

  • DrawModeProperty 设置为OwnerDrawVariable,以指定手动绘制项目;
  • IntegralHeightProperty 设置为 false,以避免Control自动调整大小;
  • ItemHeightPropertyComboBox也设置为X

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

但是,在运行时设置ComboBox 的 仅在设置为时才有效。HeightDropDownStyleSimple

我这样做是两种方式,以编程方式(修改ComboBox'sHeightSize)属性,并使用PropertyGrid我在我的应用程序上拥有的控件。

DropDown当我设置 a or的高度时DropDownList 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
}
4

1 回答 1

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

您还可以Size(Width,Height)在 Visual Studio 中更改来自属性视图

于 2012-06-20T16:35:59.753 回答