ComboBox测量项事件未触发。

3

我正在尝试开发一个自定义组合框(位于工具栏中),其中条目包括 5 种字体样式,我希望按照它们的外观显示。

问题:我无法设置每个项目的大小。 MeasureItem 事件没有触发,我不知道原因。

所以它看起来像这样: enter image description here

离我的节奏还有一段距离! ^^

这是我的代码:

Public CorpusFontStyleTitre1 As Font = New Font(New FontFamily("Lato"), 18, FontStyle.Bold, 3)
Public CorpusFontStyleTitre2 As Font = New Font(New FontFamily("Lato"), 16, FontStyle.Underline Or FontStyle.Bold, 3)
Public CorpusFontStyleTitre3 As Font = New Font(New FontFamily("Lato"), 14, FontStyle.Underline Or FontStyle.Bold, 3)
Public CorpusFontStyleTitre4 As Font = New Font(New FontFamily("Lato"), 12, FontStyle.Underline, 3)
Public CorpusFontStyleCorpsdeTexte As Font = New Font(New FontFamily("Lato"), 12, FontStyle.Regular, 3)

Public Structure CorpusFontStyleItem
        Dim strTextStyleName As String
        Dim fontTextStyle As Font
        Public Overrides Function ToString() As String
            Return strTextStyleName
        End Function
End Structure

Private Sub frmCorpusManagement_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadcmbFontStyle()
        cmbFontStyle.ComboBox.DrawMode = DrawMode.OwnerDrawFixed
        AddHandler cmbFontStyle.ComboBox.DrawItem, AddressOf cmbFontStyle_DrawItem
        AddHandler cmbFontStyle.ComboBox.MeasureItem, AddressOf cmbFontStyle_MeasureItem
End Sub

Private Sub cmbFontStyle_MeasureItem(ByVal sender As Object, ByVal e As MeasureItemEventArgs)
        Select Case e.Index
            Case "1"
                e.ItemHeight = 50
            Case "2"
                e.ItemHeight = 40
            Case "3"
                e.ItemHeight = 30
            Case "4"
                e.ItemHeight = 20
            Case "5"
                e.ItemHeight = 10
        End Select

End Sub

Private Sub cmbFontStyle_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
        e.DrawBackground()
        Dim myItem As CorpusFontStyleItem = DirectCast(cmbFontStyle.Items(e.Index), CorpusFontStyleItem)
        e.Graphics.DrawString(myItem.strTextStyleName, myItem.fontTextStyle, New SolidBrush(Color.Black), e.Bounds)
        e.DrawFocusRectangle()
End Sub  

Private Sub LoadcmbFontStyle()

        Dim itemCorpusFontStyleTitre1 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 1", .fontTextStyle = CorpusFontStyleTitre1}
        Dim itemCorpusFontStyleTitre2 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 2", .fontTextStyle = CorpusFontStyleTitre2}
        Dim itemCorpusFontStyleTitre3 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 3", .fontTextStyle = CorpusFontStyleTitre3}
        Dim itemCorpusFontStyleTitre4 As New CorpusFontStyleItem With {.strTextStyleName = "Titre 4", .fontTextStyle = CorpusFontStyleTitre4}
        Dim itemCorpusFontStyleCorps As New CorpusFontStyleItem With {.strTextStyleName = "Corps de Texte", .fontTextStyle = CorpusFontStyleCorpsdeTexte}

        cmbFontStyle.Items.Add(itemCorpusFontStyleTitre1)
        cmbFontStyle.Items.Add(itemCorpusFontStyleTitre2)
        cmbFontStyle.Items.Add(itemCorpusFontStyleTitre3)
        cmbFontStyle.Items.Add(itemCorpusFontStyleTitre4)
        cmbFontStyle.Items.Add(itemCorpusFontStyleCorps)

End Sub

你是在添加项目之后才添加处理程序的。你尝试过颠倒这些操作的顺序吗? - Craig
你的意思是这样的吗:Private Sub frmCorpusManagement_Load(sender As Object, e As EventArgs) Handles MyBase.Load cmbFontStyle.ComboBox.DrawMode = DrawMode.OwnerDrawFixed AddHandler cmbFontStyle.ComboBox.DrawItem, AddressOf cmbFontStyle_DrawItem AddHandler cmbFontStyle.ComboBox.MeasureItem, AddressOf cmbFontStyle_MeasureItem LoadcmbFontStyle() End Sub - 8oris
看起来没错(不幸的是,在注释中无法使用多行代码)。 - Craig
抱歉,Craig,它并没有解决我的问题。 :/ 无论如何还是谢谢。 - 8oris
1个回答

3

你正在使用错误的绘制模式。请更改为以下模式:

cmbFontStyle.ComboBox.DrawMode = DrawMode.OwnerDrawVariable

此外,Select Case e.Index是一个整数而不是字符串。它是从零开始计数的,因此您没有测量第一项。将其更改为:

Select Case e.Index
  Case 0
    e.ItemHeight = 50
  Case 1
    e.ItemHeight = 40

你应该尝试实际测量字体的高度而不是猜测一个数字。


谢谢你再次提供免费的建议!^^ - 8oris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接