Feedback

VB - ImageComboBox

Veröffentlicht von am 12/10/2006
(1 Bewertungen)
Im Framework gibt es leider keine ImageComboBox, also muss man sich selber eine basteln bzw. die vorhandene ComboBox entsprechend erweitern. Hinweis: Man kann nur Einträge per Code hinzufügen und nicht per Designer weil man im Designer kein ImageKey bzw. ImageIndex festlegen kann.
Imports System.Windows.Forms
Imports System.Drawing

''' <summary>
''' Da es im Framework keine ImageComboBox gibt hatte ich einfach selber
''' eine geschrieben bzw. die vorhandene ComboBox erweitert.
''' Achtung: Man kann nur Items per Code hinzufügen also nicht im Designer,
''' da man im Designer kein ImageKey festlegen kann.
''' </summary>
''' <remarks>Tim Hartwig</remarks>
Public Class ImageComboBox
    Inherits ComboBox

    Private mImageList As ImageList = Nothing

    Sub New()
        Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    End Sub

    Public Property ImageList() As ImageList
        Get
            Return mImageList
        End Get
        Set(ByVal value As ImageList)
            mImageList = value
        End Set
    End Property

    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
        MyBase.OnDrawItem(e)

        e.DrawBackground()
        e.DrawFocusRectangle()

        If e.Index >= 0 Then

            If TypeOf Me.Items(e.Index) Is ImageComboItem Then
                If mImageList IsNot Nothing Then
                    Dim CurrItem As ImageComboItem = DirectCast(Me.Items(e.Index), ImageComboItem)

                    If CurrItem.ImageIndex <> -1 Then
                        Me.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, CurrItem.ImageIndex)
                        e.Graphics.DrawString(CurrItem.Text, CurrItem.Font, _
                        New SolidBrush(CurrItem.ForeColor), _
                        e.Bounds.Left + mImageList.ImageSize.Width, e.Bounds.Top)
                    Else
                        e.Graphics.DrawString(CurrItem.Text, CurrItem.Font, _
                        New SolidBrush(CurrItem.ForeColor), _
                        e.Bounds.Left + mImageList.ImageSize.Width, e.Bounds.Top)
                    End If
                End If
            Else
                e.Graphics.DrawString(Me.Items(e.Index).ToString(), e.Font, _
                New SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top)
            End If
        End If
    End Sub
End Class



Public Class ImageComboItem
    Private mForeColor As Color = Color.Black
    Private mImageIndex As Integer = -1
    Private mTag As Object = Nothing
    Private mText As String = ""
    Private mFont As Font

    Sub New(ByVal Text As String, ByVal Font As Font, ByVal ForeColor As Color)
        mText = Text
        mFont = Font
        mForeColor = ForeColor
    End Sub

    Sub New(ByVal Text As String, ByVal Font As Font, _
    ByVal ForeColor As Color, ByVal ImageIndex As Integer)
        mText = Text
        mFont = Font
        mForeColor = ForeColor
        mImageIndex = ImageIndex
    End Sub

    Public Property ForeColor()
        Get
            Return mForeColor
        End Get
        Set(ByVal value)
            mForeColor = value
        End Set
    End Property

    Public Property ImageIndex() As Integer
        Get
            Return mImageIndex
        End Get
        Set(ByVal value As Integer)
            mImageIndex = value
        End Set
    End Property

    Public Property Tag() As Object
        Get
            Return mTag
        End Get
        Set(ByVal value As Object)
            mTag = value
        End Set
    End Property

    Public Property Text() As String
        Get
            Return mText
        End Get
        Set(ByVal value As String)
            mText = value
        End Set
    End Property

    Public Property Font() As Font
        Get
            Return mFont
        End Get
        Set(ByVal value As Font)
            mFont = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return mText
    End Function
End Class
Abgelegt unter ImageComboBox, ComboBox.

1 Kommentare zum Snippet

Tim Hartwig schrieb am 12/10/2006:
Hier ein Beispiel wie man ein Item hinzufügt:

Me.ImageComboBox1.ImageList = Me.ImageList1
Me.ImageComboBox1.Items.Add(New ImageComboItem("Hallo", New Font("Verdana", 8),
Color.Black, 0))
 

Logge dich ein, um hier zu kommentieren!