Home > Enterprise >  How to Get File then Print from a Textbox?
How to Get File then Print from a Textbox?

Time:01-06

I'm programming for a friend to help them print all files in a folder (after filtering them). I've gotten to the point where I am able to select a folder, filter the files according to extension, and list them in a ListBox. I'm thinking that I'm going to have to Loop 1) Get "File1 Name", 2) Print File1, and 3) Go to the next item, Until there are no more files. So I'm testing it out on just a regular textbox where I input the location of file "C:\Users\akapp\OneDrive\Documents\AAppleton Midterm.pdf." Is there a way to do GetFiles and Print after using a button?

This is the Print Dialogue I'm using:

    Private Sub BtnPrint_Click(sender As Object, e As RoutedEventArgs) Handles btnPrint.Click
        Dim printDoc As New PrintDocument()
        Dim printDlg As New PrintDialog With {
            .Document = printDoc,
            .PrinterSettings = printDoc.PrinterSettings,
            .AllowSomePages = True
        }

        If printDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            printDoc.PrinterSettings = printDlg.PrinterSettings
            printDoc.Print()
        End If
    End Sub

It works fine, but just prints a blank page.

I've tried printDoc.DocumentName = txtTest.Text but that does not work... ;-;

Any help is very appreciated. ^u^

Also I am a very new programmer. Like, started this past week with this project.

CodePudding user response:

Thank you to @jmcilhinney to start me in the right direction of Process.Start!

My final code (for printing files from their names in my listbox) is this!

    Dim psI As Process Start Info
    Try
        Dim printDlg As PrintDialog
        printDlg = New PrintDialog()

        If printDlg.ShowDialog = True Then
            For i = 0 To lstbxFiles.Items.Count - 1
            'psI.Arguments = """" & cmbxPrinters.Text & """"
            psI = New ProcessStartInfo(lstbxFiles.Items(i).ToString()) With 
              {
              .Arguments = """" & printDlg.PrintQueue.FullName & """",
              .UseShellExecute = True,
              .Verb = "PrintTo",
              .CreateNoWindow = True,
              .WindowStyle = ProcessWindowStyle.Hidden
              }
            System.Threading.Thread.Sleep(3000)
            Process.Start(psI)
            Next
        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

Hopefully this may help other people!

  • Related