Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Storing and retreiving printer selection in My.Settings

$
0
0
I am new to vb.net from Excel VBA and trying to get to grips with printing, so sorry if this is an obvious question. I have tried searching on the internet but can't seem to find an answer.

I have on my form an option for the user to chose a printer using a printdialog and then I want to store the printer settings in my.settings:

Private Sub btnChangePrinter_Click(sender As Object, e As EventArgs) Handles btnChangePrinter.Click
        'change printer
        dlgPrintDialog.PrinterSettings.DefaultPageSettings.Margins.Left = 0
        dlgPrintDialog.PrinterSettings.DefaultPageSettings.Margins.Right = 0
        dlgPrintDialog.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0
        dlgPrintDialog.PrinterSettings.DefaultPageSettings.Margins.Top = 0
        dlgPrintDialog.ShowDialog()
        My.Settings.ActivePrinterSettings = dlgPrintDialog.PrinterSettings
        My.Settings.Save()
        txtPrinterName.Text = My.Settings.ActivePrinterSettings.PrinterName
    End Sub



when the user later comes to print from the application I want to retrieve these printer settings so the user does not have see the printdialog box again. I have tried the following code:

Private Sub btnReprintSelectedOrder_Click(sender As Object, e As EventArgs) Handles btnReprintSelectedOrder.Click
        'reprint selected order details
        'show only the order rows with the same order number as selected row in the datagridview
        Dim ordernumber As String
        
        If dgvSales.Rows.Count = 0 Then Exit Sub 'trap error where there are no sales rows
        ordernumber = dgvSales.Item("OrderNumber", dgvSales.CurrentRow.Index).Value.ToString
        showadminsales("SELECT * FROM Sales WHERE OrderNumber = '" & ordernumber & "'")
        'send the ebook order details to the printer

        dlgPrintDialog.PrinterSettings.PrinterName = My.Settings.ActivePrinterSettings.PrinterName
        orderprintdocument = New PrintDocument
        dlgPrintDialog.Document = orderprintdocument
        orderprintdocument.Print()
        showadminsales("SELECT * FROM Sales")
    End Sub



However this code does not change the printer, it just prints to the computers default printer. Stepping through the code for btnReprintSelectedOrder_Click shows me that it sets the dlgprinterdialog.printersettings.printername to my.settings.activeprintersettings.printername ok, but as soon as I set orderprintdocument = New PrintDocument the printername for orderprintdocument returns to the default printer for the computer.

Is is something I doing wrong in terms of storing / retrieving printer settings using my.settings?? Or is there another reason as to why I can't print to the selected printer??

Thanks

Martin

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>