ListControl Item adden, nur wie?

  • Allgemein

Es gibt 22 Antworten in diesem Thema. Der letzte Beitrag () ist von ~blaze~.

    ListControl Item adden, nur wie?

    Hallo liebe Community,
    ich habe dieses Control im i-net gefunden:
    Control

    VB.NET-Quellcode

    1. Public Class ListControl : Inherits ListBox
    2. Public Sub New()
    3. Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    4. Me.ItemHeight = 100
    5. End Sub
    6. Dim _FontSubText As Font = Me.Font
    7. Public Property FontSubText() As Font
    8. Get
    9. Return _FontSubText
    10. End Get
    11. Set(ByVal value As Font)
    12. _FontSubText = value
    13. End Set
    14. End Property
    15. Dim _FontTopText As Font = Me.Font
    16. Public Property FontTopText() As Font
    17. Get
    18. Return _FontTopText
    19. End Get
    20. Set(ByVal value As Font)
    21. _FontTopText = value
    22. End Set
    23. End Property
    24. Dim _SelectedBackColor As Color = Color.CornflowerBlue
    25. Public Property SelectedBackColor() As Color
    26. Get
    27. Return _SelectedBackColor
    28. End Get
    29. Set(ByVal value As Color)
    30. _SelectedBackColor = value
    31. End Set
    32. End Property
    33. Dim _BorderColor As Color = Color.Gray
    34. Public Property BorderColor() As Color
    35. Get
    36. Return _BorderColor
    37. End Get
    38. Set(ByVal value As Color)
    39. _BorderColor = value
    40. End Set
    41. End Property
    42. Dim _SelectedBorderColor As Color = Color.DarkGray
    43. Public Property SelectedBorderColor() As Color
    44. Get
    45. Return _SelectedBorderColor
    46. End Get
    47. Set(ByVal value As Color)
    48. _SelectedBorderColor = value
    49. End Set
    50. End Property
    51. Dim _TopTextForeColor As Color = Color.Black
    52. Public Property TopTextForeColor() As Color
    53. Get
    54. Return _TopTextForeColor
    55. End Get
    56. Set(ByVal value As Color)
    57. _TopTextForeColor = value
    58. End Set
    59. End Property
    60. Dim _SubTextForeColor As Color = Color.Gray
    61. Public Property SubTextForeColor() As Color
    62. Get
    63. Return _SubTextForeColor
    64. End Get
    65. Set(ByVal value As Color)
    66. _SubTextForeColor = value
    67. End Set
    68. End Property
    69. Private Sub ListControl_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
    70. If e.Index > -1 AndAlso e.Index < (Items.Count) Then
    71. Dim tmp As New ListControlItem(New Bitmap(1, 1), "a", "b", "c")
    72. If Me.Items(e.Index).GetType Is tmp.GetType Then
    73. Dim r As Rectangle = New Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 3, e.Bounds.Height - 2)
    74. tmp = CType(Me.Items(e.Index), ListControlItem)
    75. With e.Graphics
    76. If e.Index = Me.SelectedIndex Then
    77. .FillRectangle(New SolidBrush(SelectedBackColor), r)
    78. .DrawRectangle(New Pen(New SolidBrush(SelectedBorderColor)), r)
    79. Else
    80. .FillRectangle(New SolidBrush(Color.Transparent), r)
    81. .DrawRectangle(New Pen(New SolidBrush(BorderColor)), r)
    82. End If
    83. If tmp.Image IsNot Nothing Then .DrawImage(tmp.Image, r.X + 5, r.Y + 5, 45, 45) Else .DrawIcon(Drawing.SystemIcons.Error, r.X + 5, r.Y + 5)
    84. .DrawString(tmp.TopText, Me.FontTopText, New SolidBrush(TopTextForeColor), New Rectangle(r.X + 60, r.Y + 5, Me.Width - (r.X + 60), CInt(.MeasureString(tmp.TopText, Me.FontTopText).Height)))
    85. .DrawString(tmp.MainContent, Me.Font, New SolidBrush(Me.ForeColor), New Rectangle(r.X + 60, r.Y + 25, Me.Width - (r.X + 60), CInt(60 - .MeasureString(tmp.TopText, Me.FontTopText).Height - .MeasureString(tmp.SubText, Me.FontSubText).Height)))
    86. .DrawString(tmp.SubText, Me.FontSubText, New SolidBrush(SubTextForeColor), New Rectangle(r.X + 60, r.Y + Me.ItemHeight - 20, Me.Width - (r.X + 60), CInt(.MeasureString(tmp.SubText, Me.FontSubText).Height)))
    87. End With
    88. End If
    89. End If
    90. End Sub
    91. Private Sub ListControl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SelectedIndexChanged
    92. Me.Invalidate()
    93. End Sub
    94. End Class
    95. Public Class ListControlItem
    96. Public Sub New(ByVal _IMG As Bitmap, ByVal _TopText As String, ByVal _mainCont As String, ByVal _SubText As String)
    97. If Image IsNot Nothing Then Image = _IMG
    98. TopText = _TopText
    99. SubText = _SubText
    100. MainContent = _mainCont
    101. End Sub
    102. Public Image As Bitmap = New Bitmap(1, 1)
    103. Public TopText As String = ""
    104. Public MainContent As String = ""
    105. Public SubText As String = ""
    106. End Class

    Jedoch weiß ich nicht wie ich ein Item dazu adde.
    Bitte um Hilfe.
    Danke im Vorraus.
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Gather“ ()

    Hi
    vmtl. wie bei einer ListBox. Aber guter Rat: Der Code ist schlecht, also schau ihn dir nur durch und such' dir das Prinzip raus, in dem er vorgeht (z.B. das DrawItem-Ereignis zum Zeichnen der ListBox-Items). Danach schau' mal, wie du ihn besser machst.

    Edit: Grad' hab' ich ihn nur überflogen, aber nach genauerer Betrachtung muss ich die vorherige Aussage, wonach der Code schlecht ist, zurückziehen: Der Code ist grottig.

    Gruß
    ~blaze~
    Könntest du mir bitte trotzdem sagen, wie ich ein item hinzufüge.
    Aber ein item mit img, toptext, maintext subtext.
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Könntest du mir eine Erklärung dazu geben, damit ich das ganze besser verstehe?
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Ja und ich liefer dir auch einen hoffentlich besseren Code...
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class ListControl
    2. Inherits ListBox
    3. 'evtl. noch mal anpassen
    4. Private fntSubTextFont As Font = SystemFonts.DefaultFont
    5. Private fntHeaderFont As Font = SystemFonts.DefaultFont
    6. Private clrSelectedBackColor As Color = Color.CornflowerBlue
    7. Private clrBorderColor As Color = Color.Gray
    8. Private clrSelectedBorderColor As Color = Color.DarkGray
    9. Private clrHeaderColor As Color = Color.Black
    10. Private clrSubTextColor As Color = Color.Gray
    11. Private bDrawDefaultItems As Boolean = True
    12. 'Category etc. nat. noch anfuegen, bin ich aber zu faul zu
    13. <System.ComponentModel.DefaultValue(True)> _
    14. Public Property DrawDefaultItems As Boolean
    15. Get
    16. Return bDrawDefaultItems
    17. End Get
    18. Set(ByVal value As Boolean)
    19. bDrawDefaultItems = value
    20. End Set
    21. End Property
    22. Public Property SubTextFont As Font
    23. Get
    24. Return fntSubTextFont
    25. End Get
    26. Set(ByVal value As Font)
    27. fntSubTextFont = value
    28. Invalidate()
    29. End Set
    30. End Property
    31. Public Property HeaderFont As Font
    32. Get
    33. Return fntHeaderFont
    34. End Get
    35. Set(ByVal value As Font)
    36. fntHeaderFont = value
    37. Invalidate()
    38. End Set
    39. End Property
    40. Public Property SubTextColor As Color
    41. Get
    42. Return clrSubTextColor
    43. End Get
    44. Set(ByVal value As Color)
    45. clrSubTextColor = value
    46. Invalidate()
    47. End Set
    48. End Property
    49. Public Property HeaderColor As Color
    50. Get
    51. Return clrHeaderColor
    52. End Get
    53. Set(ByVal value As Color)
    54. clrHeaderColor = value
    55. Invalidate()
    56. End Set
    57. End Property
    58. Public Property SelectedBorderColor As Color
    59. Get
    60. Return clrSelectedBorderColor
    61. End Get
    62. Set(ByVal value As Color)
    63. clrSelectedBorderColor = value
    64. Invalidate()
    65. End Set
    66. End Property
    67. Public Property SelectedBackColor As Color
    68. Get
    69. Return clrSelectedBackColor
    70. End Get
    71. Set(ByVal value As Color)
    72. clrSelectedBackColor = value
    73. Invalidate()
    74. End Set
    75. End Property
    76. Public Property BorderColor As Color
    77. Get
    78. Return clrBorderColor
    79. End Get
    80. Set(ByVal value As Color)
    81. clrBorderColor = value
    82. Invalidate()
    83. End Set
    84. End Property
    85. 'neusetzen verhindern
    86. <System.ComponentModel.Browsable(False), System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
    87. Public Overrides Property DrawMode As System.Windows.Forms.DrawMode
    88. Get
    89. Return MyBase.DrawMode
    90. End Get
    91. Set(ByVal value As System.Windows.Forms.DrawMode)
    92. MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    93. End Set
    94. End Property
    95. Public Sub New()
    96. InitializeComponent()
    97. DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    98. ItemHeight = 100
    99. End Sub
    100. Private Sub ListControl_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
    101. Dim item As Object = Items(e.Index)
    102. If TypeOf item Is ListControlItem Then ' ist das Item ein ListControlItem (im anderen Code auf eine... interessante Art geloest)
    103. Dim current As ListControlItem = DirectCast(item, ListControlItem) ' Casten zum ListControlItem
    104. 'Rechteck, in dem gezeichnet werden soll (s. o.)
    105. Dim r As Rectangle = New Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 3, e.Bounds.Height - 2)
    106. With e.Graphics
    107. Dim headerSize As Size
    108. Dim subtextSize As Size
    109. Dim elementBounds As Rectangle
    110. If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then 'ist das Item angewaehlt
    111. .FillRectangle(New SolidBrush(SelectedBackColor), r) 'Hintergrund zeichnen
    112. .DrawRectangle(New Pen(New SolidBrush(SelectedBorderColor)), r) 'Rahmen zeichnen
    113. Else
    114. 'wozu was mit einem durchsichtigen Hintergrund fuellen?!! Wenn dann noch BackColor oder so
    115. '.FillRectangle(New SolidBrush(Color.Transparent), r) 'transparentes Rechteck drueberlegen (lol)
    116. .DrawRectangle(New Pen(New SolidBrush(BorderColor)), r) 'Rahmen zeichnen
    117. End If
    118. If current.Image IsNot Nothing Then 'Wenn ein Bild vorhanden wird (Kotz-Faktor nr 2)
    119. .DrawImage(current.Image, r.X + 5, r.Y + 5, 45, 45)
    120. Else
    121. 'einfach gar nix zeichnen?!!
    122. '.DrawIcon(Drawing.SystemIcons.Error, r.X + 5, r.Y + 5) 'oder halt ein Fehlericon zeichnen
    123. End If
    124. headerSize = Size.Ceiling(.MeasureString(current.Header, HeaderFont)) 'Groesse des Headers ermitteln
    125. elementBounds = New Rectangle(r.X + 60, r.Y + 5, ClientSize.Width - r.X - 60, headerSize.Height) 'Bereich des Headers
    126. 'eigentlich per TextRenderer-Klasse (siehe weiter unten)?
    127. .DrawString(current.Header, HeaderFont, New SolidBrush(HeaderColor), elementBounds) 'Header zeichnen (Top-Text oben)
    128. subtextSize = Size.Ceiling(.MeasureString(current.SubText, SubTextFont)) 'Groesse des Subtexts ermitteln
    129. elementBounds = New Rectangle(r.X + 60, r.Y + 25, ClientSize.Width - r.X - 60, 60 - subtextSize.Height - headerSize.Height)
    130. .DrawString(current.Content, Font, New SolidBrush(Me.ForeColor), elementBounds) 'Inhalt zeichnen (main content oben)
    131. elementBounds = New Rectangle(r.X + 60, r.Y + Me.ItemHeight - 20, Me.Width - r.X - 60, subtextSize.Height)
    132. .DrawString(current.SubText, SubTextFont, New SolidBrush(SubTextColor), elementBounds) 'Subtext zeichnen
    133. 'Falls ausgewaehlt und gewuenscht, ein Fokusrechteck zeichnen
    134. If (e.State And (DrawItemState.Focus Or DrawItemState.NoFocusRect)) = DrawItemState.Focus Then
    135. ControlPaint.DrawFocusRectangle(e.Graphics, r)
    136. End If
    137. End With
    138. ElseIf DrawDefaultItems Then 'von mir eingefuehrte Eigenschaft, die es dem User ermoeglicht, nicht speziell behandelte Items ohne eigene Zeichenprozedur zu zeichnen
    139. e.DrawBackground()
    140. TextRenderer.DrawText(e.Graphics, GetItemText(item), Font, e.Bounds, ForeColor) 'hier ein BSP fuer den TextRenderer
    141. If (e.State And (DrawItemState.Focus Or DrawItemState.NoFocusRect)) = DrawItemState.Focus Then
    142. e.DrawFocusRectangle()
    143. End If
    144. End If
    145. End Sub
    146. Public NotInheritable Class ListControlItem
    147. Private ReadOnly imgImage As Image
    148. Private ReadOnly strHeader As String
    149. Private ReadOnly strContent As String
    150. Private ReadOnly strSubText As String
    151. Public ReadOnly Property Image As Image
    152. Get
    153. Return imgImage
    154. End Get
    155. End Property
    156. Public ReadOnly Property Header As String
    157. Get
    158. Return strHeader
    159. End Get
    160. End Property
    161. Public ReadOnly Property Content As String
    162. Get
    163. Return strContent
    164. End Get
    165. End Property
    166. Public ReadOnly Property SubText As String
    167. Get
    168. Return strSubText
    169. End Get
    170. End Property
    171. Public Sub New(ByVal image As Image, ByVal header As String, ByVal content As String, ByVal subText As String)
    172. imgImage = image
    173. strHeader = header
    174. strContent = content
    175. strSubText = subText
    176. End Sub
    177. End Class
    178. End Class


    Items fügst du per Code hinzu:

    VB.NET-Quellcode

    1. ListControlInstance.Items.Add(new ListControl.ListControlItem(...))

    bzw. bei dem Foltercode oben:

    VB.NET-Quellcode

    1. ListControlInstance.Items.Add(new ListControlItem(...))

    Die Klasse bei meinem Code kannst du natürlich noch herausziehen.
    Die Basis für den Code ist das DrawItem-Ereignis, das von der ListBox angeboten wird. Das wird eben dazu verwendet, die Items selber zu zeichnen, wenn die DrawMode-Eigenschaft angepasst wird. Über die übergebenen EventArgs werden die Eigenschaften des zu zeichnenden Elements angepasst.

    Gruß
    ~blaze~
    Noch was blaze wieso ist

    VB.NET-Quellcode

    1. InitializeComponent()

    Als undefiniert gekenntzeichnet?
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Mit

    Gather schrieb:

    VB.NET-Quellcode

    1. InitializeComponent()
    werden designererzeugte GUI-Elemente initialisiert.
    Wenn Du ein designereerzeugtes Control / eine Form hast, ist das automatisch dabei, diese Prozedur ist in der Datei DEIN_CONTROL.Designer.vb.
    Wenn Du eine Klasse generierst:

    VB.NET-Quellcode

    1. Public Class MyControl
    2. Inherits UserControl
    3. ' ...
    4. End Class
    ist das nicht dabei, da musst Du das von Hand einfügen.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    ja, jedoch wird sie als fehler gekenntzeichnet.
    Heißt dass ich soll sie entfernen?
    Fehler:

    Quellcode

    1. Fehler 1 "InitializeComponent" wurde nicht deklariert. Auf das Objekt kann aufgrund seiner Schutzstufe möglicherweise nicht zugegriffen werden.
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    ich verstehe nicht ganz wie das mein problem lösen soll...
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Tu es. Wenn du sie nicht hast und nicht brauchst, nimm' sie weg. Im Designer erzeugte Controls, Komponenten usw. haben diese Methode. Ich hab' sie daher einfach mit reingenommen, weil ich mir diesen Code nicht im Texteditor vom Forum durchgehen wollte und ich mir mein Control per Designer erstellt habe.

    Gruß
    ~blaze~


    Kommt bei diesem Code, wenn ich das Control auf die Form ziehen will.

    VB.NET-Quellcode

    1. Public Sub New()
    2. DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    3. ItemHeight = 100
    4. End Sub


    Bitte hilf mir weiter :D
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    NotImplementedException. ich denke mal, Deiner Klasse fehlt noch (mindestens) eine überladene Funktion.
    Poste mal Zeile 203 der Datei ListControl.vb +- ein paar Zeilen, Zeile 203 bitte markieren.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    ~blaze~ schrieb:

    Ja und ich liefer dir auch einen hoffentlich besseren Code...
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class ListControl
    2. Inherits ListBox
    3. 'evtl. noch mal anpassen
    4. Private fntSubTextFont As Font = SystemFonts.DefaultFont
    5. Private fntHeaderFont As Font = SystemFonts.DefaultFont
    6. Private clrSelectedBackColor As Color = Color.CornflowerBlue
    7. Private clrBorderColor As Color = Color.Gray
    8. Private clrSelectedBorderColor As Color = Color.DarkGray
    9. Private clrHeaderColor As Color = Color.Black
    10. Private clrSubTextColor As Color = Color.Gray
    11. Private bDrawDefaultItems As Boolean = True
    12. 'Category etc. nat. noch anfuegen, bin ich aber zu faul zu
    13. _
    14. Public Property DrawDefaultItems As Boolean
    15. Get
    16. Return bDrawDefaultItems
    17. End Get
    18. Set(ByVal value As Boolean)
    19. bDrawDefaultItems = value
    20. End Set
    21. End Property
    22. Public Property SubTextFont As Font
    23. Get
    24. Return fntSubTextFont
    25. End Get
    26. Set(ByVal value As Font)
    27. fntSubTextFont = value
    28. Invalidate()
    29. End Set
    30. End Property
    31. Public Property HeaderFont As Font
    32. Get
    33. Return fntHeaderFont
    34. End Get
    35. Set(ByVal value As Font)
    36. fntHeaderFont = value
    37. Invalidate()
    38. End Set
    39. End Property
    40. Public Property SubTextColor As Color
    41. Get
    42. Return clrSubTextColor
    43. End Get
    44. Set(ByVal value As Color)
    45. clrSubTextColor = value
    46. Invalidate()
    47. End Set
    48. End Property
    49. Public Property HeaderColor As Color
    50. Get
    51. Return clrHeaderColor
    52. End Get
    53. Set(ByVal value As Color)
    54. clrHeaderColor = value
    55. Invalidate()
    56. End Set
    57. End Property
    58. Public Property SelectedBorderColor As Color
    59. Get
    60. Return clrSelectedBorderColor
    61. End Get
    62. Set(ByVal value As Color)
    63. clrSelectedBorderColor = value
    64. Invalidate()
    65. End Set
    66. End Property
    67. Public Property SelectedBackColor As Color
    68. Get
    69. Return clrSelectedBackColor
    70. End Get
    71. Set(ByVal value As Color)
    72. clrSelectedBackColor = value
    73. Invalidate()
    74. End Set
    75. End Property
    76. Public Property BorderColor As Color
    77. Get
    78. Return clrBorderColor
    79. End Get
    80. Set(ByVal value As Color)
    81. clrBorderColor = value
    82. Invalidate()
    83. End Set
    84. End Property
    85. 'neusetzen verhindern
    86. _
    87. Public Overrides Property DrawMode As System.Windows.Forms.DrawMode
    88. Get
    89. Return MyBase.DrawMode
    90. End Get
    91. Set(ByVal value As System.Windows.Forms.DrawMode)
    92. MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    93. End Set
    94. End Property
    95. Public Sub New()
    96. InitializeComponent()
    97. DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    98. ItemHeight = 100
    99. End Sub
    100. Private Sub ListControl_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
    101. Dim item As Object = Items(e.Index)
    102. If TypeOf item Is ListControlItem Then ' ist das Item ein ListControlItem (im anderen Code auf eine... interessante Art geloest)
    103. Dim current As ListControlItem = DirectCast(item, ListControlItem) ' Casten zum ListControlItem
    104. 'Rechteck, in dem gezeichnet werden soll (s. o.)
    105. Dim r As Rectangle = New Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 3, e.Bounds.Height - 2)
    106. With e.Graphics
    107. Dim headerSize As Size
    108. Dim subtextSize As Size
    109. Dim elementBounds As Rectangle
    110. If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then 'ist das Item angewaehlt
    111. .FillRectangle(New SolidBrush(SelectedBackColor), r) 'Hintergrund zeichnen
    112. .DrawRectangle(New Pen(New SolidBrush(SelectedBorderColor)), r) 'Rahmen zeichnen
    113. Else
    114. 'wozu was mit einem durchsichtigen Hintergrund fuellen?!! Wenn dann noch BackColor oder so
    115. '.FillRectangle(New SolidBrush(Color.Transparent), r) 'transparentes Rechteck drueberlegen (lol)
    116. .DrawRectangle(New Pen(New SolidBrush(BorderColor)), r) 'Rahmen zeichnen
    117. End If
    118. If current.Image IsNot Nothing Then 'Wenn ein Bild vorhanden wird (Kotz-Faktor nr 2)
    119. .DrawImage(current.Image, r.X + 5, r.Y + 5, 45, 45)
    120. Else
    121. 'einfach gar nix zeichnen?!!
    122. '.DrawIcon(Drawing.SystemIcons.Error, r.X + 5, r.Y + 5) 'oder halt ein Fehlericon zeichnen
    123. End If
    124. headerSize = Size.Ceiling(.MeasureString(current.Header, HeaderFont)) 'Groesse des Headers ermitteln
    125. elementBounds = New Rectangle(r.X + 60, r.Y + 5, ClientSize.Width - r.X - 60, headerSize.Height) 'Bereich des Headers
    126. 'eigentlich per TextRenderer-Klasse (siehe weiter unten)?
    127. .DrawString(current.Header, HeaderFont, New SolidBrush(HeaderColor), elementBounds) 'Header zeichnen (Top-Text oben)
    128. subtextSize = Size.Ceiling(.MeasureString(current.SubText, SubTextFont)) 'Groesse des Subtexts ermitteln
    129. elementBounds = New Rectangle(r.X + 60, r.Y + 25, ClientSize.Width - r.X - 60, 60 - subtextSize.Height - headerSize.Height)
    130. .DrawString(current.Content, Font, New SolidBrush(Me.ForeColor), elementBounds) 'Inhalt zeichnen (main content oben)
    131. elementBounds = New Rectangle(r.X + 60, r.Y + Me.ItemHeight - 20, Me.Width - r.X - 60, subtextSize.Height)
    132. .DrawString(current.SubText, SubTextFont, New SolidBrush(SubTextColor), elementBounds) 'Subtext zeichnen
    133. 'Falls ausgewaehlt und gewuenscht, ein Fokusrechteck zeichnen
    134. If (e.State And (DrawItemState.Focus Or DrawItemState.NoFocusRect)) = DrawItemState.Focus Then
    135. ControlPaint.DrawFocusRectangle(e.Graphics, r)
    136. End If
    137. End With
    138. ElseIf DrawDefaultItems Then 'von mir eingefuehrte Eigenschaft, die es dem User ermoeglicht, nicht speziell behandelte Items ohne eigene Zeichenprozedur zu zeichnen
    139. e.DrawBackground()
    140. TextRenderer.DrawText(e.Graphics, GetItemText(item), Font, e.Bounds, ForeColor) 'hier ein BSP fuer den TextRenderer
    141. If (e.State And (DrawItemState.Focus Or DrawItemState.NoFocusRect)) = DrawItemState.Focus Then
    142. e.DrawFocusRectangle()
    143. End If
    144. End If
    145. End Sub
    146. Public NotInheritable Class ListControlItem
    147. Private ReadOnly imgImage As Image
    148. Private ReadOnly strHeader As String
    149. Private ReadOnly strContent As String
    150. Private ReadOnly strSubText As String
    151. Public ReadOnly Property Image As Image
    152. Get
    153. Return imgImage
    154. End Get
    155. End Property
    156. Public ReadOnly Property Header As String
    157. Get
    158. Return strHeader
    159. End Get
    160. End Property
    161. Public ReadOnly Property Content As String
    162. Get
    163. Return strContent
    164. End Get
    165. End Property
    166. Public ReadOnly Property SubText As String
    167. Get
    168. Return strSubText
    169. End Get
    170. End Property
    171. Public Sub New(ByVal image As Image, ByVal header As String, ByVal content As String, ByVal subText As String)
    172. imgImage = image
    173. strHeader = header
    174. strContent = content
    175. strSubText = subText
    176. End Sub
    177. End Class
    178. End Class


    Items fügst du per Code hinzu:

    VB.NET-Quellcode

    1. ListControlInstance.Items.Add(new ListControl.ListControlItem(...))

    bzw. bei dem Foltercode oben:

    VB.NET-Quellcode

    1. ListControlInstance.Items.Add(new ListControlItem(...))

    Die Klasse bei meinem Code kannst du natürlich noch herausziehen.
    Die Basis für den Code ist das DrawItem-Ereignis, das von der ListBox angeboten wird. Das wird eben dazu verwendet, die Items selber zu zeichnen, wenn die DrawMode-Eigenschaft angepasst wird. Über die übergebenen EventArgs werden die Eigenschaften des zu zeichnenden Elements angepasst.

    Gruß
    ~blaze~
    Blaze hat ihn hier gepostet.
    Das ist er.
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Gather schrieb:

    Blaze hat ihn hier gepostet.
    Das ist er.
    Da kommt leider keine Zeile 203 vor. :rolleyes:
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    warte... hat sich erledigt. Ich teste grad es funktioniert aber warte... ich schau noch.
    So nächstes Prob:
    Ich kann es nun adden aber wenn ich es größer machen will kommt folgender fehler:

    Ich versteh nicht was in zeile 107 falsch ist.
    Code

    VB.NET-Quellcode

    1. Public Class ListControl
    2. Inherits ListBox
    3. 'evtl. noch mal anpassen
    4. Private fntSubTextFont As Font = SystemFonts.DefaultFont
    5. Private fntHeaderFont As Font = SystemFonts.DefaultFont
    6. Private clrSelectedBackColor As Color = Color.CornflowerBlue
    7. Private clrBorderColor As Color = Color.Gray
    8. Private clrSelectedBorderColor As Color = Color.DarkGray
    9. Private clrHeaderColor As Color = Color.Black
    10. Private clrSubTextColor As Color = Color.Gray
    11. Private bDrawDefaultItems As Boolean = True
    12. 'Category etc. nat. noch anfuegen, bin ich aber zu faul zu
    13. <System.ComponentModel.DefaultValue(True)> _
    14. Public Property DrawDefaultItems As Boolean
    15. Get
    16. Return bDrawDefaultItems
    17. End Get
    18. Set(ByVal value As Boolean)
    19. bDrawDefaultItems = value
    20. End Set
    21. End Property
    22. Public Property SubTextFont As Font
    23. Get
    24. Return fntSubTextFont
    25. End Get
    26. Set(ByVal value As Font)
    27. fntSubTextFont = value
    28. Invalidate()
    29. End Set
    30. End Property
    31. Public Property HeaderFont As Font
    32. Get
    33. Return fntHeaderFont
    34. End Get
    35. Set(ByVal value As Font)
    36. fntHeaderFont = value
    37. Invalidate()
    38. End Set
    39. End Property
    40. Public Property SubTextColor As Color
    41. Get
    42. Return clrSubTextColor
    43. End Get
    44. Set(ByVal value As Color)
    45. clrSubTextColor = value
    46. Invalidate()
    47. End Set
    48. End Property
    49. Public Property HeaderColor As Color
    50. Get
    51. Return clrHeaderColor
    52. End Get
    53. Set(ByVal value As Color)
    54. clrHeaderColor = value
    55. Invalidate()
    56. End Set
    57. End Property
    58. Public Property SelectedBorderColor As Color
    59. Get
    60. Return clrSelectedBorderColor
    61. End Get
    62. Set(ByVal value As Color)
    63. clrSelectedBorderColor = value
    64. Invalidate()
    65. End Set
    66. End Property
    67. Public Property SelectedBackColor As Color
    68. Get
    69. Return clrSelectedBackColor
    70. End Get
    71. Set(ByVal value As Color)
    72. clrSelectedBackColor = value
    73. Invalidate()
    74. End Set
    75. End Property
    76. Public Property BorderColor As Color
    77. Get
    78. Return clrBorderColor
    79. End Get
    80. Set(ByVal value As Color)
    81. clrBorderColor = value
    82. Invalidate()
    83. End Set
    84. End Property
    85. 'neusetzen verhindern
    86. <System.ComponentModel.Browsable(False), System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
    87. Public Overrides Property DrawMode As System.Windows.Forms.DrawMode
    88. Get
    89. Return MyBase.DrawMode
    90. End Get
    91. Set(ByVal value As System.Windows.Forms.DrawMode)
    92. MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    93. End Set
    94. End Property
    95. ''' <summary>
    96. ''' New
    97. ''' </summary>
    98. ''' <remarks></remarks>
    99. Public Sub New()
    100. DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    101. ItemHeight = 100
    102. End Sub
    103. Private Sub ListControl_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
    104. Dim item As Object = Items(e.Index)
    105. If TypeOf item Is ListControlItem Then ' ist das Item ein ListControlItem (im anderen Code auf eine... interessante Art geloest)
    106. Dim current As ListControlItem = DirectCast(item, ListControlItem) ' Casten zum ListControlItem
    107. 'Rechteck, in dem gezeichnet werden soll (s. o.)
    108. Dim r As Rectangle = New Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 3, e.Bounds.Height - 2)
    109. With e.Graphics
    110. Dim headerSize As Size
    111. Dim subtextSize As Size
    112. Dim elementBounds As Rectangle
    113. If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then 'ist das Item angewaehlt
    114. .FillRectangle(New SolidBrush(SelectedBackColor), r) 'Hintergrund zeichnen
    115. .DrawRectangle(New Pen(New SolidBrush(SelectedBorderColor)), r) 'Rahmen zeichnen
    116. Else
    117. 'wozu was mit einem durchsichtigen Hintergrund fuellen?!! Wenn dann noch BackColor oder so
    118. '.FillRectangle(New SolidBrush(Color.Transparent), r) 'transparentes Rechteck drueberlegen (lol)
    119. .DrawRectangle(New Pen(New SolidBrush(BorderColor)), r) 'Rahmen zeichnen
    120. End If
    121. If current.Image IsNot Nothing Then 'Wenn ein Bild vorhanden wird (Kotz-Faktor nr 2)
    122. .DrawImage(current.Image, r.X + 5, r.Y + 5, 45, 45)
    123. Else
    124. 'einfach gar nix zeichnen?!!
    125. '.DrawIcon(Drawing.SystemIcons.Error, r.X + 5, r.Y + 5) 'oder halt ein Fehlericon zeichnen
    126. End If
    127. headerSize = Size.Ceiling(.MeasureString(current.Header, HeaderFont)) 'Groesse des Headers ermitteln
    128. elementBounds = New Rectangle(r.X + 60, r.Y + 5, ClientSize.Width - r.X - 60, headerSize.Height) 'Bereich des Headers
    129. 'eigentlich per TextRenderer-Klasse (siehe weiter unten)?
    130. .DrawString(current.Header, HeaderFont, New SolidBrush(HeaderColor), elementBounds) 'Header zeichnen (Top-Text oben)
    131. subtextSize = Size.Ceiling(.MeasureString(current.SubText, SubTextFont)) 'Groesse des Subtexts ermitteln
    132. elementBounds = New Rectangle(r.X + 60, r.Y + 25, ClientSize.Width - r.X - 60, 60 - subtextSize.Height - headerSize.Height)
    133. .DrawString(current.Content, Font, New SolidBrush(Me.ForeColor), elementBounds) 'Inhalt zeichnen (main content oben)
    134. elementBounds = New Rectangle(r.X + 60, r.Y + Me.ItemHeight - 20, Me.Width - r.X - 60, subtextSize.Height)
    135. .DrawString(current.SubText, SubTextFont, New SolidBrush(SubTextColor), elementBounds) 'Subtext zeichnen
    136. 'Falls ausgewaehlt und gewuenscht, ein Fokusrechteck zeichnen
    137. If (e.State And (DrawItemState.Focus Or DrawItemState.NoFocusRect)) = DrawItemState.Focus Then
    138. ControlPaint.DrawFocusRectangle(e.Graphics, r)
    139. End If
    140. End With
    141. ElseIf DrawDefaultItems Then 'von mir eingefuehrte Eigenschaft, die es dem User ermoeglicht, nicht speziell behandelte Items ohne eigene Zeichenprozedur zu zeichnen
    142. e.DrawBackground()
    143. TextRenderer.DrawText(e.Graphics, GetItemText(item), Font, e.Bounds, ForeColor) 'hier ein BSP fuer den TextRenderer
    144. If (e.State And (DrawItemState.Focus Or DrawItemState.NoFocusRect)) = DrawItemState.Focus Then
    145. e.DrawFocusRectangle()
    146. End If
    147. End If
    148. End Sub
    149. Public Class ListControlItem
    150. Private ReadOnly imgImage As Image
    151. Private ReadOnly strHeader As String
    152. Private ReadOnly strContent As String
    153. Private ReadOnly strSubText As String
    154. Public ReadOnly Property Image As Image
    155. Get
    156. Return imgImage
    157. End Get
    158. End Property
    159. Public ReadOnly Property Header As String
    160. Get
    161. Return strHeader
    162. End Get
    163. End Property
    164. Public ReadOnly Property Content As String
    165. Get
    166. Return strContent
    167. End Get
    168. End Property
    169. Public ReadOnly Property SubText As String
    170. Get
    171. Return strSubText
    172. End Get
    173. End Property
    174. ''' <summary>
    175. ''' ...
    176. ''' </summary>
    177. ''' <param name="image"></param>
    178. ''' <param name="header"></param>
    179. ''' <param name="content"></param>
    180. ''' <param name="subText"></param>
    181. ''' <remarks></remarks>
    182. Public Sub New(ByVal image As Image, ByVal header As String, ByVal content As String, ByVal subText As String)
    183. imgImage = image
    184. strHeader = header
    185. strContent = content
    186. strSubText = subText
    187. End Sub
    188. End Class
    189. End Class
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Zeile 203 wird von Zeile 107 aus aufgerufen.
    Die Zeilennummern im Bild stimmen nicht mit den Zeilennummern im Post überein :!:
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    wie kann man das dann fixxen?
    Mfg: Gather
    Private Nachrichten bezüglich VB-Fragen werden Ignoriert!


    Ich hab mal den letzten Code bei mir reingezogen, das läuft.
    Was für eine Studio-Version hast Du?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!