I wrote a class that inherits ComboBox to create a dropdown that contains
both an image and a text description. This is for an IE browser toolbar. I
use the OwnerDrawFixed method to do this.
The first time click the arrow for the drop down, and shows an empty
dropdown, and immediately closes it. The second time I click the arrow, it
works perfectly. It does this same behavior on Vista, XP, IE6 and IE7.
I'm stumped, I'm sure there is some type of a timing problem, but I'll be
darned if I can figure out why the drop down area is not drawn the first time
I click the arrow. Any help greatly appreciated.
protected override void OnDrawItem(DrawItemEventArgs ea)
{
try
{
int spacer = 5;
ImageComboItem item = (ImageComboItem)Items[ea.Index];
float width = item.Icon.Width + spacer +
ea.Graphics.MeasureString(item.Text, ea.Font).Width + spacer;
if(width this.DropDownWidth)
{
this.DropDownWidth = Convert.ToInt32(Math.Ceiling(width));
}
ea.DrawBackground();
ea.DrawFocusRectangle();
Rectangle bounds = ea.Bounds;
ea.Graphics.DrawImage(item.Icon, bounds.Left, bounds.Top,
item.Icon.Width, item.Icon.Height);
ea.Graphics.DrawString(item.Text, ea.Font, new SolidBrush(ea.ForeColor),
bounds.Left + item.Icon.Width + spacer, bounds.Top);
base.OnDrawItem(ea);
}
catch(Exception Ex)
{
EventLog myEventLog = new EventLog("Application", ".", "ToolBar Error");
myEventLog.WriteEntry(Ex.ToString(), EventLogEntryType.Error);
}
}