ヘッダー

VB.NET2002対応 VB.NET2003対応 VB2005対応

 

行ごとに背景色を変えてしましまにする

VB.NET2002対応 VB.NET2003対応 VB2005対応

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ListBox1.DrawMode = DrawMode.OwnerDrawFixed

End Sub

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

    If e.Index = -1 Then
       
Exit Sub
   
End If

    e.DrawBackground()

    '奇数項目の背景をスカイブルーにする。
   
If e.Index Mod 2 = 1 Then
       
'項目が選択されている場合は色を塗らない。
       
If CBool(e.State And DrawItemState.Selected) = False Then
           
e.Graphics.FillRectangle(Brushes.SkyBlue, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        End
If
   
End If

    Dim myBrush As Brush = New SolidBrush(ListBox1.ForeColor)

    e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

    e.DrawFocusRectangle()

End Sub

メモ:このサンプルではEnabled = Falseの場合、およびSelectionMode = Noneの場合の配慮がされていません。