vb.net 在winform中调整listBox行间距

  在为 winform中,listBox的行间距默认太紧凑,如果要调整,还是比较麻烦的。笔者查询了多种方法,现总结如下。需要两个事件,一个是DrawItem ,另一个是MeasureItem。还需要把listBox的drawMode属性设置为OwerDrawFixed, 即手动模式。
  

‘重新绘制listBox的条目
   Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
        e.DrawBackground()
        e.DrawFocusRectangle()
        Dim difH = (e.Bounds.Height - e.Font.Height) / 2
        '指定绘制文本的位置
        Dim rf As RectangleF = New RectangleF(e.Bounds.X, e.Bounds.Y + difH, e.Bounds.Width, e.Font.Height)
        '绘制指定的字符串
        e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), e.Font, New SolidBrush(Color.Black), rf)
        'e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds)
    End Sub
 ’衡量listBox条目的高度
   Private Sub ListBox1_MeasureItem(sender As Object, e As MeasureItemEventArgs) Handles ListBox1.MeasureItem
        e.ItemHeight = 50
    End Sub
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值