C#修改下拉框选项的高度

时间:2022-04-24 08:42:32

重写ListBox.DrawItem事件处理,别忘记将ListBox.DrawMode 设置为OwnerDrawVariable,ListBox.ItemHeight值改大一点,字体也适当放大一号。

         private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground(); e.DrawFocusRectangle(); StringFormat strFmt = new System.Drawing.StringFormat(); strFmt.Alignment = StringAlignment.Near; //文本水平居中 strFmt.LineAlignment = StringAlignment.Center; //文本垂直居中 e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, strFmt);
}