Nasıl yapılır: ComboBox Denetiminde Değişken Boyutlu Metin Oluşturma
Bu örnek, bir denetimdeki metnin özel çizimini gösterir ComboBox . Bir öğe belirli bir ölçütü karşıladığında, daha büyük bir yazı tipiyle çizilir ve kırmızı renkte olur.
Örnek
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
Kod Derleniyor
Bu örnek şunları gerektirir:
bir Windows formu.
ComboBox
ListBox1Özelliğindeki üç öğeyle adlı bir denetim Items . Bu örnekte, üç öğe adlandırılır"One", Two", and Three". DrawModeÖzelliğiComboBox1olarak ayarlanmalıdır OwnerDrawVariable .System.Windows.FormsVe System.Drawing ad alanlarına başvurular.