Simple Print Document and Change Default Printer

menguin

Geek
Pinoy Techie
Code:
using System;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
using System.Runtime.InteropServices;
namespace testprintingshit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FillListBox();
        }



        void FillListBox()
        {
            foreach (var p in PrinterSettings.InstalledPrinters)
            {
                comboBox1.Items.Add(p);
            }
        }

        private void AddNewOrderBTN_Click(object sender, EventArgs e)
        {
            txtClientName.Clear();
            cmbItemName.SelectedIndex = 0;
            cmbQuantity.SelectedIndex = 0;
            txtPrice.Text = String.Empty;

            txtClientName.Focus();
        }
        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            UpdateTotalPrice();
        }
        private void UpdateTotalPrice()
        {
            if (cmbQuantity.Text != String.Empty && txtPrice.Text != String.Empty)
            {
                decimal TotalPriceToPay = Convert.ToInt32(cmbQuantity.Text) * Convert.ToInt32(txtPrice.Text);
                lblTotaltoPayOutput.Text = TotalPriceToPay.ToString();
            }
            else
            {
                lblTotaltoPayOutput.Text = "0";
            }
        }
        private void cmbQuantity_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateTotalPrice();
        }

        private void PrintPreviewBTN_Click(object sender, EventArgs e)
        {
            TestprintPreviewDialog.Document = TestprintDocument;
            TestprintPreviewDialog.ShowDialog();
        }

        private void TestprintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawString("Client Name: " + txtClientName.Text, new Font("Arial", 12, FontStyle.Regular),Brushes.Black, new Point(25,180));
            e.Graphics.DrawString("Item Name: " + cmbItemName.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 200));
            e.Graphics.DrawString("Quantity: " + cmbQuantity.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 220));
            e.Graphics.DrawString("Price: " + txtPrice.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 240));
            e.Graphics.DrawString("Total To Pay:: " + lblTotaltoPayOutput.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(25, 260));
        }

        private void printBTN_Click(object sender, EventArgs e)
        {
            TestprintDocument.Print();
        }
    //eto ung punyetang pang palit ng printer
        public static class printer
        {
            [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool SetDefaultPrinter(string Printer);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Pname = comboBox1.SelectedItem.ToString();
            printer.SetDefaultPrinter(Pname);
        }

    }
}
 

Similar threads


Top Bottom