Print control code

Iam creating a order form using vb 2005 express beta, Iam having trouble writing correct code to print button that will print form contents.
Iam only able to print a blank page.
Please any help would br great
thanks, Bruni
[235 byte] By [Bruni] at [2007-12-16]
# 1
What are you doing currently?
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
PrintDocument1.Print()
I havn't found any thing else that works along with this
iam semi new to vb 2005 only been taking video lessons for 2 months
Please excuse my lack of knowledge
thanks for any help u can give me
Bruni
Bruni at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Sorry for not replying. I seem not to be being notified of posts to subscribed threads. I'll look into that. Calling Print on a PrintDocument object raises the PrintPage event. You do the actual printing in the handler for this event. Go here and download the VB samples. They are for 2003 but you can convert to 2005 no problem. There will be at least one printing example included. You can also go here for 2005-specific samples, but I'm not sure what's there.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
I tryed to download samples but it kept tryed to install it over and over and i couldn't open it up.any other ideas.
thanks Bruni
Bruni at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
I've not known anyone to have problems with those samples. What goes wrong when you try to use them? Also, you can simply use the help documentation for the PrintDocument class to get info and an example.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
i need to take a short break from the learning my head is about to explode.
thanks for the help i'll reply again in a couple of days
Bruni at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8
thanks for the help but iam so confused at were to place the print code to make it work and i can't locate a tutorial that will teach me anything about printing that i can undestand at the level of training that iam at and i want to learn how to do it and move on to more advanced vb.
Thanks Bruni
Bruni at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9
Like I said, you do the actual printing in the PrintPage event handler for your PrintDocument object. If you've added the PrintDocument in the designer, just double-click it and you'll get an empty event handler. The event handler gets passed a PrintPageEventArgs object that you use to control the printing. It's most important properties are Graphics and HasMorePages. Graphics is a System.Drawing.Graphics object which you use to actually draw string, lines, etc. on the printing surface. HasMorePages is a Boolean value that indicates whether there are more pages to print. If HasMorePages is set to True then the PrintPage event will be raised again. If HasMorePages is set to False then the printing process is finished.
Here is very simple example that prints 5 pages with the page number on each.

Private m_PrintPageIndex As Integer = 1
Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
Me.m_PrintPageIndex = 1
Me.PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim [string] As String = "This is page " & Me.m_PrintPageIndex.ToString() 'String to draw.
Dim font As New Font("Times New Roman", 12) 'Defines the text format of the string.
Dim brush As Brush = Brushes.Black 'Determines the colour and texture of the drawn text.
Dim x As Single = 20 'X coordinate of the upper-left corner of the drawn text.
Dim y As Single = 20 'y coordinate of the upper-left corner of the drawn text.
'Draw the string on the printing surface.
e.Graphics.DrawString([string], font, brush, x, y)
'Increment the page index.
Me.m_PrintPageIndex += 1
If Me.m_PrintPageIndex > 5 Then
'All five pages have been printed.
e.HasMorePages = False
'Reset the page index in case this was a print preview.
Me.m_PrintPageIndex = 1
Else
'There are more pages to print.
e.HasMorePages = True
End If
End Sub


If you want to test printing without wasting lots of paper, add a PrintPreviewDialog to your form. You can set the Document property of the PrintPreviewDialog to the PrintDocument you already have. Then, instead of calling PrintDocument.Print you call PrintPreviewDialog.ShowDialog. Notice that I set the page index back to 1 after setting HasMorePages to False. This is because if you show a PrintPreviewDialog first and then print from it, the whole printing process happens twice, so you must make sure the index is reset after the first run so it is correct for the start of the second run.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10
Thank You Soooooooo Much!!!! i feel that there is light at the end of some tunnel, i havn't found the tunnel yet but you made me see that there is one out there somewhere.
Now what i was able to do was print 5 pages that said this is page 1 -5 in upper left corner.
What code do i need to add to make data on form print?

thanks Bruni

Bruni at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 11
The line that calls e.Graphics.DrawString is the one that actually draws the string on the printing surface. You need to put code in its place that will draw your data instead. You should read a little about the Graphics class and its DrawString method, as well as the other methods available. The first argument to DrawString is the actual string you want to print. The other arguments determine how it will look and where it will be located.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 12
I have spent hours searching for an answer, and I finally found it.
I read your instructions. But which part of e.Graphics.DrawString am I to change?
My form is called Form1 and the print Document is PrintDocument1.

Thanks
plz reply ASAP

Rashin at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 13
Rashin wrote:
I have spent hours searching for an answer, and I finally found it.
I read your instructions. But which part of e.Graphics.DrawString am I to change?
My form is called Form1 and the print Document is PrintDocument1.

Thanks
plz reply ASAP

I don't know how much plainer I can make it. I've included a comment where I declared each of the variables that gets passed to DrawString to describe what each of them does. If you don't understand that already then I don't know what I can say to make it any clearer.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 14

look at the powerpack

http://msdn2.microsoft.com/en-us/vbasic/aa701261.aspx

This may help in printing a form

spotty at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...