I want to set the height of a ComboBox (from the Windows Forms Controls) to X during runtime.
I have
DrawMode Property set to OwnerDrawVariable, to specify that the
items are drawn manually;IntegralHeight Property set to false, to avoid the Control from automatically resizing itself;ItemHeight Property of the ComboBox also set to X.I've also overriden the DrawItem and MeasureItem events for the comboBox's items (see code below)
However, setting the Height of the ComboBox at runtime only works if the DropDownStyle is set to Simple.
I do this both ways, programmatically (modifying the ComboBox's Height or Size) property, and using a PropertyGrid Control which I have on my App.
When I set the height of a DropDown or DropDownList ComboBox, I noticed that Windows automatically changes its height to another value: Y (which, after some debugging I noticed to be X + 6 for some reason).
What am I missing? Why does this happen?
Here is the code from the handlers:
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);
}
}
}
You can also change the Size(Width,Height) from properties view in Visual Studio
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With