JaredH20
JaredH20

Reputation: 368

How to print a Windows Form

I have made an Address Book WinForm in C# and would like to know how to print it as a text file, how would I go about doing this?

I have displayed everything in a DataGridView, I would ideally just like to print the information in the table as text.

Upvotes: 1

Views: 6982

Answers (4)

Glory Raj
Glory Raj

Reputation: 17701

you can try like this...

[STAThread]
  static void Main() 
  {
    Application.Run(new PrintPreviewDialog());
  }

  private void btnOpenFile_Click(object sender, System.EventArgs e)
  {
    openFileDialog.InitialDirectory = @"c:\";
    openFileDialog.Filter = "Text files (*.txt)|*.txt|" +
            "All files (*.*)|*.*";
    openFileDialog.FilterIndex = 1;              //  1 based index

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
      StreamReader reader = new StreamReader(openFileDialog.FileName);
      try
      {
        strFileName = openFileDialog.FileName;
        txtFile.Text = reader.ReadToEnd();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
        return;
      }
      finally
      {
        reader.Close();
      }
    }
  }

  private void btnSaveFile_Click(object sender, System.EventArgs e)
  {
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.InitialDirectory = @"c:\";
    sfd.Filter = "Text files (*.txt)|*.txt|" +
            "All files (*.*)|*.*";
    sfd.FilterIndex = 1;              //  1 based index

    if (strFileName != null)
      sfd.FileName = strFileName;
    else
      sfd.FileName = "*.txt";

    if (sfd.ShowDialog() == DialogResult.OK)
    {
      StreamWriter writer = new StreamWriter(strFileName,false);
      try
      {
        strFileName = sfd.FileName;
        writer.Write(txtFile.Text);
      }
      catch(Exception ex)
      {
        MessageBox.Show(ex.Message);
        return;
      }
      finally
      {
        writer.Close();
      }
    }
  }

//here you can print form as text file by clicking on the button..

  private void btnPageSetup_Click(object sender, System.EventArgs e)
  {
    PageSetupDialog psd = new PageSetupDialog();
    psd.Document = printDocument;
    psd.ShowDialog();
  }

  private void btnPrint_Click(object sender, System.EventArgs e)
  {
    PrintDialog pdlg = new PrintDialog();
    pdlg.Document = printDocument;

    if (pdlg.ShowDialog() == DialogResult.OK)
    {
      try
      {
        printDocument.Print();
      }
      catch(Exception ex)
      {
        MessageBox.Show("Print error: " + ex.Message);
      }
    }
  }

  private void btnPrintPreview_Click(object sender, System.EventArgs e)
  {
    PrintPreviewDialog ppdlg = new PrintPreviewDialog();
    ppdlg.Document = printDocument;
    ppdlg.ShowDialog();
  }

  private void pdPrintPage(object sender, PrintPageEventArgs e)
  {
    float linesPerPage = 0;
    float verticalOffset = 0;
    float leftMargin = e.MarginBounds.Left;
    float topMargin = e.MarginBounds.Top;
    int linesPrinted = 0;
    String strLine = null;

    linesPerPage = e.MarginBounds.Height / currentFont.GetHeight(e.Graphics);

    while (linesPrinted < linesPerPage &&
        ((strLine = stringReader.ReadLine())!= null ))
    {
      verticalOffset = topMargin + (linesPrinted * currentFont.GetHeight(e.Graphics));
      e.Graphics.DrawString(strLine, currentFont, Brushes.Black, leftMargin, verticalOffset);
      linesPrinted++;
    }

    if (strLine != null)
      e.HasMorePages = true;
    else
      e.HasMorePages = false;

  }

  private void pdBeginPrint(object sender, PrintEventArgs e)
  {
    stringReader = new StringReader(txtFile.Text);
    currentFont = txtFile.Font;
  }

  private void pdEndPrint(object sender, PrintEventArgs e)
  {
    stringReader.Close();
    MessageBox.Show("Done printing.");
  }
}

Upvotes: 1

Fischermaen
Fischermaen

Reputation: 12468

The simpliest way is to create a text file and write the values in it. Like this:

var textFile = File.CreateText("Address.txt");
textFile.WriteLine("Name: Fischermaen");
textFile.Close();

Upvotes: 0

Davide Piras
Davide Piras

Reputation: 44605

you should give more details on what you want to do.

how do you intend to print the form as text file? How do you convert the graphics like labels, buttons and other controls into text?

what you ask is possible and you can control every aspect of the printed content in both ways graphic rendering or text only, have a look here as starting point:

Windows Forms Print Support

Upvotes: 0

Alex KeySmith
Alex KeySmith

Reputation: 17111

Preview and Print from Your Windows Forms App with the .NET Printing Namespace http://msdn.microsoft.com/en-us/magazine/cc188767.aspx

It's a little old (2003) but still looks relevent.

Upvotes: 0

Related Questions