在winform中,listBox控件的行间距默认太紧凑,如果要调整,还是比较麻烦的。这时候就需要自己绘制行高 ,首先需要将DrawMode属性设置为OwnerDrawFixed。然后将ItemHeight设置为合适的高度。
最后在listbox的DrawItem事件中加入如下代码即可。
Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
e.Graphics.FillRectangle(New SolidBrush(e.BackColor), e.Bounds)
If e.Index >= 0 Then
Dim sStringFormat As StringFormat = New StringFormat()
sStringFormat.LineAlignment = StringAlignment.Center
e.Graphics.DrawString((CType(sender, ListBox)).Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, sStringFormat)
End If
e.DrawFocusRectangle()
End Sub
如果有多个Listbox那么在其余listbox的DrawItem事件中调用ListBox1的DrawItem即可。代码如下
Private Sub ListBox2_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox2.DrawItem
ListBox1_DrawItem(sender, e)
End Sub
效果如下图