3

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?

mafap
  • 371
  • 3
  • 18
  • 40
  • 1
    This should be very simple to do, looking at that link, all they do is get the text from the richtextbox so I don't see where textboxes are different – Sayse Aug 31 '13 at 12:02
  • try replacing the `Richtextbox` with your `textbox` – Rohit Aug 31 '13 at 12:50
  • @Kyle yes but how 'OnBeginPrint' and 'OnPrintPage' are created? – mafap Aug 31 '13 at 12:58
  • 1
    They are the events of PrintDocument...you need to add printDocument from Toolbox to your form ...and in the properties tab you can click on events and access these two events....or alternatively you add printdocument from toolbox to your form and in your form load event add `printDocument1.BeginPrint += new PrintEventHandler(OnBeginPrint); printDocument1.PrintPage += new PrintPageEventHandler(OnPrintPage); ` – Rohit Aug 31 '13 at 13:10

2 Answers2

9

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();
        }
    }
  • 1
    Your solution is similar to [this one](http://stackoverflow.com/a/15616680/2208810) and they both worked :) – mafap Sep 01 '13 at 19:44
4

Take a look at this: http://answers.yahoo.com/question/index?qid=20081230163003AA4xOaT, and this: How to print the contents of a TextBox Also, there is a tutorial on printing in C#: http://www.dreamincode.net/forums/topic/44330-printing-in-c%23/

If, after this, you still cannot print TextBox content from some reason, you can always create a new RichTextBox object and assign your TextBox's Text to its text. Then proceed with printing using the RichTextBox.

Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80