c# listBox 设置itemHeight 行高



首先设置listbox的DrawMode为OwnerDrawVariable;然后增加事件关联

void listBox_num1_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
e.DrawBackground();
e.DrawFocusRectangle();
////让文字位于Item的中间
//float difH = (e.Bounds.Height – e.Font.Height) / 2;
//RectangleF rf = new RectangleF(e.Bounds.X, e.Bounds.Y + difH, e.Bounds.Width, e.Font.Height);
//e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), rf);
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Black), e.Bounds);
}
catch
{ }
//throw new NotImplementedException();
}

 

void listBox_num1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 20;

//throw new NotImplementedException();
}