using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Drawing.Printing; namespace Neo.Afx.ActiveX.Printing { [Guid("33DE363F-9A74-4B0D-9E3F-6B22A3C7417F")] public interface BarcodeLabelPrinterInterface { void PrintLabels(string BarcodeData, string PrintedBy, short Copies, string PrinterPath, int LabelHeight, int LabelWidth, int LeftMargin, bool Preview); } [ProgId("Neo.Afx.ActiveX.BarcodeLabelPrinterControl")] [Guid("2F3F1A1C-C828-4D93-BEC9-BB5524DFED27")] [ClassInterface(ClassInterfaceType.None)] public partial class BarcodeLabelPrinterControl : UserControl, BarcodeLabelPrinterInterface { public BarcodeLabelPrinterControl() { InitializeComponent(); } private void PrintButton_Click(object sender, EventArgs e) { Print(); } #region public PrintLabels method public void PrintLabels(string BarcodeData, string PrintedBy, short Copies, string PrinterPath, int LabelHeight, int LabelWidth, int LeftMargin, bool Preview) { BarcodeDataTextBox.Text = BarcodeData; PrintedByTextBox.Text = PrintedBy; LeftMarginTextBox.Text = LeftMargin.ToString(); if (Copies.ToString() == "999") { var userInput = Microsoft.VisualBasic.Interaction.InputBox("Please enter the number of labels you would like to print.", "Print labels", "6", 400, 400).ToString(); try { if (userInput.Length == 0) { return; } var numberCopies = Int32.Parse(userInput); CopiesTextBox.Text = numberCopies.ToString(); } catch { MessageBox.Show("Invalid number entered."); return; } } else { CopiesTextBox.Text = Copies.ToString(); } this.LabelHeightTextBox.Text = LabelHeight.ToString(); this.LabelWidthTextBox.Text = LabelWidth.ToString(); this.previewCheckBox.Checked = Preview; this.PrinterPathTextBox.Text = GetPrinter(PrinterPath); if (this.PrinterPathTextBox.Text.Length == 0) { MessageBox.Show("No barcode printer (" + PrinterPath + ") found."); } else { try { Print(); MessageBox.Show(this.CopiesTextBox.Text + " copies printed to " + PrinterPathTextBox.Text); } catch (Exception e) { MessageBox.Show(e.Message); } } } #endregion #region private Print() Method private void Print() { BarcodeLabelPrinter BarcodeLabelPrinter1 = new BarcodeLabelPrinter(this.BarcodeDataTextBox.Text, this.PrintedByTextBox.Text, Convert.ToInt16(this.CopiesTextBox.Text), this.PrinterPathTextBox.Text, Convert.ToInt16(this.LabelHeightTextBox.Text), Convert.ToInt16(this.LabelWidthTextBox.Text), Convert.ToInt16(LeftMarginTextBox.Text), this.previewCheckBox.Checked); BarcodeLabelPrinter1.Print(); } #endregion #region getPrinter private string GetPrinter(string printerPath) { var printerName = ""; foreach (String printer in PrinterSettings.InstalledPrinters) { if (printer.ToLower().Contains(printerPath.ToLower())) { printerName = printer; } } return printerName; } #endregion } }