dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] how to print in vb.net

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    57

    Resolved [RESOLVED] how to print in vb.net

    hi vbforum members
    i want learn how to print in vb.net and use different aspect to print what i want in form
    and datagridview

  2. #2
    Addicted Member
    Join Date
    Sep 2009
    Posts
    129

    Re: how to print in vb.net


  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: how to print in vb.net

    Here's a solution for printing DGVs

    https://code.msdn.microsoft.com/VBNe...ample-bc3b0176

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: how to print in vb.net

    Ok. Here's a slightly modified PrintPage sub from my code that you tried. I've highlighted the changes...


    Code:
    ''' <summary> 
    ''' this is the actual printing routine. 
    ''' using the pagedetails i calculated earlier, it prints a title, 
    ''' + as much of the datagridview as will fit on 1 page, then moves to the next page. 
    ''' this is setup to be dynamic. try resizing the dgv columns or rows 
    ''' </summary> 
    ''' <param name="sender"></param> 
    ''' <param name="e"></param> 
    ''' <remarks></remarks> 
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim rect As New Rectangle(20, 20, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label1.Height)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
    
        Static startPage As Integer = 0
    
            '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        if startPage = 0 Then
            'This part draws the 2 labels and textbox
            e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, rect, sf)
            rect = New Rectangle(20, rect.Bottom, TextBox1.Width, TextBox1.Height) 'Label2.Height)
            e.Graphics.DrawRectangle(Pens.Black, rect)
            e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Black, rect, sf)
            rect = New Rectangle(20, rect.Bottom, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label2.Height)
            e.Graphics.DrawString(Label2.Text, Label2.Font, Brushes.Black, rect, sf) 
        End If
       '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
    
        sf.Alignment = StringAlignment.Near
    
        Dim startX As Integer = 50
        Dim startY As Integer = if(startPage > 0, rect.Bottom, 20)
    
        For p As Integer = startPage To pages.Count - 1
            Dim cell As New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.ColumnHeadersHeight)
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
            e.Graphics.DrawRectangle(Pens.Black, cell)
    
            startY += DataGridView1.ColumnHeadersHeight
    
            For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
                cell = New Rectangle(startX, startY, DataGridView1.RowHeadersWidth, DataGridView1.Rows(r).Height)
                e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
                e.Graphics.DrawRectangle(Pens.Black, cell)
                e.Graphics.DrawString(DataGridView1.Rows(r).HeaderCell.Value.ToString, DataGridView1.Font, Brushes.Black, cell, sf)
                startY += DataGridView1.Rows(r).Height
            Next
    
            startX += cell.Width
            startY = rect.Bottom
            For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.ColumnHeadersHeight)
                e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
                e.Graphics.DrawRectangle(Pens.Black, cell)
                e.Graphics.DrawString(DataGridView1.Columns(c).HeaderCell.Value.ToString, DataGridView1.Font, Brushes.Black, cell, sf)
                startX += DataGridView1.Columns(c).Width
            Next
    
            startY = rect.Bottom + DataGridView1.ColumnHeadersHeight
    
            For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
                startX = 50 + DataGridView1.RowHeadersWidth
                For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                    cell = New Rectangle(startX, startY, DataGridView1.Columns(c).Width, DataGridView1.Rows(r).Height)
                    e.Graphics.DrawRectangle(Pens.Black, cell)
                    e.Graphics.DrawString(DataGridView1(c, r).Value.ToString, DataGridView1.Font, Brushes.Black, cell, sf)
                    startX += DataGridView1.Columns(c).Width
                Next
                startY += DataGridView1.Rows(r).Height
            Next
    
            If p <> pages.Count - 1 Then
                startPage = p + 1
                e.HasMorePages = True
                Return
            Else
                startPage = 0
            End If
    
        Next
    
    End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    57

    Re: how to print in vb.net

    yes it works fine thanks a lot
    Name:  Sans titre.jpg
Views: 58
Size:  27.7 KB
    and the rowheader is up a little and missing rows in the botom

    Code:
             If startPage = 0 Then
                e.Graphics.DrawString(Lb1.Text, Lb1.Font, Brushes.Black, rect, sf)
                'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
                rect = New Rectangle(370, 20, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label3.Height)
                e.Graphics.DrawString(Label3.Text, Label3.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(310, 20, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label2.Height)
                e.Graphics.DrawString(Label2.Text, Label2.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(-290, 50, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label4.Height)
                e.Graphics.DrawString(Label4.Text, Label4.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(-200, 50, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), LbNum.Height)
                e.Graphics.DrawString(LbNum.Text, LbNum.Font, Brushes.Black, rect, sf)
    
    
                rect = New Rectangle(-60, 50, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label8.Height)
                e.Graphics.DrawString(Label8.Text, Label8.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(0, 50, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), DateSrt.Height)
                e.Graphics.DrawString(DateSrt.Text, DateSrt.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(-280, 80, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label5.Height)
                e.Graphics.DrawString(Label5.Text, Label5.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(-200, 80, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), LbFrs.Height)
                e.Graphics.DrawString(LbFrs.Text, LbFrs.Font, Brushes.Black, rect, sf)
    
    
                rect = New Rectangle(-80, 80, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Frs.Height)
                e.Graphics.DrawString(Frs.Text, Frs.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(80, 80, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label9.Height)
                e.Graphics.DrawString(Label9.Text, Label9.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(230, 80, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), ReferenceLb.Height)
                e.Graphics.DrawString(ReferenceLb.Text, ReferenceLb.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(-290, 110, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label6.Height)
                e.Graphics.DrawString(Label6.Text, Label6.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(-200, 110, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), FactDeviseLb.Height)
                e.Graphics.DrawString(FactDeviseLb.Text, FactDeviseLb.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(-50, 110, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label10.Height)
                e.Graphics.DrawString(Label10.Text, Label10.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(20, 110, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), LbCours.Height)
                e.Graphics.DrawString(LbCours.Text, LbCours.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(150, 110, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label12.Height)
                e.Graphics.DrawString(Label12.Text, Label12.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(260, 110, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), TotConvertiLb.Height)
                e.Graphics.DrawString(TotConvertiLb.Text, TotConvertiLb.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(-280, 140, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label7.Height)
                e.Graphics.DrawString(Label7.Text, Label7.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(-180, 140, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), LabelFraisGlob.Height)
                e.Graphics.DrawString(LabelFraisGlob.Text, LabelFraisGlob.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(-70, 140, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label11.Height)
                e.Graphics.DrawString(Label11.Text, Label11.Font, Brushes.Black, rect, sf)
    
                rect = New Rectangle(30, 140, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label19.Height)
                e.Graphics.DrawString(Label19.Text, Label19.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(150, 140, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label13.Height)
                e.Graphics.DrawString(Label13.Text, Label13.Font, Brushes.Black, rect, sf)
                rect = New Rectangle(260, 140, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Label22.Height)
                e.Graphics.DrawString(Label22.Text, Label22.Font, Brushes.Black, rect, sf)
    
            End If
           ''@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    Last edited by houssem; May 17th, 2019 at 07:21 AM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: how to print in vb.net

    Try this. It should work a lot better...

    Printing example rev 2.zip

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: how to print in vb.net

    It seems you have quite a lot more textboxes and labels than you initially told me. The modified project I posted should fix most if not all of your problems. Let me know here if you need more help. The MSDN Q+A pages are restrictive for posting code...

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    57

    Re: how to print in vb.net

    thanks a lot it work perfectly you are the best

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width