3

我有以下代码:

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 ,但是这会将列表项更改为不同的颜色,尝试调整代码并没有成功。谁能指出我正确的方向?

非常感谢。

4

1 回答 1

4

尝试创建一个字体变量:

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)
于 2013-08-27T16:58:50.737 回答