Put on print Button and then write the click event for that
"m_richTextBox"
/// To print the contents of the documents.
public override void btnPrint_Click(object sender, EventArgs
e)
{
try
{
if (m_richTextBox.Text.Trim() != "")
{
m_printDialog.Document = m_printDocument;
string strText = this.m_richTextBox.Text;
m_SReader = new StringReader(strText);
if (m_printDialog.ShowDialog() == DialogResult.OK)
{
this.m_printDocument.Print();
}
base.btnPrint_Click(sender, e);
}
else
{
cusMessageBox.ShowBox("There are no records to
Print.");
}
}
catch (Exception Ex)
{
ErrorPublish(Ex);
cusMessageBox.ShowBox(Ex.Message);
}
}
/// To redirect the contents of the textbox to the window
printer service.
protected void m_printDocument_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs ev)
{
try
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
System.Drawing.Font printFont =
this.m_richTextBox.Font;
SolidBrush myBrush = new SolidBrush(Color.Black);
// Work out the number of lines per page, using the
MarginBounds.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Iterate over the string using the StringReader,
printing each line.
while (count < linesPerPage && ((line =
m_SReader.ReadLine()) != null))
{
// calculate the next line position based on
// the height of the font according to the
printing device
yPosition = topMargin + (count *
printFont.GetHeight(ev.Graphics));
// draw the next line in the rich edit control
ev.Graphics.DrawString(line, printFont, myBrush,
leftMargin, yPosition, new StringFormat());
count++;
}
// If there are more lines, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
myBrush.Dispose();
}
catch (Exception Ex)
{
ErrorPublish(Ex);
cusMessageBox.ShowBox(Ex.Message);
}
}