我制作了一个在启动时运行的程序,要求用户选择他们的默认打印机。该代码在 ComboBox 中显示所有已安装的打印机,并且提交按钮会将在 ComboBox 中选择的打印机设置为该用户的默认打印机。
有没有办法只能显示包含特定文本的打印机名称?
例如:
ABC
printer1ABC
network1 abc
printer2 def
network2def
所以它只会显示printer1ABC
和network1 abc
?这是我到目前为止的代码(一个 ComboBox 和一个 Button)
Imports System.Drawing.Printing
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strInstalledPrinters As String
Dim prntDoc As New PrintDocument
'check if there is installed printer
If PrinterSettings.InstalledPrinters.Count = 0 Then
MsgBox("No printer installed")
Exit Sub
End If
'display installed printer into combobox list item
For Each strInstalledPrinters In PrinterSettings.InstalledPrinters
ComboBox1.Items.Add(strInstalledPrinters)
Next strInstalledPrinters
'Display current default printer on combobox texts
ComboBox1.Text = prntDoc.PrinterSettings.PrinterName
Button1.Text = "Set Default Printer"
End Sub
End Class
将打印机设置为默认值的功能:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", ComboBox1.Text))
MsgBox("You have changed your default printer")
Me.Close()
End Sub
谢谢你的帮助