0

リストボックス コントロールのカスタム インスタンスをドロップダウン メニューとして使用するカスタム コンボボックスを作成しました。

リストボックスの選択ハイライトをカスタマイズするには、「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
4

1 に答える 1

0

以下は、関連するコードの残りの部分ですが、気にしないでください。 のAutosizeプロパティを_controlHostに設定して問題を修正しましたFalse。リスト内のすべての項目を表示するために (ドロップダウンの最大値を定義する必要なしに)に設定しTrueましたが、何らかの理由でDrawModeが に設定されていると動作が異なりOwnerDrawます。

    _listBox = New ListBox()
    _listBox.IntegralHeight = True
    _listBox.BorderStyle = BorderStyle.FixedSingle
    _listBox.SelectionMode = SelectionMode.One
    _listBox.BindingContext = New BindingContext()
    _dropDownWidth = Me.Width
    _listBox.Width = _dropDownWidth

    _controlHost = New ToolStripControlHost(_listBox)
    _controlHost.Padding = New Padding(0)
    _controlHost.Margin = New Padding(0)
    _controlHost.AutoSize = False 'Used to be variable property _dropDownAutoSize

    _popupControl = New ToolStripDropDown()
    _popupControl.Padding = New Padding(0)
    _popupControl.Margin = New Padding(0)
    _popupControl.AutoSize = True '
    _popupControl.DropShadowEnabled = False
    _popupControl.Items.Add(_controlHost)
于 2013-09-11T09:06:17.190 に答える