Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the contents of a TextBox

How do I print the contents of a TextBox in metro apps? I have read this quickstart guide on MSDN and many online tutorials, but they are very complicated and do not work with TextBox controls, only RichTextBox controls.

How do we print from a TextBox control in a metro app? Is it even possible? How?

like image 837
jay_t55 Avatar asked Mar 22 '13 06:03

jay_t55


People also ask

How do I print just the text box in Word?

Print a Selected Portion of TextHighlight the text you want to print. Select File > Print. Select the Page drop-down arrow and choose Print Selection. Select the Printer drop-down arrow, choose your printer, then select Print.

How do I view a text box?

On the View menu, select Properties Window. Find TextBox1 in the Properties window drop-down box and change the Name property of the text box to displayText. Drag a Button control to the document and change the following properties. Now you can write the code that will run when the button is clicked.

Does a text box show up when you print?

When printing a document, you are able to see color changes from where a text box overlaps on another area that might have a background color or shape with a color. All text boxes are set to have no fill, shape outline or shape effects.


2 Answers

UPDATE 1

I have created a helper class which simplifies printing text box content. You can add helper class via NuGet. If you want to enhance my existing helper class, fork on GitHub


Here I am giving you the modified print sample from MSDN. I have put textbox you can write anything and that will be printed. Please note I have not done sample which prints textbox text exactly same as it is i.e formatting (bold, italic, underline, colors). I have set hard-coded print format. You can make your own format.

Stack Overflow has character limit in answer and my code is too long so posting CodePaste.net links.

XAML : http://codepaste.net/9nf261

CS : http://codepaste.net/q3hsm3

Please note that I have used some images so put images in "Images" folder

like image 122
Farhan Ghumra Avatar answered Sep 23 '22 00:09

Farhan Ghumra


I just created a small winforms-application with a textbox (textBox1) and a button (button1). The code-behind looks like:

public partial class Form1 : Form
{
    public Form1()
    {
           InitializeComponent();
    }

    private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawString(this.textBox1.Text, this.textBox1.Font, Brushes.Black, 10, 25);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        PrintDocument printDocument = new PrintDocument();
        printDocument.PrintPage += PrintDocumentOnPrintPage;
        printDocument.Print();
    }
}

On a click on the button the printing will be done.

like image 39
Tomtom Avatar answered Sep 23 '22 00:09

Tomtom