Windows Develop Bookmark and Share   
 index > Windows Forms General > Printing
 

Printing

I am trying to do printing in C# for the first time.

I am working the example at http://msdn.microsoft.com/en-us/library/cfkddyc2(VS.80).aspx

Here is the rub.

It is not recognizing printDocument1

I am guessing that printDocument1 is an instantition of a provided class, but I odn't know what it is and it si not in the example.


this.printDocument1.PrintPage += new
   System.Drawing.Printing.PrintPageEventHandler
   (this.printDocument1_PrintPage);
this.button1.Click += new System.EventHandler(this.button1_Click);

private void button1_Click(object sender, System.EventArgs e) { PrintDialog printDialog1 = new PrintDialog(); printDialog1.Document = printDocument1; DialogResult result = printDialog1.ShowDialog(); if (result == DialogResult.OK) { printDocument1.Print(); } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.FillRectangle(Brushes.Red, new Rectangle(500, 500, 500, 500)); }


  • Edited byJimGuyer Monday, September 21, 2009 9:42 PMedit
  •  
JimGuyer  Monday, September 21, 2009 9:40 PM
Start a new Windows Forms Application and replace the code on Form1 with this:

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      PrintDocument pd = new PrintDocument();
      pd.PrintPage += pd_PrintPage;
      PrintPreviewDialog ppd = new PrintPreviewDialog();
      ppd.Document = pd;
      ppd.ShowDialog();
    }
    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
      e.Graphics.FillRectangle(Brushes.Red, 100, 100, 100, 100);
    }
  }
}


Do you get a red rectangle on the PrintPreview?
  • Marked As Answer byJimGuyer Tuesday, September 22, 2009 3:19 PM
  •  
JohnWein  Tuesday, September 22, 2009 2:04 PM
Jim:

Without having to open Visual Studio, you can guess the class name is PrintDocument. Visual Studio names controls and components by adding a number to its base class.

If you open a form and then go to the toolbox, you should see a PrintDocument toolbox item. It should be at least in the "All Windows Forms" tab.
MCP
webJose  Monday, September 21, 2009 9:44 PM
Find the PrintDocument component in the Toolbox and double-click on it. Your squigglies should go away and the program should compile and run. Unless you need to use PrintDialogEx.
JohnWein  Monday, September 21, 2009 9:50 PM
Alright, I got that figured out, and it does run a page through the printer, but it is not printing my rectangle on the page.

        public static void DropDownItem_Print_Click(object ParmObject, EventArgs ParmEventArgs) {
            PrintDocument printDocument1 = new PrintDocument();

            PrintDialog printDialog1 = new PrintDialog();
            printDialog1.Document = printDocument1;
            DialogResult result = printDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                printDocument1.Print();
            }
        }
        private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e) {
            e.Graphics.FillRectangle(Brushes.Red,
              new Rectangle(500, 500, 500, 500));
        }
JimGuyer  Monday, September 21, 2009 9:52 PM
Since it sendsa page through the printer, it's executing the PrintPage event, so there must be something wrong with the FillRectangle statement. Change the rectangle to100,100,100,100.
JohnWein  Monday, September 21, 2009 10:10 PM
You are creating a *new* instance of the PrintDocument class in your Print_Click event handler. Don't. That new instance won't have the PrintPage event handler set. Just delete the line.

Hans Passant.
nobugz  Tuesday, September 22, 2009 12:52 AM
That's how I had it before, but I get

Error1The name 'printDocument1' does not exist in the current contextC:\Documents and Settings\jim.guyer\My Documents\Visual Studio 2008\Projects\NucorPrd\NucorPrd\BarCut.cs27737NucorPrd
JimGuyer  Tuesday, September 22, 2009 1:40 PM
Start a new Windows Forms Application and replace the code on Form1 with this:

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      PrintDocument pd = new PrintDocument();
      pd.PrintPage += pd_PrintPage;
      PrintPreviewDialog ppd = new PrintPreviewDialog();
      ppd.Document = pd;
      ppd.ShowDialog();
    }
    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
      e.Graphics.FillRectangle(Brushes.Red, 100, 100, 100, 100);
    }
  }
}


Do you get a red rectangle on the PrintPreview?
  • Marked As Answer byJimGuyer Tuesday, September 22, 2009 3:19 PM
  •  
JohnWein  Tuesday, September 22, 2009 2:04 PM
Excellent! That worked great. I had failed to associate a PrintDoucument,Printpage Event
JimGuyer  Tuesday, September 22, 2009 3:20 PM

You can use google to search for other answers

Custom Search

More Threads

• User verification
• how to export rtb text into image
• Monitoring Changes on a Tab Page
• How To Detect Shift+Arrow Key
• UIPAB Question
• C# Issue
• could not find file error
• Partial underline within a rich text box
• Form hides to back at hiding dialog box
• How to initiate a new object when ...