Voici comment realiser une columns dans listbox.
Vous pouvez choisir la couleur du texte dans la columns et choisir aussi quand vous sélectionnez la couleur dans listbox.
ma classe :
Imports System.Runtime.InteropServices
Imports System.Drawing.Drawing2D
Public Class ListboxColumns : Inherits ListBox
Public _ColorSetection As Color
Public Property ColorSelection As Color
Get
Return _ColorSetection
End Get
Set(value As Color)
If value = Color.Transparent OrElse ForeColor = value Then
Return
Else
_ColorSetection = value
Me.Invalidate()
End If
End Set
End Property
Public _ColumnsColor As Color
Public Property ColumnsColor As Color
Get
Return _ColumnsColor
End Get
Set(value As Color)
If value = Color.Transparent Then
Return
Else
_ColumnsColor = value
Me.Invalidate()
End If
End Set
End Property
Public _Text As String
Public Property MyText() As String
Get
Return _Text
End Get
Set(value As String)
_Text = value
Me.Invalidate()
End Set
End Property
Private _color As Color
Public Property ColorText() As Color
Get
Return _color
End Get
Set(value As Color)
If value = Color.Transparent Then
Return
Else
_color = value
Me.Invalidate()
End If
End Set
End Property
Private _alignement As StringAlignment
Public Property AlignementText() As StringAlignment
Get
Return _alignement
End Get
Set(value As StringAlignment)
_alignement = value
Me.Invalidate()
End Set
End Property
Public Sub New()
Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.StandardClick Or ControlStyles.OptimizedDoubleBuffer, True)
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
Me.Font = New Font("Times New Roman", 8, FontStyle.Regular)
Me._ColumnsColor = Color.Yellow
Me._Text = "ListboxColumns"
Me._color = Color.Black
Me._alignement = StringAlignment.Center
Me.Size = New Size(120, 95)
Me._ColorSetection = Color.Red
Me.ItemHeight = 22
Me.IntegralHeight = False
Me.Name = "ListboxColumns"
Me.ForeColor = Color.Black
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If m.Msg = 15 Then CustomPaint()
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
Try
If e.Index >= 0 Then
Dim ItemBounds As New Rectangle(0, e.Bounds.Y + 21, e.Bounds.Width, e.Bounds.Height)
e.Graphics.FillRectangle(New SolidBrush(BackColor), ItemBounds)
If InStr(e.State.ToString, "Selected,") > 0 Then
e.Graphics.FillRectangle(New SolidBrush(_ColorSetection), ItemBounds)
e.Graphics.DrawString(Items(e.Index).ToString(), Me.Font, New SolidBrush(Me.ForeColor), ItemBounds)
Else
e.Graphics.DrawString(Items(e.Index).ToString(), Me.Font, New SolidBrush(Me.ForeColor), ItemBounds)
End If
End If
MyBase.OnDrawItem(e)
Catch ex As Exception : End Try
End Sub
Private Sub CustomPaint()
Dim rect As New Rectangle(0, 0, Me.Width - 2, 20)
CreateGraphics.DrawRectangle(Pens.Black, rect)
CreateGraphics.FillRectangle(New SolidBrush(_ColumnsColor), rect)
Using sf As New StringFormat With {.Alignment = _alignement, .LineAlignment = StringAlignment.Center}
CreateGraphics.DrawString(Me._Text, Me.Font, New SolidBrush(_color), rect, sf)
End Using
End Sub
Protected Overrides Sub OnResize(ByVal e As EventArgs)
MyBase.OnResize(e)
If Me.Width < 120 Then
Me.Width = 120
ElseIf Me.Height < 95 Then
Me.Height = 95
End If
End Sub
End Class
Aucun commentaire:
Enregistrer un commentaire