您需要处理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