C# substitute of VB 6 Printer Class

439 views Asked by At

I need a substitute of VB6 Printer Class:

Microsoft.VisualBasic.PowerPacks.Printing.Compatibility

I checked System.Drawing.Printing in C# but not getting direct alternative of Printer object in VB6.

Set p = Printer 

Any link would be more helpful.

1

There are 1 answers

0
Brian M Stafford On

Here is code to illustrate how to use Microsoft.VisualBasic.PowerPacks.Printing.Compatibility. This is nearly identical to VB6 code I have had in production for many years. It should be noted that DeviceName is read-only even in VB6 so you need to iterate the list of printers looking for a match.

using Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6;

private void Test()
{
   //get the default printer
   Printer p = new Printer();
   MessageBox.Show(p.DeviceName);

   //print to non-default printer
   p = FindPrinter("some printer name");

   if (p != null)
   {
      p.CurrentX = 500;
      p.CurrentY = 500;
      p.Print("Some text out to specified printer");
      p.EndDoc();
   }
}

private Printer FindPrinter(string DeviceName)
{
   var Printers = new PrinterCollection();

   foreach (Printer p in Printers)
   {
      if (p.DeviceName == DeviceName) return p;
   }

   return null;
}