Answer the question
In order to leave comments, you need to log in
How to print text from TextBox in C#?
And how to implement at the same time a preview before printing?
Actually there is a text in TextBox. Created a menuStrip created a Print button. Added printDialog, printDocument and printPreviewDialog. Then I spied on the following code for implementing printing:
private string strToPrint = String.Empty;
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument f = new System.Drawing.Printing.PrintDocument();
PrintDialog theDialog = new PrintDialog();
System.Drawing.Printing.PrintDocument thePrintDocument = new System.Drawing.Printing.PrintDocument();
theDialog.Document = thePrintDocument;
theDialog.ShowDialog();
foreach (Control curControl in this.Controls)
{
this.strToPrint += curControl.Text + " ";
}
//the for each loop above is probably useless for you, just take it out and only append to "strToPrint" the text you want to print from wherever it is.
thePrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
thePrintDocument.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int linesPerPage = 0;
int charsOnPage = 0;
e.Graphics.MeasureString(this.strToPrint, this.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charsOnPage, out linesPerPage);
e.Graphics.DrawString(this.strToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);
this.strToPrint = this.strToPrint.Substring(charsOnPage);
e.HasMorePages = (this.strToPrint.Length > 0);
}
The code is completely incomprehensible to me, as a result, an empty landscape sheet is printed.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question