Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1896 | Besucher online: 534
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

ImageComboBox


Autor: Tim Hartwig
Sprache: VB.NET
Bewertung: 7,4
(1 Bewertung)
Anzahl der Aufrufe: 6251
  
Kick it on dotnet-kicks.de  

Beschreibung:

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.

Abgelegt unter: ImageComboBox, ComboBox.



Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

Tim Hartwig schrieb am:  10.12.2006 16:23:29

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))


schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.