DrawItem event of combo box

O

orekinbck

Hi There

I have wired up an event handler to a combo boxes DrawItem event, and I
want to draw a string that is vertically centered in the combo box.

At the moment I am doing it like this:

int centeredY = e.Bounds.Y + (int)((e.Bounds.Height -
mf.GetHeight(e.Graphics))/2);

Is there an easier way ? FYI, here is a more complete listing:

private void cmbFonts_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == -1 || sender == null)
return;

ComboBox cboFont = (ComboBox)sender;
FontFamily ff = new FontFamily(cboFont.Items[e.Index].ToString());
Font mf = new Font(ff.Name,10,GetFontStyle(ff));

e.DrawBackground();
e.DrawFocusRectangle();

int centeredY = e.Bounds.Y + (int)((e.Bounds.Height -
mf.GetHeight(e.Graphics))/2);

e.Graphics.DrawString(ff.Name, mf,new
SolidBrush(e.ForeColor),e.Bounds.X,centeredY);

}

private FontStyle GetFontStyle(FontFamily ff)
{
FontStyle mfs = FontStyle.Regular;
if(!ff.IsStyleAvailable(mfs))
mfs = FontStyle.Italic;
if(!ff.IsStyleAvailable(mfs))
mfs = FontStyle.Bold;
return mfs;
}

Cheers
Bill
 

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