如何在列表框中加粗项目?

时间:2013-08-27 16:56:04

标签: vb.net visual-studio

我有以下代码:

Private Sub ListBox1_DrawItem(ByVal sender As Object, _
 ByVal e As System.Windows.Forms.DrawItemEventArgs) _
 Handles ListBox1.DrawItem

    ' Draw the background of the ListBox control for each item.
    e.DrawBackground()

    ' Define the default color of the brush as black. 
    Dim myFontStyle As FontStyle = FontStyle.Bold
    ' Determine the color of the brush to draw each item based on    
    ' the index of the item to draw. 
    Select Case e.Index
        Case 0
            'Set Font to Bold
        Case 1
            'Set Font to Normal

    End Select

    ' Draw the current item text based on the current  
    ' Font and the custom brush settings.
    e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
    e.Font, e.Brush, e.Bounds, StringFormat.GenericDefault)
End Sub

我正在尝试这样做,以便我可以在ListBox中加粗某些项目,但是我遇到了很多麻烦。我提到MSDN但是这会将列表项更改为不同的颜色,尝试调整代码使我没有成功。任何人都可以指出我正确的方向吗?

非常感谢。

1 个答案:

答案 0 :(得分:3)

尝试创建字体变量:

Dim useFont as Font = e.Font
Select Case e.Index
    Case 0
        'Set Font to Bold
      useFont = New Font(e.Font, FontStyle.Bold)
End Select

' Draw the current item text based on the current  
' Font and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
useFont, e.Brush, e.Bounds, StringFormat.GenericDefault)