我在这里遇到一个奇怪的问题,我的组合框将其样式从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
,但只有底部的一个手动绘制了项目:
有没有办法克服这个问题?
答案 0 :(得分:0)
试试这段代码:
ddlInterval.DrawMode = DrawMode.Normal;
而不是:
ddlInterval.DrawMode = DrawMode.OwnerDrawFixed;