ComboBoxのドロップダウンリストのバックカラーの色を変えない方法

解決


MARI  2008-05-27 15:11:23  No: 144790  IP: 192.*.*.*

お世話になります。
VB2005でComboBoxにフォーカスが移動したらBackColorを変え、
フォーカスを失ったら元のカラーに戻すプログラムうを作っています。

ドロップダウンリストを選択すると、リストの背景までもカラーが変わって
しまいます、ComboBoxで入力できる領域のみ背景のみカラーを変える事は
できないでしょうか。

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        For i As Integer = 1 To 10
            ComboBox1.Items.Add(Format(i, "000") & "番目")
        Next i
    End Sub

    Private Sub ComboBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Enter
        ComboBox1.BackColor = Drawing.Color.LightSteelBlue
    End Sub

    Private Sub ComboBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave
        ComboBox1.ResetBackColor()
    End Sub

いろいろ試したのですがうまく動作しません何か良い方法があれば教えて
頂ければと思います、宜しくお願いします。

編集 削除
特攻隊長まるるう  2008-05-27 16:37:37  No: 144791  IP: 192.*.*.*

下記の2つのサンプルコードを組み合わせて修正してみるとか。

[DOBON.NET > .NET Tips > ComboBoxの項目を自分で描画する]
http://dobon.net/vb/dotnet/control/cbownerdraw.html

[ComboBox.MeasureItem イベント]
http://msdn.microsoft.com/ja-jp/library/system.windows.forms.combobox.measureitem.aspx

編集 削除
Abyss  2008-05-27 17:12:45  No: 144792  IP: 192.*.*.*

背景の色をWindowsのデフォルトを使うなら
こんな感じでしょうか。

Public Class Form1
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        ComboBox1.DrawMode = DrawMode.OwnerDrawFixed

        For i As Integer = 1 To 10
            ComboBox1.Items.Add(Format(i, "000") & "番目")
        Next i
    End Sub

    Private Sub ComboBox1_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.Enter
        ComboBox1.BackColor = Drawing.Color.LightSteelBlue
    End Sub

    Private Sub ComboBox1_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.Leave
        ComboBox1.ResetBackColor()
    End Sub

    Private Sub ComboBox1_DrawItem(ByVal o As Object, ByVal e As DrawItemEventArgs) Handles ComboBox1.DrawItem
        Dim grfx As Graphics = e.Graphics
        Dim rect As Rectangle = e.Bounds
        Dim flag As Boolean = (e.State And DrawItemState.Selected) <> 0
        If e.Index = -1 Then Exit Sub

        If flag Then
            grfx.FillRectangle(New SolidBrush(ComboBox1.BackColor), rect)
        Else
            grfx.FillRectangle(SystemBrushes.Window, rect)
        End If

        grfx.DrawString(ComboBox1.Items(e.Index).ToString, e.Font, _
                        New SolidBrush(e.ForeColor), rect)
        e.DrawFocusRectangle()
    End Sub
End Class

編集 削除
MARI  2008-05-28 08:03:04  No: 144793  IP: 192.*.*.*

お陰様で、ComboBoxを継承したコントロールを
作成し色々なプログラムで使いまわしすることが
できるようになりました、ありがとうございました。

編集 削除