이 예에서는 ComboBox 컨트롤에서 텍스트의 사용자 지정 그리기를 보여 줍니다. 항목이 특정 조건을 충족하는 경우 글꼴이 커지고 빨간색이 됩니다.
예시
Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem
Dim bFont As New Font("Arial", 8, FontStyle.Bold)
Dim lFont As New Font("Arial", 12, FontStyle.Bold)
Dim siText As SizeF
If ComboBox1.Items().Item(e.Index) = "Two" Then
siText = e.Graphics.MeasureString(ComboBox1.Items().Item(e.Index), _
lFont)
Else
siText = e.Graphics.MeasureString(ComboBox1.Items().Item(e.Index), bFont)
End If
e.ItemHeight = siText.Height
e.ItemWidth = siText.Width
End Sub
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
Dim g As Graphics = e.Graphics
Dim bFont As New Font("Arial", 8, FontStyle.Bold)
Dim lFont As New Font("Arial", 12, FontStyle.Bold)
If ComboBox1.Items().Item(e.Index) = "Two" Then
g.DrawString(ComboBox1.Items.Item(e.Index), lfont, Brushes.Red, _
e.Bounds.X, e.Bounds.Y)
Else
g.DrawString(ComboBox1.Items.Item(e.Index), bFont, Brushes.Black, e.Bounds.X, e.Bounds.Y)
End If
End Sub
코드 컴파일
이 예제에는 다음 사항이 필요합니다.
Windows 폼.
Items 속성에 세 개의 항목이 있는
ListBox1
이라는 ComboBox 컨트롤. 이 예에서는 세 항목의 이름이"One", Two", and Three"
으로 지정됩니다.ComboBox1
의 DrawMode 속성은 OwnerDrawVariable로 설정해야 합니다.System.Windows.Forms 및 System.Drawing 네임스페이스에 대한 참조
참고하십시오
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback