ThisIsABird ThisIsABird - 6 months ago 24
Vb.net Question

vb.net - PrintDialog not appearing

I'm almost done with my system. All I need to finish is the print process. Sadly, the print dialog is not appearing.

Here's the code for the printing process

Private Sub PrintReceipt()
Dim printdialog As New PrintDialog
Dim printdocument As New Printing.PrintDocument

printdialog.Document = printdocument

AddHandler printdocument.PrintPage, AddressOf printdocument_printPage

Dim result As DialogResult

If (result = DialogResult.OK) Then
printdocument.Print()
End If
End Sub


There is nothing wrong with the printdocument_printpage, which is basically just what it will be printing, yet, since printing hasn't even begun, I won't bother putting it here unless requested.

I want the said dialog to appear after the transaction has been saved. which its not doing for some reason. And here's the code for the save transaction.

Dim payment As New Payment
mydbcon = New MySqlConnection
mydbcon.ConnectionString = "server=localhost;userid=root;password=;database=sdudb"
Dim reader As MySqlDataReader

If e.PaymentSuccess = True Then
Try
mydbcon.Open()
Dim Query As String
Query = "select * from itemstored"
COMMAND = New MySqlCommand(Query, mydbcon)
reader = COMMAND.ExecuteReader()
While reader.Read
insertTranscation(reader.GetString("itemname"), reader.GetString("price"))
End While
DeleteItemStored()
PrintReceipt()
reader.Close()
reader.Dispose()
mydbcon.Close()
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
End Try
End If


Ignore the mysql here. It's not the main problem.

Answer

The dialog will not show if you don't tell it to. You've forgotten to call printdialog.ShowDialog() in your code, and you also never set result to anything before checking it.

It's a simple fix:

Dim result As DialogResult = printdialog.ShowDialog()