Visual Basic 中学校 > Visual Basic サンプル集 > Visual Basic サンプル集目次 > Windows.Forms.ListBox >
特定の項目の文字の色を変える
以下の例は3番目の項目を赤くする。
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 e.DrawBackground() Dim myBrush As Brush If
e.Index = 2 Then 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の場合の配慮がされていません。