RunningCSharp

MS系開発者による、雑多な記事。記事は所属企業とは関係のない、個人の見解です。

VB(.net) Windows10 WinForms ComboBox(DrowDownStyle = DropDownList , FlatStyle=Standard)の背景色を白くする

掲題の条件下では、コンボボックスの背景色は灰色となります。

f:id:ys-soniclab:20190407194806p:plain

f:id:ys-soniclab:20190407194842p:plain

下記手順で、コンボボックスの背景色を白くできます。

①DrawModeを、「OwnerDrawFixed」に変更

f:id:ys-soniclab:20190407194927p:plain

②下記例のように、ComboBoxのDrawItemイベントへ関連付けたメソッドに処理を追加する

    Private Sub combobox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
        If e.Index >= 0 Then
            e.DrawBackground()
            e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault)
            e.DrawFocusRectangle()
        End If
    End Sub

上記対応を行うと、コンボボックスが下記のようになる。

f:id:ys-soniclab:20190407194948p:plain

f:id:ys-soniclab:20190407195010p:plain