将打印机设置保存到文件,稍后再读。 [英] Save printer settings to a file, and read it later.

查看:133
本文介绍了将打印机设置保存到文件,稍后再读。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的阅读,首先,请原谅我糟糕的英语。

我正在编写一个简单的代码来习惯VB.net,并且需要在发生这种情况时解决一些问题。



我需要从一台PC打印到网络中的多台打印机,这不是问题,但是因为会有大量的打印件和快,我需要将打印机设置保存到一台打印机的printer1.txt文件,另一台打印机的printer2.txt。当再次单击该按钮时,请阅读设置文件并再次启动PrintDialog。



这里是我的简单代码,让您了解



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

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

推荐答案

我认为你最好使用 My.Settings [ ^ ]比使用文本文件存储此信息。



右键单击解决方案资源管理器中的项目。



选择属性。这将打开项目的属性页面。



选择设置选项卡。此区域允许您设置各种设置,如果程序关闭并重新启动,可以保留这些设置。



为Printer1添加属性,使类型 System.Drawing.Printing.PrinterSettings [ ^ ]。



对打印机2和打印机3执行相同操作。



然后在您的代码中,在您之前在每次单击事件中执行If PrintDialog1.ShowDialog位,检查相应的设置是否包含数据。如果是,则跳过显示对话框。如果没有,则显示找到它的对话框并设置设置,以便下次跳过对话框。您可以使用

My.Settings访问代码中的设置。< SettingNameThatYouProvided>



希望这有帮助。
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.


这篇关于将打印机设置保存到文件,稍后再读。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆