Forum Clubic

[RESOLU] [VB.NET 2002] Imprimer un listview

Bonjour à tous,

Je suis désespérément à la recherche d’un petit bout de code me permettant d’imprimer le contenu d’un ListView.

J’ai essayé ceci mais sans succès, l’impression se lance bien, mais la page reste vierge :


    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
       PrintDocument1.PrintPage
        Dim myPaintArgs As New PaintEventArgs(e.Graphics, New Rectangle(New Point(0, 0), Me.Size))
        Me.InvokePaint(lstv, myPaintArgs)
    End Sub

Puis dans un bouton :


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        PrintDocument1.DefaultPageSettings.Landscape = True

        Dim dllg As New PrintPreviewDialog()
        dllg.Document = PrintDocument1
        dllg.ShowDialog()


        Dim dlg As New PrintDialog()
        dlg.Document = PrintDocument1

        Dim result As DialogResult = dlg.ShowDialog()

        If (result = System.Windows.Forms.DialogResult.OK) Then
            PrintDocument1.Print()
        End If
    End Sub

Merci d’avance pour votre aide.

Amicalement,
Yan

Il semblerait qu’il ne soit pas possible de récupérer ou d’imprimer des données venant d’un ListView (peu être en utilisant des api) mais là trop compliqué pour moi. Le seul moyen que j’ai trouver, c’est de les stocker dans un tableau a colonne de type string (désoler c’est du C#), toutes les valeurs que tu met dans le listview ainsi tu peu les récupérer plus tard

        string[,] tableau = new string[2, 2];
        private void Button1_Click(object sender, EventArgs e)
        {
             tableau[1, 1] = Form1.Text;
             tableau[1, 2] = textbox1.Text;
             tableau[2, 1] = textbox2.Text;
             // Et après tu récupere les données
             textbox3.Text = tableau[1, 1] + tableau[1, 2]; 
        }

Salut, merci pour ta réponse.

Comme tu le dis, iln’y a pas de solution “clef en main” j’ai trouvé le code ci-dessous je ne sais où hier, et ça marche. Cela consiste à convertir chaque cellule du listview en label, puis à l’imprimer.



'Méthode Utilisé une fois par page 
'tu l'appelle dans l'évenement : Private Sub PP_PrintPage(sender As Object, 
ev As PrintPageEventArgs) 


 Public Sub PrintListView(ByRef e As Drawing.Printing.PrintPageEventArgs, 
ByVal Position As Drawing.Rectangle, ByVal Lst As ListView, Optional ByVal 
NewPrint As Boolean = False, Optional ByVal RepeatFirstCol As Boolean = 
False) 
        Static Cl, Ln As Integer 
        Dim LbTmp As New Label 
        Dim nbCol, LeftPos, TopPos As Integer 
        Dim It As ListViewItem 


        If NewPrint Then 
            Cl = 0 
            Ln = 0 
        End If 


        LeftPos = Position.Left 
        TopPos = Position.Top 


        'Colonne 
        If Cl = Lst.Columns.Count Then Cl = 0 
        If RepeatFirstCol And Cl <> 0 Then 
            LbTmp.Text = Lst.Columns(0).Text 
            Select Case Lst.Columns(0).TextAlign 
                Case HorizontalAlignment.Center 
                    LbTmp.TextAlign = ContentAlignment.MiddleCenter 
                Case HorizontalAlignment.Left 
                    LbTmp.TextAlign = ContentAlignment.MiddleLeft 
                Case HorizontalAlignment.Right 
                    LbTmp.TextAlign = ContentAlignment.MiddleRight 
            End Select 
            LbTmp.Width = Lst.Columns(0).Width 
            LbTmp.Left = LeftPos 
            LbTmp.Top = TopPos 


            LbTmp.BackColor = Color.LightGray 
            LbTmp.Font = Lst.Font 
            LbTmp.BorderStyle = BorderStyle.FixedSingle 


            PrintLabel(LbTmp, e) 


            LeftPos += Lst.Columns(0).Width 
        End If 
        For I As Integer = Cl To Lst.Columns.Count - 1 
            LbTmp.Text = Lst.Columns(I).Text 
            Select Case Lst.Columns(I).TextAlign 
                Case HorizontalAlignment.Center 
                    LbTmp.TextAlign = ContentAlignment.MiddleCenter 
                Case HorizontalAlignment.Left 
                    LbTmp.TextAlign = ContentAlignment.MiddleLeft 
                Case HorizontalAlignment.Right 
                    LbTmp.TextAlign = ContentAlignment.MiddleRight 
            End Select 
            LbTmp.Width = Lst.Columns(I).Width 
            LbTmp.Left = LeftPos 
            LbTmp.Top = TopPos 


            LbTmp.BackColor = Color.LightGray 
            LbTmp.Font = Lst.Font 
            LbTmp.BorderStyle = BorderStyle.FixedSingle 


            PrintLabel(LbTmp, e) 


            LeftPos += Lst.Columns(I).Width 
            If I = Lst.Columns.Count - 1 Then 
                nbCol = I 
            Else 
                If LeftPos + Lst.Columns(I + 1).Width > Position.Width + 
Position.Left Then 
                    nbCol = I 
                    Exit For 
                End If 
            End If 
        Next 


        'LIGNE 
        TopPos += LbTmp.Height 


        For J As Integer = Ln To Lst.Items.Count - 1 
            It = Lst.Items(J) 
            LeftPos = Position.Left 
            If RepeatFirstCol And Cl <> 0 Then 
                LbTmp.Text = It.Text 
                Select Case Lst.Columns(0).TextAlign 
                    Case HorizontalAlignment.Center 
                        LbTmp.TextAlign = ContentAlignment.MiddleCenter 
                    Case HorizontalAlignment.Left 
                        LbTmp.TextAlign = ContentAlignment.MiddleLeft 
                    Case HorizontalAlignment.Right 
                        LbTmp.TextAlign = ContentAlignment.MiddleRight 
                End Select 
                LbTmp.Width = Lst.Columns(0).Width 
                LbTmp.Left = LeftPos 
                LbTmp.Top = TopPos 
                LbTmp.BackColor = It.BackColor 
                LbTmp.ForeColor = It.ForeColor 
                LbTmp.Font = Lst.Font 
                LbTmp.BorderStyle = BorderStyle.FixedSingle 


                PrintLabel(LbTmp, e) 
                LeftPos += LbTmp.Width 
            End If 
            For I As Integer = Cl To nbCol 
                Dim Sb As ListViewItem.ListViewSubItem = It.SubItems(I) 
                LbTmp.Text = Sb.Text 
                Select Case Lst.Columns(I).TextAlign 
                    Case HorizontalAlignment.Center 
                        LbTmp.TextAlign = ContentAlignment.MiddleCenter 
                    Case HorizontalAlignment.Left 
                        LbTmp.TextAlign = ContentAlignment.MiddleLeft 
                    Case HorizontalAlignment.Right 
                        LbTmp.TextAlign = ContentAlignment.MiddleRight 
                End Select 
                LbTmp.Width = Lst.Columns(I).Width 
                LbTmp.Left = LeftPos 
                LbTmp.Top = TopPos 
                If Not It.UseItemStyleForSubItems Then 
                    LbTmp.BackColor = Sb.BackColor 
                    LbTmp.ForeColor = Sb.ForeColor 
                    LbTmp.Font = Sb.Font 
                Else 
                    LbTmp.BackColor = It.BackColor 
                    LbTmp.ForeColor = It.ForeColor 
                    LbTmp.Font = It.Font 
                End If 


                LbTmp.Font = Lst.Font 
                LbTmp.BorderStyle = BorderStyle.FixedSingle 


                PrintLabel(LbTmp, e) 
                LeftPos += LbTmp.Width 
            Next 
            TopPos += LbTmp.Height 
            If J = Lst.Items.Count - 1 Then 
                If nbCol < Lst.Columns.Count - 1 Then 
                    e.HasMorePages = True 
                    Cl = nbCol + 1 
                End If 
            Else 
                If TopPos > Position.Top + Position.Height Then 
                    If nbCol < Lst.Columns.Count - 1 Then 
                        e.HasMorePages = True 
                        Cl = nbCol + 1 
                        Exit For 
                    Else 
                        e.HasMorePages = True 
                        Ln = J + 1 
                        Cl = 0 
                        Exit For 
                    End If 
                End If 
            End If 
        Next 


    End Sub 


__________________________________ 


    Public Sub PrintLabel(ByVal Lb As Label, ByVal E As 
System.Drawing.Printing.PrintPageEventArgs) 
        Dim RecF As New RectangleF(Lb.Left, Lb.Top, Lb.Width, Lb.Height) 


        'BackGround 
        E.Graphics.FillRectangle(New SolidBrush(Lb.BackColor), RecF) 


        'Bordure 
        Select Case Lb.BorderStyle 
            Case BorderStyle.FixedSingle 
                E.Graphics.DrawRectangle(System.Drawing.Pens.Black, Lb.Left, 
Lb.Top, Lb.Width, Lb.Height) 
            Case BorderStyle.Fixed3D 
        End Select 


        'TEXT 
        Dim F As New System.Drawing.StringFormat 
        Select Case Lb.TextAlign 
            Case ContentAlignment.BottomCenter 
                F.Alignment = StringAlignment.Center 
                F.LineAlignment = StringAlignment.Far 


            Case ContentAlignment.BottomLeft 
                F.Alignment = StringAlignment.Near 
                F.LineAlignment = StringAlignment.Far 


            Case ContentAlignment.BottomRight 
                F.Alignment = StringAlignment.Far 
                F.LineAlignment = StringAlignment.Far 


            Case ContentAlignment.MiddleCenter 
                F.Alignment = StringAlignment.Center 
                F.LineAlignment = StringAlignment.Center 


            Case ContentAlignment.MiddleLeft 
                F.Alignment = StringAlignment.Near 
                F.LineAlignment = StringAlignment.Center 


            Case ContentAlignment.MiddleRight 
                F.Alignment = StringAlignment.Far 
                F.LineAlignment = StringAlignment.Center 


            Case ContentAlignment.TopCenter 
                F.Alignment = StringAlignment.Center 
                F.LineAlignment = StringAlignment.Near 


            Case ContentAlignment.TopLeft 
                F.Alignment = StringAlignment.Near 
                F.LineAlignment = StringAlignment.Near 


            Case ContentAlignment.TopRight 
                F.Alignment = StringAlignment.Far 
                F.LineAlignment = StringAlignment.Near 
        End Select 


        E.Graphics.DrawString(Lb.Text, Lb.Font, New 
SolidBrush(Lb.ForeColor), RecF, F) 
    End Sub 
__________________________________ 

A+
Yan

wow …

le mieux c’est de le formater en html ou rtf et l’imprimer …

Salut,

Merci pour ta réponse, mais comment t’y prend-tu ?

A+
Yan