Witam mam obiekt ComboBoxEx poniżej jego kod, który dodaje ikony do tradycyjnego comboBox'a.
Jedna z formatek zawierająca ComboBoxEx posiada przeciążenie metody OnDrawItem. Przy czym jeżeli dodaje dymki to nie dodawane są ikony. W jaki sposób mogę połączyć obie metody?
public class ComboBoxEx : ComboBox
{
private ImageList imageList;
public ImageList ImageList
{
get { return imageList; }
set { imageList = value; }
}
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
public ComboBoxEx()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.FormattingEnabled = true;
this.DropDownStyle = ComboBoxStyle.DropDownList;
}
protected override void OnDrawItem(DrawItemEventArgs ea)
{
ea.DrawBackground();
ea.DrawFocusRectangle();
ComboBoxExItem item;
Size imageSize = imageList.ImageSize;
Rectangle bounds = ea.Bounds;
try
{
item = (ComboBoxExItem)Items[ea.Index];
if (item.ImageIndex != -1)
{
imageList.Draw(ea.Graphics, bounds.Left, bounds.Top, item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left+imageSize.Width, bounds.Top);
}
else
{
ea.Graphics.DrawString(item.Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
catch
{
if (ea.Index != -1)
{
ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
else
{
ea.Graphics.DrawString(Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
base.OnDrawItem(ea);
}
}
private void comboBoxEx_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
{
return;
}
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
toolTip1.Show(comboBoxEx.Items[e.Index].ToString(),comboBoxEx, e.Bounds.Right, e.Bounds.Bottom);
}
e.DrawBackground();
e.Graphics.DrawString(comboBoxEx.Items[e.Index].ToString(),e.Font,
Brushes.Black, new Point(e.Bounds.X, e.Bounds.Y));
}
private void comboBoxEx_DropDownClosed(object sender, EventArgs e)
{
toolTip1.Hide(comboBoxEx);
}