C# – Printing a Document

cprinting

I want to print my TextBox and here is my code:

private void MenuItemPrint()
{
        if (FileName != "")
        {
            PrintDocument document = new PrintDocument();
            document.PrinterSettings.PrintFileName = FileName;
            document.Print();
        }
}

and it doesn't work. What should I do?

Best Answer

Try This:

private void MenuItemPrint()
{
   if (!FileName.Trim().Equals(""))
   {                        
     using(PrintDocument pd = new PrintDocument())
     {
        using(PrintDialog printDialog=new PrintDialog())
        {
          if(printDialog.ShowDialog()==DialogResult.Yes)
          {
          pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);                        
          pd.Print();
          }
         }
      }
    }
 }
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
  ev.Graphics.DrawString(FileName, new Font("Arial", 10), Brushes.Black,
                       ev.MarginBounds.Left, 0, new StringFormat());
 }