组合框MeasureItem事件没有发射

0

的问题

我试图开发一个自定义的组合框(其是在工具条)在其项目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
combobox vb.net winforms
2021-11-19 14:18:54
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

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

2021-11-19 16:08:05

谢谢,并再次对免费的建议! ^^
8oris

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................