Reputation: 22094
How to make printout using C#. At the moment I would like to make printouts of text contents only. That is something similar to Notepad, but the print actions would be executed when a Form Button is clicked.
Upvotes: 0
Views: 733
Reputation: 244732
If you just want to be able to print a short, simple text string, try something like this code:
private void printButton_Click(Object sender, EventArgs e)
{
PrintDocument doc = New PrintDocument();
doc.PrintPage += new PrintPageEventHandler(printPage);
doc.Print();
}
private void printPage(Object sender, PrintPageEventArgs e)
{
string printText = myTextbox.Text;
Font printFont = myTextbox.Font;
e.Graphics.DrawString(printText, printFont, Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
}
The documentation for the PrintDocument
class contains a more comprehensive sample that can be easily modified to print the text from your textbox instead from a file on disk.
Both sets of code will send the print job to your default printer, but you can modify them to show a print dialog box and allow the user to choose which printer to use, among other parameters.
Upvotes: 2