vb.net改变组合框中文本的颜色 [英] vb.net change color of text in combobox

查看:396
本文介绍了vb.net改变组合框中文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体中有一个组合框

I have a combobox in windows form

我试图改变组合框的亮点。

and I'm trying to change the highlight of combobox.

如果我输入我的组合框,然后按回车键,我的组合框的背景颜色就像这个图像一样

if I type in my combobox and then press enter it put the backcolor of my combobox like this image

如何更改默认值?

这里需要系统颜色

推荐答案

您需要将ComboBox DrawMode修改为OwnerDrawFixed并处理选择事件:

You need to modify you ComboBox DrawMode to OwnerDrawFixed and handle the selection events:

使用代码:

Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Location = New Point(100, 100)

        UpdateComboBoxDrawMode()

    End Sub

'Initialize the ComboBox so DrawMode is OwnerDrawFixed
    Private Sub UpdateComboBoxDrawMode()
        Me.ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
        Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

    End Sub

''' <summary>
    ''' This handler is used when ComboBox is OwnerDraw
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
        'No Selection
        If e.Index < 0 Then Exit Sub

        'Manipulating the Item rectangle
        Dim rect As Rectangle = e.Bounds
        If e.State And DrawItemState.Selected Then
            'Modify the Rectangle color...
            e.Graphics.FillRectangle(Brushes.LightGreen, rect) 'change the selected color you like
        Else
            'By default the item will be ligthPink background.... not pretty but for showing the example
            e.Graphics.FillRectangle(Brushes.LightPink, rect)
        End If
        Dim ItemText As String = ComboBox1.Items(e.Index)
'Draw the text in a black Brush...could be any color
        Dim ItemTextBrush As Brush = Brushes.Black
        e.Graphics.DrawString(ItemText, Me.ComboBox1.Font, ItemTextBrush, rect.X, rect.Y)
    End Sub


这篇关于vb.net改变组合框中文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆