Formatting Items in Combobox

G

gmahesh917

Hi All,

Please suggest me how we can do the formatting (e.g. BackColor
Changed) for perticular Item in combobox.?

I have done DrawMode = OwnerDrawFixed

and written following code in DrawItem Event :

private void comboBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
Font fntTab;
Brush bshBack;
Brush bshFore;
if (e.Index == 0)
{
fntTab = new Font(e.Font, FontStyle.Bold);
Color customColor = Color.FromArgb(211, 211, 211);
bshBack = new SolidBrush(customColor);
bshFore = new SolidBrush(Color.Black);
}
else
{
fntTab = e.Font;
Color customColor = Color.FromArgb(255,255,255);
bshBack = new SolidBrush(customColor);
bshFore = new SolidBrush(Color.Black);
}

string tabName = this.comboBox1.Items[e.index].ToString();
StringFormat sftTab = new StringFormat();
e.Graphics.FillRectangle(bshBack, e.Bounds);
Rectangle recTab = e.Bounds;

recTab = new Rectangle(recTab.X, recTab.Y + 2, recTab.Width + 8,
recTab.Height - 2);
e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
}

Everything works fine...

but
at line

string tabName = this.comboBox1.Items[e.index].ToString();

I get unhandled exception.
In this event e.Index is always -1

Please suggest solution...

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top