リストボックス コントロールのカスタム インスタンスをドロップダウン メニューとして使用するカスタム コンボボックスを作成しました。
リストボックスの選択ハイライトをカスタマイズするには、「DrawMode」プロパティを「OwnerDrawFixed」に変更し、次のコードを追加する必要がありました。
Private Sub _listBox_DrawItem(sender As Object, e As DrawItemEventArgs)
If e.Index >= 0 Then
e.DrawBackground()
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
Using br = New LinearGradientBrush(e.Bounds, ColorSelectionListbox, ColorSelectionListbox, 0)
e.Graphics.FillRectangle(br, e.Bounds)
End Using
End If
Using b As New SolidBrush(ColorTextListbox)
e.Graphics.DrawString(_listBox.GetItemText(_listBox.Items(e.Index)), e.Font, b, e.Bounds)
End Using
e.DrawFocusRectangle()
RaiseEvent DrawItem(Me, e)
End If
End Sub
しかし、これでは、設定した幅は無視され、15ピクセル程度の固定幅になります。
オーナー描画コントロールの幅を設定するにはどうすればよいですか? 現在、私はそれをプロパティとして持っています:
Public Property DropDownWidth() As Integer
Get
Return _dropDownWidth
End Get
Set(value As Integer)
_dropDownWidth = value
_listBox.Width = value
Invalidate(True)
End Set
End Property