DrawItem 이벤트를 사용하여 목록 상자 항목의 앞색을 설정할 수 있습니다. 그러나 예를 들어 내 목록에 단일 빨간색 항목이 포함 된 경우 원하는 녹색 항목을 다음 항목에 추가하면 첫 번째 항목을 빨간색 항목으로 유지할 수 없습니다. 색상을 설정할 수 있다고 가정하지만 먼저 항목 색상을 가져와야합니다. 목록 상자 항목의 앞색을 얻는 방법은 무엇입니까? 감사합니다.
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
If e.Index = listBoxSize Then
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Green, e.Bounds.X, e.Bounds.Y)
Else
Using br = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, br, e.Bounds.X, e.Bounds.Y)
End Using
End If
e.DrawFocusRectangle()
End Sub
Dictionary(TKey, TValue)
클래스를 사용 하여 나열된 항목의 색상을 저장할 수 있습니다.
Dim colors As New Dictionary(Of Integer, Color)
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
Dim clr As Color = e.ForeColor
If e.Index = listBoxSize Then
clr = Colors.Green
Using br = New SolidBrush(clr)
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, br, e.Bounds.X, e.Bounds.Y)
End Using
colors.Add(e.Index, clr)
e.DrawFocusRectangle()
End Sub
이제 목록 색인으로 색상을 검색 할 수 있습니다.
Dim clr Color = colors(listBox1.SelectedIndex)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다