Visual Basic .NET - Change color of one listBox item

后端 未结 1 719
深忆病人
深忆病人 2020-12-22 12:15

I\'m making a small program in VB.NET. I have a listBox and a button. I\'d like to be able to press the button and have the selected listBox item change it\'s foreColor to g

相关标签:
1条回答
  • 2020-12-22 12:43

    You need to handle DrawItem event and DrawMode=OwnerDrawFixed property.

    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
    
    0 讨论(0)
提交回复
热议问题