Impression

fallpoint - 16 nov. 2012 à 11:32
 Utilisateur anonyme - 9 déc. 2012 à 23:13
bonjour à tous je veut imprimer le contenu d'un textbox multiline avec le code si dessous mais mon probléme est que sa imprime tout sur une même ligne. comment faire pour aller à la ligne ?
si quelqu'un peut m'aider svp
merci d'avance
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' autorise le choix de la page d'impression
        PrintDialog1.AllowSomePages = True
        ' montre le bouton d'aide
        PrintDialog1.ShowHelp = True
        ' Fixe les propriétés du documents  (impératif)
        PrintDialog1.Document = docToPrint
        Dim result As DialogResult = PrintDialog1.ShowDialog()
        docToPrint.DefaultPageSettings.Landscape = True
        ' Si le résultat est bon on imprime
        If (result = Windows.Forms.DialogResult.OK) Then
            docToPrint.Print()
        End If    
    End Sub
 Private Sub document_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docToPrint.PrintPage
        'champs de saisie
 lab1 As String = Label1.Text
 
        Dim printFont As New System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Regular)
        'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
        Dim strText As String = TextBox1.Text
        Dim myreader = New StringReader(strText)
        Dim linesPerPage As Single = 0
        Dim yPosition As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = e.MarginBounds.Left
        Dim topMargin As Single = e.MarginBounds.Top
        Dim line As String = Nothing
        Dim printFont1 As Font = TextBox1.Font
        Dim myBrush As New SolidBrush(Color.Black)
        linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
        While count < linesPerPage
            line = myreader.ReadLine()
            If (line Is Nothing) Then
                Exit While
            Else
                yPosition = topMargin + count * printFont.GetHeight(e.Graphics)
 
                e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, New StringFormat())
 
                count += 1
                If count >= linesPerPage Then
                    e.HasMorePages = True
                    count = 0
                End If
            End If
        End While
        e.HasMorePages = False
        myBrush.Dispose()
    'champ de saisie
        e.Graphics.DrawString(lab1, printFont, System.Drawing.Brushes.Black, 300, 80)
    End Sub

4 réponses

Utilisateur anonyme
18 nov. 2012 à 18:42
Bonsoir,

Utilise Graphics.MeasureString pour déterminer si la longueur de la ligne ne dépasse pas la largeur de la feuille. Dans ce cas, découpe la ligne et appelle DrawString autant de fois que nécéssaire.
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
18 nov. 2012 à 23:04
Bonsoir

si cela imprime une ligne c'est que tu n'as pas
introduit le enter dans le text et donc ton textbox n'a qu'une ligne
es tu sur que multiline est à true
voila une code plus simple qui imprime le contenu
d'un textbox
un bouton nommé buttonprint
un textbox nommé textbox1
Private Sub Buttonprint_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ButtonPlay.MouseClick
        Try
            Dim printdoc As New PrintDocument
            Dim ppd As New PrintPreviewDialog
            AddHandler printdoc.PrintPage, AddressOf printtext
            ppd.Document = printdoc
            Dim ps As New PageSettings
            
            ps.Landscape = True

            printdoc.DefaultPageSettings = ps
            ppd.WindowState = FormWindowState.Maximized
            'imprssion si ok
            If ppd.ShowDialog = Windows.Forms.DialogResult.OK Then
                ppd.Document.Print()
            End If
            'printdoc.Print()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub printtext(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        ev.Graphics.DrawString(TextBox1.Text, New Font("arial", 12, FontStyle.Bold), Brushes.Black, 10, 10)
        ev.HasMorePages = False
    End Sub
0
bonsoir et merci pour les réponse banana32 pouvez vous me donner plus de détails svp
0
Utilisateur anonyme
9 déc. 2012 à 23:13
Bonsoir,

Tu as des tas d'exemples sur l'impression sur msdn ici.

Bonne nuit.
0
Rejoignez-nous