I want to do some visual changes on listbox items so i set DrawMode to "OwnerDrawFixed" i want the text to be the middle of the item vertivally it was easy by doing this:
private void listTypes_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.Graphics.DrawString(listTypes.Items[e.Index].ToString(),
e.Font, Brushes.Black, e.Bounds.Left, e.Bounds.Top + e.Bounds.Height/4
, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
but to center the text horizentally i need to know the text width how to get it or is there a better way to do this
You can try with code
void listTypes_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox list = (ListBox)sender;
if (e.Index > -1)
{
object item = list.Items[e.Index];
e.DrawBackground();
e.DrawFocusRectangle();
Brush brush = new SolidBrush(e.ForeColor);
SizeF size = e.Graphics.MeasureString(item.ToString(), e.Font);
e.Graphics.DrawString(item.ToString(), e.Font, brush, e.Bounds.Left + (e.Bounds.Width / 2 - size.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2 - size.Height / 2));
}
}