Avatar of Kin Fat SZE
Kin Fat SZEFlag for Hong Kong

asked on 

How to choose the printer when printing by printer object by Visual Basic .net?

Dear Sir/Madam

I have a question about choosing printer from Visual Basic .net
I am using following code to print a simply document that's fine. BUT, how to choose the difference printer to print document out? It is alway print out the document from default printer.
Is anyone willing to help me?
Thanks a lot
Dim result As System.Windows.Forms.DialogResult
        result = PrintDialog1.ShowDialog()
 
        If result.ToString.ToUpper.Equals("OK") Then
            Try
                Dim printer As New Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.Printer
 
                printer.FontSize = 20
                printer.CurrentX = 100
                printer.CurrentY = 100
                printer.Print("ok")
                printer.EndDoc()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

Open in new window

.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Kin Fat SZE
Avatar of VBRocks
VBRocks
Flag of United States of America image

       Dim diag As New PrintDialog()
        diag.ShowDialog()

        MsgBox(diag.PrinterSettings.PrinterName)

Avatar of VBRocks
VBRocks
Flag of United States of America image

Here's a Microsoft link with more information about printing in VB.Net

     http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

Avatar of Howard Cantrell
Here is a sample app. on printers
'FORM 1
 
Imports System.Drawing.Printing
Public Class frmSelectPrinter
    Inherits System.Windows.Forms.Form
 
#Region " Windows Form Designer generated code "
 
    Public Sub New()
        MyBase.New()
 
        'This call is required by the Windows Form Designer.
        InitializeComponent()
 
        'Add any initialization after the InitializeComponent() call
 
    End Sub
 
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(20, 12)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(244, 95)
        Me.ListBox1.TabIndex = 0
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(28, 116)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(236, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "TextBox1"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(12, 144)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 50)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Current Printer Default"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(101, 158)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(71, 23)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "List printers"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(184, 144)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(75, 50)
        Me.Button3.TabIndex = 4
        Me.Button3.Text = "Select default printer"
        '
        'frmSelectPrinter
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 206)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.ListBox1)
        Me.Name = "frmSelectPrinter"
        Me.Text = "Form2"
        Me.ResumeLayout(False)
        Me.PerformLayout()
 
    End Sub
 
#End Region
    'this will get current systems default printer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New Printing.PageSettings
        TextBox1.Text = a.PrinterSettings.PrinterName()
    End Sub
 
    'this will get a list of every printer setup on the current machine
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim b As PrinterSettings.StringCollection = PrinterSettings.InstalledPrinters
        Dim X As Integer = 0
        For X = 0 To b.Count - 1
            ListBox1.Items.Add(b.Item(X))
        Next
    End Sub
 
    'this will make the selected printer the default printer 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim a As New Printing.PageSettings
        a.PrinterSettings.PrinterName = ListBox1.SelectedItem
        SetDefaultPrinter(ListBox1.SelectedItem)
        MessageBox.Show("New Default Printer: " & a.PrinterSettings.PrinterName)
    End Sub
#Region " Initialize Default Printer "
    Public Function InitializeDefaultPrinter() As String
        ' Set the specified printer to the default printer for this program. Return
        ' true if the printer was found.
        Dim objprinter As PageSettings = New PageSettings
        Return objprinter.PrinterSettings.PrinterName
    End Function
    Public Function SetDefaultPrinter(ByVal strPrinterName As String) As Boolean
        Dim wshnetwork As Object
        Try
            wshnetwork = CreateObject("WScript.Network")
            wshnetwork.SetDefaultPrinter(strPrinterName)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
#End Region
End Class

Open in new window

Avatar of Kin Fat SZE
Kin Fat SZE
Flag of Hong Kong image

ASKER

Sorry, I fixed it and also thanks all of you to help me
        Dim i As Integer
        Dim result As System.Windows.Forms.DialogResult
        result = PrintDialog1.ShowDialog()
 
        If result.ToString.ToUpper.Equals("OK") Then
            Try
                Dim printer As New Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.Printer
 
                Dim printerCollect As New Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.PrinterCollection
 
                For i = 0 To printerCollect.Count - 1
                    If printerCollect.Item(i).DeviceName.ToString().Equals(PrintDialog1.PrinterSettings.PrinterName.ToString) Then
                        printer = printerCollect.Item(i)
                        Exit For
                    End If
                Next
 
                printer.FontSize = 20
                printer.CurrentX = 100
                printer.CurrentY = 100
                printer.Print("ok")
                printer.EndDoc()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Kin Fat SZE
Kin Fat SZE
Flag of Hong Kong image

ASKER

I try to print out "OK" because I just want to test a printer to pint it out and
also (second task) try to choose which of printer to do it.
Many thanks VBRocks
I will try to your code which you provided to me. But also, I fixed it right now.
:>
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo