Printing more then 1 page

Joined
Oct 20, 2006
Messages
21
Programming Experience
Beginner
Ok I have a dynamically allocated “For loop” that will print something to the screen and increment and continue to print until all the stuff in the list box is printed. I know how many items I can fit on a page (about 13). What I need to do is have the loop go through 13 times then set hasmorepages = true and continue printing the items in the list box until 13 is reached then create another new page and so on. So far all I managed to get was an infinite number of pages with the same content or 1 page. The thing is I think I have to jump out of the loop and then set more pages then jump back in where I left off. The following is sort of what I tried.


VB.NET:
       Count = lstPrintStudents.Items.Count
        pCount = 0
        For N = 0 To Count - 1

            If lcounter <= 13 Then


            pCount = (Count - N)
            if (N <> 0) And ((N Mod (13)) = 0) And (pCount >= 1) Then
               e.HasMorePages = True
            Else
             e.HasMorePages = False
            End If

'''''Print Stuff Here''''''
Next N
 
One variable and pure logic ;)
VB.NET:
    Dim startindex As Integer = 0
 
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
    Handles PrintDocument1.PrintPage
        Dim count As Integer
        Dim max As Integer = lstPrintStudents.Items.Count -1
 
        For count = startindex To max
            'print item(count)
            If count - startindex = 13 Then Exit For
        Next
 
        If count < max Then e.HasMorePages = True
    End Sub
 
I’m not sure what I am doing wrong. I tried your code but I think I used it incorrectly because I just got a ton of pages with the same stuff. So essentially what I have is a list box that can have any number of items in it. When the user clicks print I get a count of the number of items and using a for loop print them all. Obviously if there are a lot they cannot all fit on one page. I have them formatted so they print the same size. Consequently I know that 13 can fit on a page. I can figure out how to get just 13 on a page and stop but I need the loop to pick up where it left off to print the rest. The following is my code for the print page.
VB.NET:
  [FONT=&quot]    [COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] PrintMe_PrintPage([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.Object, [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.Drawing.Printing.PrintPageEventArgs) [COLOR=blue]Handles[/COLOR] printMe.PrintPage[/FONT]
  [COLOR=blue][FONT=&quot]Dim[/FONT][/COLOR][FONT=&quot] MyGraphicsPage [COLOR=blue]As[/COLOR] Graphics = e.Graphics[/FONT]
  [FONT=&quot]        MyGraphicsPage.PageUnit = GraphicsUnit.Pixel[/FONT]
  
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] N [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR][/FONT]
  [COLOR=blue][FONT=&quot]Dim[/FONT][/COLOR][FONT=&quot] Count [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] ID [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] FName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] LName [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] email [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] pict [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] class1 [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] section [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] semester [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] year [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] total [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] image [COLOR=blue]As[/COLOR] Image[/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] file [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] x1 [COLOR=blue]As[/COLOR] [COLOR=blue]Single[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] x2 [COLOR=blue]As[/COLOR] [COLOR=blue]Single[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] y1 [COLOR=blue]As[/COLOR] [COLOR=blue]Single[/COLOR][/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] y2 [COLOR=blue]As[/COLOR] [COLOR=blue]Single[/COLOR][/FONT]
  [FONT=&quot]        x1 = 1[/FONT]
  [FONT=&quot]x2 = 1[/FONT]
  [FONT=&quot]        y1 = 1[/FONT]
  [FONT=&quot]        y2 = 1[/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] myFont [COLOR=blue]As[/COLOR] Font[/FONT]
  [COLOR=blue][FONT=&quot]Dim[/FONT][/COLOR][FONT=&quot] textSize [COLOR=blue]As[/COLOR] SizeF[/FONT]
  [FONT=&quot]        myFont = [COLOR=blue]New[/COLOR] Font([COLOR=maroon]"arial"[/COLOR], 10)[/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] names [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR][/FONT]
  [FONT=&quot]        page += 1[/FONT]
  [FONT=&quot]        [COLOR=blue]Dim[/COLOR] counter [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR][/FONT]
  [FONT=&quot]        counter = 1[/FONT]
  
  [FONT=&quot]        Count = lstPrintStudents.Items.Count[/FONT]
  [FONT=&quot]        [COLOR=blue]For[/COLOR] N = 0 [COLOR=blue]To[/COLOR] Count - 1[/FONT]
  
  
  [FONT=&quot]            txtTemp.Text = lstPrintStudents.Items.Item(N)[/FONT]
  
  [FONT=&quot]            txtTemp.Select(0, 20)[/FONT]
  [FONT=&quot]            ID = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtID.Text = ID.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(20, 10)[/FONT]
  [FONT=&quot]            LName = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtLName.Text = LName.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(30, 10)[/FONT]
  [FONT=&quot]            FName = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtFName.Text = FName.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(40, 25)[/FONT]
  [FONT=&quot]            email = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtEmail.Text = email.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(65, 40)[/FONT]
  [FONT=&quot]            pict = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtPict.Text = pict.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(105, 10)[/FONT]
  [FONT=&quot]            class1 = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtClass.Text = class1.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(115, 5)[/FONT]
  [FONT=&quot]            section = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtSection.Text = section.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(120, 10)[/FONT]
  [FONT=&quot]            semester = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtSemester.Text = semester.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(130, 6)[/FONT]
  [FONT=&quot]            year = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtYear.Text = year.Trim[/FONT]
  
  [FONT=&quot]            txtTemp.Select(136, 2)[/FONT]
  [FONT=&quot]            total = txtTemp.SelectedText[/FONT]
  [FONT=&quot]            txtTotal.Text = total.Trim[/FONT]
  
  
  
  [FONT=&quot]            file = txtPict.Text[/FONT]
  [FONT=&quot]            image = [COLOR=blue]New[/COLOR] Bitmap(file)[/FONT]
  [FONT=&quot]MyGraphicsPage.DrawImage(image, x1, y1, 500, 500)[/FONT]
  
  [FONT=&quot]names = txtFName.Text & [COLOR=maroon]" "[/COLOR] & txtLName.Text[/FONT]
  [FONT=&quot]            textSize = MyGraphicsPage.MeasureString(names, myFont)[/FONT]
  [FONT=&quot]            MyGraphicsPage.DrawString(names, myFont, Brushes.Black, 550, (y1 + 0))[/FONT]
  [FONT=&quot]            MyGraphicsPage.DrawString(email, myFont, Brushes.Black, 550, (y1 + 100))[/FONT]
  [FONT=&quot]            MyGraphicsPage.DrawString(class1 & [COLOR=maroon]" "[/COLOR] & section & [COLOR=maroon]" "[/COLOR] & semester & [COLOR=maroon]" "[/COLOR] & year, myFont, Brushes.Black, 550, (y1 + 200))[/FONT]
  [FONT=&quot]            MyGraphicsPage.DrawString(total & [COLOR=maroon]" "[/COLOR] & cmbType.Text, myFont, Brushes.Black, 550, (y1 + 300))[/FONT]
  [FONT=&quot]            [COLOR=blue]Try[/COLOR][/FONT]
  [FONT=&quot]                [COLOR=blue]Call[/COLOR] Print(MyGraphicsPage, (x1 + 1700), (y1 + 0), 3000, 300, txtID.Text)[/FONT]
  [FONT=&quot]            [COLOR=blue]Catch[/COLOR][/FONT]
  [FONT=&quot]                MsgBox(Err.Description, MsgBoxStyle.Exclamation, Application.ProductName)[/FONT]
  [FONT=&quot]            [COLOR=blue]End[/COLOR] [COLOR=blue]Try[/COLOR][/FONT]
  
  [FONT=&quot]            y1 = y1 + 500[/FONT]
  
  [FONT=&quot]        [COLOR=blue]Next[/COLOR] N[/FONT]
  
  [FONT=&quot]    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
 
maybe it was "if count = max then hasmorepages..." and not "count < max". Anyway, that's how it goes.
 
Back
Top