调整ListBox控件的行间距及设置文本格式

  1. 首先要将该控件的DrawMode属性为OwnerDrawVariable
  2. 添加DrawItem重绘事件: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属性中设置你想要的高度值 ,大于字体高度即可,原先默认为字体高度。DrawMode属性若不是为OwnerDrawVariable,则ItemHeight无法修改。
posted @ 2015-07-15 09:03  北冥子  阅读(4458)  评论(0编辑  收藏  举报