Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI, Thanks for reading, first of all, excuse my bad english.
I'm making a simple code to get used to VB.net and need to solve some problems when that happens.

I need to print from one PC to multiple printers that are in a network, it's not a problem, but as there will be a lot of prints to be made and fast, I need to save a printer settings to a file like "printer1.txt" for one printer, and "printer2.txt" for another printer. and when click the button again, read the setting file and doesnt launch again the PrintDialog.

here is my simple code to make you understand

VB
Public Class Form1

    Public NPrinter As String

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        e.Graphics.DrawString(NPrinter, New Font("Verdana", 18, FontStyle.Bold), Brushes.Black, 10, 10)

        For i As Integer = 1 To 2
            e.Graphics.DrawString("Ejemplo de linea: " & i, New Font("Tahoma", 10, FontStyle.Bold), Brushes.Black, 10, 20 + (i * 30))
        Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        NPrinter = "Printer 1"
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            Dim NPrinter As String

            PrintDocument1.Print()
        End If

    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        NPrinter = "Printer 2"
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            Dim NPrinter As String
            PrintDocument1.Print()
        End If
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        NPrinter = "Printer 3"
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            Dim NPrinter As String

            PrintDocument1.Print()
        End If
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub
End Class
Posted

1 solution

I think you'd be better off using My.Settings[^] than using text files to store this information.

Right Click on your Project in the Solution Explorer.

Select Properties. This opens your project's property pages.

Select the Settings Tab. This area lets you setup various settings that can be retained if your program is closed and restarted.

Add a property for Printer1, making the type System.Drawing.Printing.PrinterSettings[^].

Do the same for Printer2 and Printer 3.

Then in your code, before you do the If PrintDialog1.ShowDialog bit in each click event, check to see if the corresponding setting has data. If it does, then skip showing the dialog. If it doesn't, show the dialog to find it and set the setting so that the next time you will skip the dialog. You can access settings in code with
My.Settings.<SettingNameThatYouProvided>

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900