如何:在 ComboBox 控件中创建大小可变的文本
此示例演示控件 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 窗体。
一个名为
ListBox1
的 ComboBox 控件,在 Items 属性中有三个项。 在本例中,这三个项分别名为"One", Two", and Three"
。ComboBox1
的 DrawMode 属性必须设置为 OwnerDrawVariable。对 System.Windows.Forms 和 System.Drawing 命名空间的引用。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈