vb.net - ComboBox MeasureItem 事件未触发

标签 vb.net winforms combobox

我正在尝试开发一个自定义组合框(在工具条中),其中的项目是 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

最佳答案

您使用了错误的绘图模式。将其更改为:

cmbFontStyle.ComboBox.DrawMode = DrawMode.OwnerDrawVariable

此外,Select Case e.Index 是一个整数,而不是一个字符串。它是从零开始的,所以你不是在衡量第一项。将其更改为:

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

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

关于vb.net - ComboBox MeasureItem 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70036266/

相关文章:

vb.net - 修改 Word 文档中嵌入的 Excel 对象

c# - .NET 的整数不就是简单地基于补码系统吗?

c++ - winAPI 中的组合框样式编辑控件

java - 比较 2 个多行字符串

.net - Mono 的 VB.Net 支持是否已准备好用于生产站点?

c# - 如何使用服务器模式在 GridView 中获取异常详细信息?

c# - 将异步方法的执行推送到线程池线程

sql - 是否可以从 Access 表单中制作一个 EXE?

mysql - 如何从数据库获取自动填充组合框的 ID?

c# - 如何设置组合框项目可见性?