ComboBox DropDownList and items from picture and text

你离开我真会死。 提交于 2019-12-13 03:39:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!