【问题标题】:Changing the text color of the "hovered" item in a Combobox?更改组合框中“悬停”项目的文本颜色?
【发布时间】:2013-12-18 15:51:39
【问题描述】:

我有一个组合框控件,我将其设置为“下拉列表”样式(因为我不希望用户能够在组合框中键入文本)。唯一可能是我不喜欢“下拉列表”样式的外观,所以我尝试将其更改为 看起来 像普通组合框样式的位置。

所以我将绘制模式设置为“所有者绘制固定”。我的目标是为未悬停的组合框项目设置白色背景/黑色文本,为悬停的项目设置蓝色背景/白色文本(就像普通组合框一样)。

背景颜色正常工作,但文本颜色不正常(文本保持黑色,即使在悬停的项目上也是如此)。

这是我的代码...

Private Sub ComboBox1_DrawItem_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem

    Dim TextBrush As Brush

    If (e.State And DrawItemState.HotLight) = DrawItemState.HotLight Then
        TextBrush = Brushes.White
    Else
        TextBrush = Brushes.Black
    End If

    Dim index As Integer = If(e.Index >= 0, e.Index, 0)
    e.DrawBackground()
    e.Graphics.DrawString(ComboBox1.Items(index).ToString(), e.Font, TextBrush, e.Bounds, StringFormat.GenericDefault)
    e.DrawFocusRectangle()

End Sub

有什么想法吗?

我已经在 Google 上搜索了几个小时的解决方案,但到目前为止没有运气。

【问题讨论】:

  • 您是否设置了断点或打印调试语句以便判断 e.State 是否包含 HotLight 标志?
  • 刚刚测试过,它似乎永远不会触发“HotLight”状态,即使将项目悬停在上面也是如此。当一个项目悬停在上面时,e.State.ToString 值显示为“Selected, Focus, NoAccelerator, NoFocusRect”。不知道该怎么做。奇怪!
  • 那么我建议尝试其中一个值,例如Selected,而不是HotLight
  • 我也会尊重NoFocusRect 标志。
  • OK... 只是做了更多测试,以及悬停项目的 e.State 值(不是 'e.State.ToString' 值,只是 'e.State' 值)似乎是“785”。不知道这背后的原因是什么,但我现在已经在此基础上工作了。无论如何,谢谢,Dan-o :-)

标签: vb.net winforms visual-studio-2010 .net-2.0


【解决方案1】:

e.State 是一个标志集。它的值由ORing 标志值组成。所以你会想要使用If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then,而不是观察到的785 值。

true == 785 == (Selected OR Focus OR NoAccelerator OR NoFocusRect)

如果您删除NoFocusRect,整数值将会改变,但位掩码仍将包含Selected 的值。

【讨论】:

  • 它是“DrawItemState”,但这似乎工作得很好。谢谢!
【解决方案2】:

这是我如何更改组合框的选定背景颜色和选择文本前景色

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
    'create custom color and brush for selection color
    Dim myColor As Color
    Dim myBrush As Brush

    myColor = Color.FromArgb(231, 199, 100)

    myBrush = New SolidBrush(myColor)

    If e.Index < 0 Then Exit Sub

    Dim rect As Rectangle = e.Bounds
    Dim TextBrush As Brush
    Dim index As Integer = If(e.Index >= 0, e.Index, 0)

    If e.State And DrawItemState.Selected Then
        'selection background color
        e.Graphics.FillRectangle(myBrush, rect)
        'selection forecolor
        TextBrush = Brushes.Black
        e.Graphics.DrawString(ComboBox1.Items(index).ToString(), e.Font, TextBrush, e.Bounds, StringFormat.GenericDefault)
    Else
        e.Graphics.FillRectangle(Brushes.Black, rect)
        TextBrush = Brushes.White
        e.Graphics.DrawString(ComboBox1.Items(index).ToString(), e.Font, TextBrush, e.Bounds, StringFormat.GenericDefault)
    End If

End Sub

【讨论】:

    【解决方案3】:

    【讨论】:

    • 我会查看这些链接。谢谢 Neolisk!
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多