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
'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
ASKER
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
ASKER
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.
TRUSTED BY
diag.ShowDialog()
MsgBox(diag.PrinterSetting