ComboBox在手动绘制项目时更改样式

时间:2013-05-07 08:35:41

标签: c# winforms combobox

我在这里遇到一个奇怪的问题,我的组合框将其样式从DropDownList更改为DropDown并且仅在视觉上。检查我的组合框的DropDownStyle属性会将其设置为DropDownList

基本上这就是我在做的事情:

void InitComponents()
{
            ddlInterval.DrawMode = DrawMode.OwnerDrawFixed;
            ddlInterval.DrawItem += new DrawItemEventHandler(DrawIntervalItems);
            List<IntervalItem> backupIntervalsList = new List<IntervalItem>();
            IntervalsList.Add(new IntervalItem(5, GetIntervalString(5)));
            IntervalsList.Add(new IntervalItem(15, GetIntervalString(15)));
            IntervalsList.Add(new IntervalItem(30, GetIntervalString(30)));
            IntervalsList.Add(new IntervalItem(60, GetIntervalString(60)));
            IntervalsList.Add(new IntervalItem(120, GetIntervalString(120)));
            IntervalsList.Add(new IntervalItem(240, GetIntervalString(240)));
            IntervalsList.Add(new IntervalItem(480, GetIntervalString(480)));
            IntervalsList.Add(new IntervalItem(720, GetIntervalString(720)));
            IntervalsList.Add(new IntervalItem(1440, GetIntervalString(1440)));
            ddlInterval.DataSource = IntervalsList;
            ddlInterval.DisplayMember = "Name";
            ddlInterval.ValueMember = "Value";
}

 void DrawIntervalItems(object sender, DrawItemEventArgs e)
        {
            if(_bDisableItems)
            {
                switch (e.Index)
                {
                    case 5:
                    case 7:
                    case 8:
                        {
                            string s = ddlInterval.GetItemText(ddlInterval.Items[e.Index]);
                            e.DrawBackground();
                            e.Graphics.DrawString(s, ddlInterval.Font, Brushes.Black, e.Bounds);
                        }
                        break;
                    default:
                        {

                            string s = ddlInterval.GetItemText(ddlInterval.Items[e.Index]);
                            e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                            e.Graphics.DrawString(s, ddlInterval.Font, Brushes.LightSlateGray, e.Bounds);
                        }
                        break;
                }
            }
        }

这是一张显示我的两个组合框之间差异的图像。请注意,两者都将DropDownStyle设置为DropDownList,但只有底部的一个手动绘制了项目:

Difference between comboboxes

有没有办法克服这个问题?

1 个答案:

答案 0 :(得分:0)

试试这段代码:

 ddlInterval.DrawMode = DrawMode.Normal;

而不是:

 ddlInterval.DrawMode = DrawMode.OwnerDrawFixed;