Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print textbox contents in C#

Tags:

c#

winforms

I want to print the contents of a simple TextBox. After I click the print button, PrintDialog is shown.

I found a lot of info but they all use RichTextBoxes. Is there an easy way to do something like this?

like image 718
mafap Avatar asked Dec 06 '25 03:12

mafap


1 Answers

This print contents of textbox named textbox1

    PrintDocument document = new PrintDocument();
    PrintDialog dialog = new PrintDialog();
    public Form1()
    {
        InitializeComponent();
        document.PrintPage += new PrintPageEventHandler(document_PrintPage);
    }

    void document_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawString(textBox1.Text, new Font("Arial", 20, FontStyle.Regular), Brushes.Black, 20, 20);
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        dialog.Document = document;
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            document.Print();
        }
    }

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!