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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
| Option Strict On
Option Explicit On
Imports System.ComponentModel
#Region "Multi Colonne COMBOBOX"
Public Class MultiColumnComboBoxDropDownListColumn
Private sngWidth As Single
Private blnVisible As Boolean
Public Event MultiColumnComboBoxDropDownListColumnChanged()
Public Property Width() As Single
Get
Return sngWidth
End Get
Set(ByVal value As Single)
sngWidth = value
RaiseEvent MultiColumnComboBoxDropDownListColumnChanged()
End Set
End Property
Public Property Visible() As Boolean
Get
Return blnVisible
End Get
Set(ByVal value As Boolean)
blnVisible = value
RaiseEvent MultiColumnComboBoxDropDownListColumnChanged()
End Set
End Property
End Class
Public Class MultiColumnComboBox
Inherits ComboBox
' On masque la propriété afin qu'elle ne soit pas modifiable
' Propriété cachée pour le designer, accessible seulement par code
<Category("CustomProperty"), _
Description("Colonnes de la dropdownlist"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), _
Browsable(False)> _
Public Shadows ReadOnly Property DrawMode() As Windows.Forms.DrawMode
Get
Return MyBase.DrawMode
End Get
End Property
Public Sub New()
Me.DropDownStyle = ComboBoxStyle.DropDown
Me.FlatStyle = Windows.Forms.FlatStyle.System
' Taille personnalisée pour les items de la liste
' et dessin des items gérés, forcé seulement à l'exécution
If Not Me.DesignMode Then
MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
End If
fFont = Me.Font
End Sub
Private lstMyCol As New SortedList(Of Integer, MultiColumnComboBoxDropDownListColumn)
' Propriété cachée pour le designer, accessible seulement par code
<Category("CustomProperty"), _
Description("Colonnes de la dropdownlist"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Property DropDownListColumns() As SortedList(Of Integer, MultiColumnComboBoxDropDownListColumn)
Get
Return lstMyCol
End Get
Set(ByVal value As SortedList(Of Integer, MultiColumnComboBoxDropDownListColumn))
lstMyCol = value
End Set
End Property
Private sngItemHeight As Single
Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
' Ici on fixe une hauteur constante pour les items de la liste
e.ItemHeight = CInt(sngItemHeight)
End Sub
Private cComboBoxSelectedRowGradientColor1 As Color = Color.LightBlue
Private cComboBoxSelectedRowGradientColor2 As Color = Color.WhiteSmoke
Private fFont As Font
Private Const cstVerticalLineWidth As Integer = 1
Private Const cstSpcBeforeStringText As Integer = 2
Private Const cstSpcAfterStringText As Integer = 2
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
' index < 0, il s'agit de la zone d'édition du combo
If e.Index < 0 Then Exit Sub
'
' Couleur de fond :
' si sélectionné --> dégradé
' sinon --> on laisse faire
'
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
Dim backbrush As New _
System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, _
cComboBoxSelectedRowGradientColor1, _
cComboBoxSelectedRowGradientColor2, _
System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
e.Graphics.FillRectangle(backbrush, e.Bounds)
Else
e.DrawBackground()
End If
' Ligne horizontale de séparation des items de la liste
e.Graphics.DrawLine(Pens.Silver, e.Bounds.X, e.Bounds.Y + e.Bounds.Height - 1, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height - 1)
'
' Dessin des différents contenus pour chaque colonnes
'
Dim sngWidth As Single = 0
For i As Integer = 0 To Me.DropDownDataTable.Columns.Count - 1
If lstMyCol(i).Visible Then
' Ligne de séparation des colonnes
e.Graphics.DrawLine(Pens.Silver, sngWidth, e.Bounds.Y, sngWidth, _
e.Bounds.Y + e.Bounds.Height - 1)
sngWidth = sngWidth + cstVerticalLineWidth
' Espacement
sngWidth = sngWidth + cstSpcBeforeStringText
' Text de la colonne
Dim strValue As String = CStr(Me.DropDownDataTable.Rows(e.Index).Item(i))
e.Graphics.DrawString(strValue, fFont, System.Drawing.Brushes.Black, _
New RectangleF(sngWidth, e.Bounds.Y + 1, _
lstMyCol(i).Width, e.Bounds.Height))
sngWidth = sngWidth + lstMyCol(i).Width + cstSpcAfterStringText
End If
Next
' Dessin du Focus de l'item si besoin
e.DrawFocusRectangle()
End Sub
Private DropDownDataTable As New DataTable
Private Sub LoadDropDownDataTable()
' Le chargement en datatable est un prérequis pour pouvoir
' calculer la taille des différentes colonnes de la liste à l'affichage
DropDownDataTable = New DataTable
If Me.DataManager.List.Count = 0 Then Exit Sub
Dim t1 As Type = Me.DataManager.List.Item(0).GetType
Select Case True
Case TypeOf (Me.DataManager.List) Is Array
DropDownDataTable.Columns.Add("C")
For Each o As Object In Me.DataManager.List
Dim dr As DataRow = DropDownDataTable.NewRow()
dr(0) = o.ToString
DropDownDataTable.Rows.Add(dr)
Next
Case TypeOf (Me.DataManager.List) Is DataView
'
' La datasource est de type datatable ou dataset
' le .list du manager contient un dataview
'
Me.DropDownDataTable = CType(Me.DataManager.List, DataView).Table
Case Else
'
' La datasource est de type autre que datatable ou dataset
' une collection d'objets par exemple
'
'-----------------------------------------------------------------
' On considére implicitement que les objets du datasource sont
' tous du même type et que les propriétés qu'ils exposent
' sont toutes accessibles
'
Dim t As Type = Me.DataManager.List.Item(0).GetType()
Dim pDataSourceObjectProperties As System.Reflection.PropertyInfo() = t.GetProperties
For Each p As System.Reflection.PropertyInfo In pDataSourceObjectProperties
DropDownDataTable.Columns.Add(p.Name)
Next
'
' On charge les lignes de la datatable à partir des items de
' la liste en datasource
For Each o As Object In Me.DataManager.List
Dim dr As DataRow = DropDownDataTable.NewRow()
For Each p As System.Reflection.PropertyInfo In pDataSourceObjectProperties
dr(p.Name) = p.GetValue(o, Nothing)
Next
DropDownDataTable.Rows.Add(dr)
Next
End Select
End Sub
Private Sub SetDropDownSize()
'
Dim sngDropDownListWidth As Single
'
' On déduit la largeur de la dropdown liste
' en cumulant les largeurs des colonnes visibles
'
sngDropDownListWidth = 0
For i As Integer = 0 To lstMyCol.Count - 1
' xxx pour la ligne et un espacement, xxx en fin pour l'espacement
If lstMyCol(i).Visible Then
sngDropDownListWidth = cstSpcBeforeStringText + sngDropDownListWidth _
+ lstMyCol(i).Width + cstSpcAfterStringText
End If
Next
' On augmente la largeur pour éclaicir un peu à droite
sngDropDownListWidth = sngDropDownListWidth + 5
' On augmente la largeur pour la barre de défilement
' si la liste contient plus d'items que le nombre max affichable
If Me.DropDownDataTable.Rows.Count > Me.MaxDropDownItems Then
sngDropDownListWidth = sngDropDownListWidth + SystemInformation.VerticalScrollBarWidth
End If
Dim intWidth As Integer
intWidth = CInt(sngDropDownListWidth)
' Cas d'un liste vide
If intWidth <= 0 Then
intWidth = Me.Width
End If
MyBase.DropDownWidth = intWidth
If sngItemHeight = 0 Then
MyBase.DropDownHeight = Me.Height
Else
MyBase.DropDownHeight = CInt(sngItemHeight) * Me.MaxDropDownItems
End If
End Sub
Private Sub InitializeDropDownColumn()
lstMyCol.Clear()
' Mesure de la largeur max de chaque colonne
' ------------------------------------------
' Création du graphic utilisé par le control
Dim cGraphics As Graphics = Me.CreateGraphics()
' Pour chaque ligne de la table source, mesure de la longueur de la donnée
Dim rRow As DataRow
Dim sfSize As SizeF
sngItemHeight = 0
'
' On va définir pour chaque colonne de la datatable
' quelle est la taille maximum du texte quelle contient
'
For Each rRow In Me.DropDownDataTable.Rows
For i As Integer = 0 To DropDownDataTable.Columns.Count - 1
' On mesure la taille du texte
sfSize = cGraphics.MeasureString(CStr(rRow.Item(i)), fFont)
' Calcul de hauteur de ligne maximale
If sngItemHeight < sfSize.Height Then
sngItemHeight = sfSize.Height + 2
End If
' Calcul de la largeur de la colonne
If lstMyCol.ContainsKey(i) Then
If lstMyCol(i).Width < sfSize.Width Then
lstMyCol(i).Width = sfSize.Width + 1
End If
Else
Dim cMyCol As New MultiColumnComboBoxDropDownListColumn
cMyCol.Width = sfSize.Width + 1
cMyCol.Visible = True
lstMyCol.Add(i, cMyCol)
' Détection des modifications de colonnes
AddHandler cMyCol.MultiColumnComboBoxDropDownListColumnChanged, AddressOf Me.SetDropDownSize
End If
Next
Next
cGraphics.Dispose()
Me.SetDropDownSize()
End Sub
Protected Overrides Sub OnDataSourceChanged( _
ByVal e As EventArgs _
)
MyBase.OnDataSourceChanged(e)
Me.LoadDropDownDataTable()
Me.InitializeDropDownColumn()
End Sub
End Class
#End Region |