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

Recommended Answers

All 7 Replies

google streamread and streamwrite

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 
         System.IO.StreamReader(openFileDialog1.FileName);
      MessageBox.Show(sr.ReadToEnd());
      sr.Close();
   }
}

Thanks and how to print that tekst file?

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)
{
   printDocument1.Print();
   // 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;
  }

ok.thanks :)

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.