How to opet text file via open file dialog and print text?

to open a text file using an OpenFileDialog and show it in a MessageBox:

private void button1_Click(object sender, System.EventArgs e)
   if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      System.IO.StreamReader sr = new 

Edited 5 Years Ago by agugglez: n/a

Add a printDocument1 component to your form. You also will of course have a button or menu tool strip item for 'print'.
you code the 'print' button to call the printDocument1.Print() method.

private void printButton_Click(object sender, System.EventArgs e)
   // Note: You could also code a print preview if you add a printPreviewDialog   
   // component to your form

   // printPreviewDialog1.Document = printDocument1;
   // printPreviewDialog1.ShowDialog();

Code for the printDocument1_PrintPage() method sets up the printed pages.

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
   // The PrintPage event is fired once for each page via 'callback'.
   // Set up output to print in here.

As to the setting up of output to print:
I am familiar with the DrawString method of the Graphics object of the PrintPageEventArgs argument only. DrawString() is overloaded. Syntax:

GraphicsObject.DrawString(StringToPrint, Font, Brush, Xcoordinate, Ycoordinate);

You have to supply the arguments; what to print, what font to use, color, and where on the graphics memory page to print it.
You first declare your print and coordinate variables.
Then set up a line to print.
Print the line.
Then increment the Ycoordinate if another line follows.
The above three steps go inside a loop if there are multiple lines.

As an example, the following code would print 'someThing' on a page.

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  // Declarations at top of method.
  Font printFont = new Font("Arial", 12);
  float lineHeightFloat = printFont.GetHeight;
  float horizontalPrintLocationFloat = e.MarginBounds.Left;
  float verticalPrintLocationFloat = e.MarginBounds.Top;

  // Set up a line to print.
  string printLineString = "someThing";
  // Print a line.
  e.Graphics.DrawString(printLineString, printFont,  Brushes.Black, horizontalPrintLocationFloat, verticalPrintLocationFloat);
  // Increment the Y position for the next line.
  verticalPrintLocationFloat += lineHeightFloat;

If you had multiple list such as a comboBox list to print you would use a loop such as;

// Loop through the list
  for (int indexInteger = 0; indexInteger < comboBox1.Items.Count; indexInteger++)
    e.Graphics.DrawString(comboBox.Items[indexInteger].ToString(), printFont, Brushes.Black, horizontalPrintLocationFloat, verticalPrintLocationFloat);
    verticalPrintLocationFloat += lineHeightFloat;

Edited 5 Years Ago by Chellam2: n/a

Not 100% sure, but I think you can also just create a report file and send the text to the report and print it that way?

This article has been dead for over six months. Start a new discussion instead.