調整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沒法修改。
相關文章
相關標籤/搜索