VB.NET设置listbox控件行高的方法(Winform)

在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

效果如下图

未经允许不得转载:桔子雨工作室 » VB.NET设置listbox控件行高的方法(Winform)
分享到: 生成海报
一个数字化服务提供商

承接外贸建站,软件APP开发

部分产品联系我们
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码