11 Replies - 5790 Views - Last Post: 14 April 2013 - 04:25 PM Rate Topic: -----

#1 ak4744710   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 30-January 13

Please help how to print more than one page

Posted 14 April 2013 - 03:02 PM

Hi I am fairly new to vb.net and trying to learn the basics. I have been toying with this code for hours and looking at countless websites, but I can not figure out how to print or make more than one page.
Imports System.Drawing.Printing
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
        If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintDialog1.AllowSomePages = True
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()
        PrintDialog1.Document = PrintDocument1
        Dim result As DialogResult = PrintDialog1.ShowDialog()
        If (result = Windows.Forms.DialogResult.OK) Then
            PrintDocument1.Print()
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Static myPages As Integer
        Dim headerfont = New Font("Arial", 14, FontStyle.Bold)
        e.Graphics.DrawString("some line of text", headerfont, Brushes.Black, 100, 100)
        e.Graphics.DrawString("some line of text", headerfont, Brushes.Black, 250, 100)
        e.Graphics.DrawString("some line of text", headerfont, Brushes.Black, 400, 100)
        Dim myFont = New Font("Arial", 12, FontStyle.Regular)
        e.Graphics.DrawString(TextBox1.Text, myFont, Brushes.Black, 100, 150)
        e.Graphics.DrawString(TextBox2.Text, myFont, Brushes.Black, 250, 150)
        e.Graphics.DrawString(TextBox3.Text, myFont, Brushes.Black, 400, 150)
     
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
        If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
        End If
    End Sub
End Class

I can make it print with the 3 columns showing the 3 line of text. After this I am puzzled on how to create or print other pages though. I am just toying with printing trying to learn how to do it and researched so much but now I am hung up. If someone could please show me how to get further I would appriciate it thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Please help how to print more than one page

#2 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6622
  • View blog
  • Posts: 27,031
  • Joined: 12-December 12

Re: Please help how to print more than one page

Posted 14 April 2013 - 03:26 PM

This MSDN walkthough should be helpful. Essentially, it is the HasMorePages property that determines whether printing should continue.
Was This Post Helpful? 0
  • +
  • -

#3 ak4744710   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 30-January 13

Re: Please help how to print more than one page

Posted 14 April 2013 - 03:29 PM

View Postandrewsw, on 14 April 2013 - 05:26 PM, said:

This MSDN walkthough should be helpful. Essentially, it is the HasMorePages property that determines whether printing should continue.

Thank you I will look into that I tried it earlier but through another tut and it was not making much sense to me because when I fired it it would just keep counting but hopefully this one will be a bit more clear thanks.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6622
  • View blog
  • Posts: 27,031
  • Joined: 12-December 12

Re: Please help how to print more than one page

Posted 14 April 2013 - 03:38 PM

I dug a little deeper:

We do not send a page-break to start a new page - there is no such thing (to .NET)! What happens is, before PrintPage is completed, we set HasMorePages to True. This causes PrintPage to be called again, each time starting a new page (a fresh canvas). A static variable within PrintPage could control the number of pages to print.

Static currPage As Integer = 1

Added: I see you already have such a variable.

This post has been edited by andrewsw: 14 April 2013 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 ak4744710   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 30-January 13

Re: Please help how to print more than one page

Posted 14 April 2013 - 03:52 PM

yes I was writting it with the static varible and then putting this
 If mypages < 4 Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            mypages = 0
        End If
for the has more pages but it just seems to act as a counter and never stops. I went through the tut but I still can not see how to correct it or how to make there be more pages I appreciate your help but I think my ignorance of vb.net is getting the best of me on this.

ok I noticed that is because I might not have been using the correct number becuase when I set the integer to 4 then it seems fine but I still have no clue how to creat more pages like how to get things to print as seperate pages sort of the idea of using a page break.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6622
  • View blog
  • Posts: 27,031
  • Joined: 12-December 12

Re: Please help how to print more than one page

Posted 14 April 2013 - 04:04 PM

From that code snippet you are not increasing (incrementing) mypages, so HasMorePages is constantly true, and this is probably stalling the computer.

If you haven't studied all of the fundamentals of VB.NET then I strongly recommend that you do this now: this is essential if you wish to make any progress. There are tutorials available here at DIC.
Was This Post Helpful? 0
  • +
  • -

#7 ak4744710   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 30-January 13

Re: Please help how to print more than one page

Posted 14 April 2013 - 04:04 PM

just wanted to update again I noticed if I start new lines like this
    e.Graphics.DrawString("Name", headerfont, Brushes.Black, 100, 100)
        e.Graphics.DrawString("CourseID", headerfont, Brushes.Black, 250, 100)
        e.Graphics.DrawString("CourseName", headerfont, Brushes.Black, 400, 100)
        Dim myFont = New Font("Arial", 12, FontStyle.Regular)
        e.Graphics.DrawString(TextBox1.Text, myFont, Brushes.Black, 100, 150)
        e.Graphics.DrawString(TextBox2.Text, myFont, Brushes.Black, 250, 150)
        e.Graphics.DrawString(TextBox3.Text, myFont, Brushes.Black, 400, 150)
       'these are my new lines and I belive the next page
 e.Graphics.DrawString("yourName", headerfont, Brushes.Black, 100, 100)
        e.Graphics.DrawString(TextBox4.Text, myFont, Brushes.Black, 100, 150)

then they show up but they write over the first page.
Was This Post Helpful? 0
  • +
  • -

#8 ak4744710   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 30-January 13

Re: Please help how to print more than one page

Posted 14 April 2013 - 04:05 PM

Thank you I was searching the tuts I was looking into one on printing which has helped but yeah I do not get it all yet
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6622
  • View blog
  • Posts: 27,031
  • Joined: 12-December 12

Re: Please help how to print more than one page

Posted 14 April 2013 - 04:10 PM

FWIW Printing a file :)

Dim p = Process.Start("NOTEPAD /P somefile.txt")


View Postak4744710, on 14 April 2013 - 11:04 PM, said:

just wanted to update again I noticed if I start new lines like this
    e.Graphics.DrawString("Name", headerfont, Brushes.Black, 100, 100)
        e.Graphics.DrawString("CourseID", headerfont, Brushes.Black, 250, 100)
        e.Graphics.DrawString("CourseName", headerfont, Brushes.Black, 400, 100)
        Dim myFont = New Font("Arial", 12, FontStyle.Regular)
        e.Graphics.DrawString(TextBox1.Text, myFont, Brushes.Black, 100, 150)
        e.Graphics.DrawString(TextBox2.Text, myFont, Brushes.Black, 250, 150)
        e.Graphics.DrawString(TextBox3.Text, myFont, Brushes.Black, 400, 150)
       'these are my new lines and I belive the next page
 e.Graphics.DrawString("yourName", headerfont, Brushes.Black, 100, 100)
        e.Graphics.DrawString(TextBox4.Text, myFont, Brushes.Black, 100, 150)

then they show up but they write over the first page.

Yes, it would do. There is nothing in that code that tells it what to print on one page, and what to print on another. This is where the static variable needs to be used.

Anyway, good luck.
Was This Post Helpful? 0
  • +
  • -

#10 ak4744710   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 30-January 13

Re: Please help how to print more than one page

Posted 14 April 2013 - 04:18 PM

View Postandrewsw, on 14 April 2013 - 06:10 PM, said:

FWIW Printing a file :)/>

Dim p = Process.Start("NOTEPAD /P somefile.txt")


View Postak4744710, on 14 April 2013 - 11:04 PM, said:

just wanted to update again I noticed if I start new lines like this
    e.Graphics.DrawString("Name", headerfont, Brushes.Black, 100, 100)
        e.Graphics.DrawString("CourseID", headerfont, Brushes.Black, 250, 100)
        e.Graphics.DrawString("CourseName", headerfont, Brushes.Black, 400, 100)
        Dim myFont = New Font("Arial", 12, FontStyle.Regular)
        e.Graphics.DrawString(TextBox1.Text, myFont, Brushes.Black, 100, 150)
        e.Graphics.DrawString(TextBox2.Text, myFont, Brushes.Black, 250, 150)
        e.Graphics.DrawString(TextBox3.Text, myFont, Brushes.Black, 400, 150)
       'these are my new lines and I belive the next page
 e.Graphics.DrawString("yourName", headerfont, Brushes.Black, 100, 100)
        e.Graphics.DrawString(TextBox4.Text, myFont, Brushes.Black, 100, 150)

then they show up but they write over the first page.

Yes, it would do. There is nothing in that code that tells it what to print on one page, and what to print on another. This is where the static variable needs to be used.

Anyway, good luck.

I appreciate it I was thinking about it after your last comment and I was thinking maybe part of my problem is that I should be writing a text file and using a streamreader to read it with vb and then print I am making the mistake of trying to write it in my code I think. Thanks that really did help.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is online

  • head thrashing
  • member icon

Reputation: 6622
  • View blog
  • Posts: 27,031
  • Joined: 12-December 12

Re: Please help how to print more than one page

Posted 14 April 2013 - 04:23 PM

View Postak4744710, on 14 April 2013 - 11:18 PM, said:

I appreciate it I was thinking about it after your last comment and I was thinking maybe part of my problem is that I should be writing a text file and using a streamreader to read it with vb and then print I am making the mistake of trying to write it in my code I think. Thanks that really did help.

Yes, I would use a StreamReader. Create a text-file with several pages of text in it. This is more realistic anyway, and easier than creating the text line-by-line.

This post has been edited by andrewsw: 14 April 2013 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#12 ak4744710   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 30-January 13

Re: Please help how to print more than one page

Posted 14 April 2013 - 04:25 PM

perfect sorry it took me so long to get it it was when you mentioned notepad that it really kicked in thanks again and have a great day.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1