Question About Printing listbox contents? Plez read!

ghostpally

Member
Joined
Jan 19, 2008
Messages
23
Programming Experience
Beginner
Hi all,
I am making a program for my job at a restaurant i have the program all done but one major part.:D what the program does is allow my boss to mange the seats and table orders. and be able to edit and add to the custmors orders. Well that done. The only thing i need to do is be able to print the receipt. All the info for that party info is in a listbox. I want my boss to be able to print that receipt. I would like him to be able to select the printer if need be and print off everything in the listbox. If you need more info just ask.
 
From Printing section in Toolbox add a PrintDialog and a PrintDocument. In the Document property of the PrintDialog select the PrintDocument instance, this will ensure that selections made by user in the dialog is transferred to the PrintDocument (what printer etc). For your "print" button add this code:
VB.NET:
If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    Me.PrintDocument1.Print()
End If
Doubleclick the PrintDocument instance in Designer to generate the event handler for PrintPage event. For most basic needs you only need this code:
VB.NET:
Dim x As Integer = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
Dim lineheight As Single = Me.ListBox1.Font.GetHeight(e.Graphics)
For Each item As String In Me.ListBox1.Items
    e.Graphics.DrawString(item, Me.ListBox1.Font, Brushes.Black, x, y)
    y += lineheight
Next
This simple setup requires that the item width fit a single line within MarginBounds of page and that the number of items fit one page, else you have to make more calculations. To add more fanciness like borders and images you can use any drawing method that the Graphics instance provides. You can also specify a different Font and draw headers and such.
 
That work great but...

That work great but when i go and select the printer no matter what one i pick it alwas go to the defalte printer.
 
That is because you didn't do what I said:
In the Document property of the PrintDialog select the PrintDocument instance, this will ensure that selections made by user in the dialog is transferred to the PrintDocument (what printer etc).
 
Back
Top