您需要處理DRAWITEM事件和DrawMode = OwnerDrawFixed財產。
Dim buttonPressed As Boolean
Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
If ListBox1.SelectedIndices.Contains(e.Index) And buttonPressed Then
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Green, e.Bounds.X, e.Bounds.Y)
Else
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)
End If
If e.Index = ListBox1.Items.Count - 1 Then
buttonPressed = False
End If
e.DrawFocusRectangle()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
buttonPressed = True
ListBox1.Refresh()
End Sub
繪製字符串後繪製焦點矩形*。建議使用TextRenderer.DrawText。 – 2011-03-09 03:19:11
使用這個確切的代碼,我得到這個錯誤:InvalidArgument ='-1'的值是'索引'無效。 參數名稱:索引 – Quark 2011-03-09 23:02:45
@Quark:本代碼僅供參考,請妥善保管控制語句以避免此類問題。我沒有測試過這個代碼。 – adatapost 2011-03-10 01:09:13