| The RichTextBox control in C# is a versatile element that allows users to display and edit formatted text, including different fonts, colors, sizes, and styles. It provides features like text highlighting, bullet points, indentation, and hyperlinks, making it suitable for applications that require rich text editing capabilities, such as word processors, text editors, and code editors. Below is a .NET approach to Simplified Printing in which,we are going to use a RichTextBox to cache all of our text for printing in printDocument. // Form Load
private void Form1_Load(object sender,System.EventArgs e)
{
// Write to richTextBox
richTextBox1.Text = " "
DateTime.Now.Month "/" DateTime.Now.Day "/"
DateTime.Now.Year " ";
richTextBox1.AppendText("This is a greatly simplified Print "
"Document Method ");
richTextBox1.AppendText("We can write text to a richTextBox,"
"or use Append Text," " " "or Concatenate a String,and "
"write that textBox. The " " " "richTextBox does not "
"even have to be visible. " " " "Because we use a "
"richTextBox it's physical dimensions are " " "
"irrelevant. We can place it anywhere on our form,and set the "
" " "Visible Property to false. ");
richTextBox1.AppendText("This is the document we will print. The "
"rich TextBox serves " " " "as a Cache for our Report,"
"or any other text we wish to print. ");
richTextBox1.AppendText("I have also included Print Setup "
"and Print Preview. ");
}
// Print Event
private void miPrint_Click(object sender,System.EventArgs e)
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
// OnBeginPrint
private void OnBeginPrint(object sender,System.Drawing.Printing.PrintEventArgs e)
{
char[] param = {' '};
if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
{
lines = richTextBox1.SelectedText.Split(param);
}
else
{
lines = richTextBox1.Text.Split(param);
}
int i = 0;
char[] trimParam = {' '};
foreach (string s in lines)
{
lines[i ] = s.TrimEnd(trimParam);
}
}
// OnPrintPage
private void OnPrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
Brush brush = new SolidBrush(richTextBox1.ForeColor);
while (linesPrinted < lines.Length)
{
e.Graphics.DrawString (lines[linesPrinted ],richTextBox1.Font,brush,x,y);
y = 15;
if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
else
}
e.HasMorePages = false;
}
}
}
// Page Setup
private void miSetup_Click(object sender,System.EventArgs e)
{
// Call Dialog Box
pageSetupDialog1.ShowDialog();
}
// Print Preview
private void miPreview_Click(object sender,System.EventArgs e)
{
// Call Dialog Box
printPreviewDialog1.ShowDialog();
}
|