C# Listbox的行间距以及文本水平垂直居中

来源:互联网 发布:今晚非农数据 编辑:程序博客网 时间:2024/05/01 00:28

1、首先设置DrawMode属性为OwnerDrawVariable

2、 增加事件重画ListBox

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

3、属性中设置你想要的高度值 ItemHeight

原创粉丝点击