问题
I have a ComboBox in WindowsForms and I draw items manually. Each item is composed from picture and text, so item is 34 px height.
I want to set DropDownStyle of ComboBox to DropDownList to enable user input. But when I select some of the item, it is deformed, because picture and text is visible. And I want to display only text if user select some item.
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index > -1)
{
Piece item = this.Items[e.Index] as Piece;
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(e.Bounds.Left + 6, e.Bounds.Top + 6, 22, 22));
e.Graphics.DrawImage(item.Image, new Rectangle(e.Bounds.Left + 7, e.Bounds.Top + 7, 20, 20));
e.Graphics.DrawString(item.Title, e.Font,
new SolidBrush(e.ForeColor), e.Bounds.Left + 34, e.Bounds.Top + 10);
}
e.DrawFocusRectangle();
}
Thanks
回答1:
1) Do you mean DropDownStyle of DropDown? This is the setting which enables user input.
2) What do you mean by 'deformed' - and where are you seeing this?
Edit: If this OnDrawItem call is to render the top box - e.State has the ComboBoxEdit bit flag set. Check for this to render differently.
if( (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit )
{
// Do drawing logic just for the top edit part
}
else
{
// Draw logic here for rendering in the drop-down
}
来源:https://stackoverflow.com/questions/5106736/combobox-dropdownlist-and-items-from-picture-and-text