Ajouter des contrôles dans une listview

Ajouter des contrôles dans une Listview

Description

Ce tutoriel est composé de plusieurs fonctions permettant d'ajouter un ou plusieurs contrôle(s) avec Mise à jour du positionnement et des dimensions des contrôles ajoutés lors du redimensionnement des colonnes du DataGrivView ou de la Listview.

Contrôles pris en charge :
- Button
- CheckBox
- Label
- LinkLabel
- ProgressBar
- RadioButton
- TextBox
- RichTextBox
- ComboBox
- MaskedTextBox

Je vous laisse découvrir sans plus attendre ces fonctions.

Fonction ajoutant le contrôle dans la ListView

Pour ajouter un contrôle à un ListView avec mise à jour de la position et dimension des contrôles lors d'un redimensionnement des colonnes :

Exemple utilisation :

'
Dim bouton As New Button
Ajouter_Controles_ListView(ListView1, bouton, 0, 0)

Note 1 : Ajoute le contrôle bouton dans ListView1 (1° ligne, 1° colonne)
Note 2 : Pour que le code fonctionne correctement, il faut que ListView1.Items(0).Subitems(0) existe

'
Sub Ajouter_Controles_ListView(ByVal controle_target As ListView, ByVal controle_add As Windows.Forms.Control, ByVal no_ligne As Integer, ByVal no_colonne As Integer)

         ' Teste si controle_add est bien :
        ' Un Button /ou/ une CheckBox /ou/ un Label /ou/ un LinkLabel /ou/ une ProgressBar /ou/ un RadioButton /ou/ une TextBox /ou/ un RichTextBox
        If controle_add.GetType.FullName <> "System.Windows.Forms.Button" And controle_add.GetType.FullName <> "System.Windows.Forms.CheckBox" _
             And controle_add.GetType.FullName <> "System.Windows.Forms.Label" And controle_add.GetType.FullName <> "System.Windows.Forms.LinkLabel" _
             And controle_add.GetType.FullName <> "System.Windows.Forms.ProgressBar" And controle_add.GetType.FullName <> "System.Windows.Forms.RadioButton" _
             And controle_add.GetType.FullName <> "System.Windows.Forms.TextBox" And controle_add.GetType.FullName <> "System.Windows.Forms.RichTextBox" _
             And controle_add.GetType.FullName <> "System.Windows.Forms.ComboBox" And controle_add.GetType.FullName <> "System.Windows.Forms.MaskedTextBox" Then

            ' Si controle_add n'est pas l'un des contrôles précedemment cités,
            ' On affiche un message pour dire que le contrôle choisi n'est pas correcte
            MsgBox("Désolé, " + controle_add.GetType.FullName + " n'est pas accepté")
            Exit Sub
        End If

        ' Teste si le 3° paramètre no_ligne est compris entre 0 et le nombre de ligne du DatagridView - 1
        If no_ligne < 0 Then
            MsgBox("Désolé, le 3° paramètre no_ligne de la fonction est incorrecte")
            Exit Sub
        End If

        ' Teste si no_ligne correspond à l'index d'un item existant
        If no_ligne > controle_target.Items.Count - 1 Then
            MsgBox("Désolé, l'item ayant pour index " + no_ligne.ToString + " n'existe pas")
            Exit Sub
        End If

        ' Teste si le 4° paramètre no_colonne est compris entre 0 et le nombre de colonnes du DataGridView - 1
        If no_colonne < 0 Then
            MsgBox("Désolé, le 4° paramètre no_colonne de la fonction est incorrecte")
            Exit Sub
        End If

        'Teste si no_colonne correspond à l'index d'une colonne existante
        If no_colonne > controle_target.Columns.Count - 1 Then
            MsgBox("Désolé, la colonne ayant pour index " + no_colonne.ToString + " n'existe pas")
            Exit Sub
        End If

        If controle_add.GetType.FullName = "System.Windows.Forms.Button" Then
            Dim Control As New Button
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.CheckBox" Then
            Dim Control As New CheckBox
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.Label" Then
            Dim Control As New Label
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.LinkLabel" Then
            Dim Control As New LinkLabel
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.ProgressBar" Then
            Dim Control As New ProgressBar
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.Radiobutton" Then
            Dim Control As New RadioButton
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.TextBox" Then
            Dim Control As New TextBox
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.RichTextBox" Then
            Dim Control As New RichTextBox
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.ComboBox" Then
            Dim Control As New ComboBox
            controle_target.Controls.Add(Control)
        ElseIf controle_add.GetType.FullName = "System.Windows.Forms.MaskedTextBox" Then
            Dim Control As New MaskedTextBox
            controle_target.Controls.Add(Control)
        End If

        ' Utilise la propriété Tag pour se souvenir où a été ajouté le contrôle
        controle_target.Controls(controle_target.Controls.Count - 1).Tag = no_ligne.ToString + "|" + no_colonne.ToString

        ' Place controle_add dans la cellule choisie
        With controle_target
            .Controls(controle_target.Controls.Count - 1).Top = .Items(no_ligne).SubItems(no_colonne).Bounds.Top
            .Controls(controle_target.Controls.Count - 1).Left = .Items(no_ligne).SubItems(no_colonne).Bounds.Left
            .Controls(controle_target.Controls.Count - 1).Width = .Items(no_ligne).SubItems(no_colonne).Bounds.Width
            .Controls(controle_target.Controls.Count - 1).Height = .Items(no_ligne).SubItems(no_colonne).Bounds.Height
        End With

        ' Ajoute un évènement ColumnWidthChanging et l'associe à la fonction MAJ_positions_dimensions_controles
        ' La fonction se déclenchera dés que la largeur d'une colonne sera modifiée
        AddHandler controle_target.ColumnWidthChanging, AddressOf MAJ_positions_dimensions_controles_ListView

    End Sub

Mise à jour positionnement et dimension

Fonction permettant de mettre à jour le positionnement et la dimension des contrôles ajoutés lors d'un redimensionnement d'une colonne de la ListView

'
Sub MAJ_positions_dimensions_controles_ListView(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnWidthChangingEventArgs)
        For nb As Integer = 0 To sender.Controls.Count - 1
            For nbitem As Integer = 0 To sender.Items.Count - 1
                If e.ColumnIndex  <>  0 Then
                    If sender.Controls(nb).Tag  = nbitem.ToString + "|" + (e.ColumnIndex - 1).ToString Then
                        With sender.Items(nbitem).SubItems(e.ColumnIndex - 1).Bounds
                            sender.Controls(nb).Top = .Top
                            sender.Controls(nb).Left = .Left
                            sender.Controls(nb).Width = .Width
                            sender.Controls(nb).Height = .Height
                        End With
                    End If
                End If
                For nbre_colonnes As Integer = e.ColumnIndex To sender.Columns.Count - 1
                    If sender.Controls(nb).Tag = nbitem.ToString + "|" + (nbre_colonnes).ToString Then
                        With sender.Items(nbitem).SubItems(nbre_colonnes).Bounds
                            sender.Controls(nb).Top = .Top
                            sender.Controls(nb).Left = .Left
                            sender.Controls(nb).Width = .Width
                            sender.Controls(nb).Height = .Height
                        End With
                    End If
                Next
            Next
        Next
    End Sub

Voilà c'est terminé...

Si vous avez des questions, n'hésitez pas à venir me les poser en MP

++

Fauve

Ce document intitulé « Ajouter des contrôles dans une listview » issu de CodeS SourceS (codes-sources.commentcamarche.net) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.
Rejoignez-nous