Print in WPF

I have a sub below to print in WPF. When I call it, it won't print. However, when I put the code under button 1, it prints. Not sure why.



Public Sub Printnow()
        Dim printDialog As New PrintDialog()
        Dim query = New ManagementObjectSearcher("SELECT *FROM Win32_Printer")
        Dim printers = query.Get
        For Each printer As ManagementObject In printers   'loop to get Printer name
            If printer("name").ToString() = "HP 2000" Then
                'printer.InvokeMethod("SetDefaultPrinter", New Object() {"HP 2000"})
                printer.InvokeMethod("SetDefaultPrinter", New Object() {"HP 2000"})
                Exit For
            End If
        Next
        printDialog.PrintVisual(Me.GroupBox4, "Printing Header")

    End Sub
VBdotnet2005Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CodeCruiserConnect With a Mentor Commented:
I think tabs are only created/initialized when they get focus for the first time so that may be your issue.
0
 
VBdotnet2005Author Commented:
I forgot to add. My labels are on in Tab #2 and in Groupbox.
0
 
Bob LearnedCommented:
There is not enough context to understand how you called it when it wasn't in a button click event handler.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.