print in c# the very somple way

F

freddy

I am new to c# and I made a form with different controls like textbox,
labals, groupboxes, radio buttons and so on... I would like to print out the
text of the textbox control and not the contol itself. How would I go about
printing the groupbox control and the readio buttons? Also how can I print
from a mdi form?

I have done the printform vb powerpack and that prints all the controls -
that is not what I want.
 
J

Jeff Louie

Freddy. The .NET way is to use a reporting tool. I am use to using a RAD
database that prints out the content of a form without printing the
controls as
you suggest, but this is not the .NET way.

You can write your own print engine and extend the existing controls so
that
they know how to print themselves, even across page breaks. I would do
it for
you, but I am busy building an airplane at the moment :)

Regards,
Jeff
 
P

prem

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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top