none
Never comes back from simple print routine, what am I missing? RRS feed

  • Question

  • I am using VS2008 C# .NET 3.5 forms application. I want to print a label on the printer and this works but after stepping out of the EndPrint method control never returns to the application. I must be missing something I hope simple for somebody who does printing to see.

    internal void			PrintCustomerLabel ()
    {	PrintDocument	LabelDocument = null;
    	try
    	{	LabelDocument = new PrintDocument();
    		LabelDocument.PrinterSettings.PrinterName = "HP DeskJet 895Cse";	
    		LabelDocument.BeginPrint	+= new PrintEventHandler (LabelDocument_BeginPrint);
    		LabelDocument.PrintPage		+= new PrintPageEventHandler (LabelDocument_PrintPage);
    		LabelDocument.EndPrint		+= new PrintEventHandler (LabelDocument_EndPrint);
    		LabelDocument.Print();
    	}
    	catch (Exception OOPS)
    	{	MTGStatic.StartCatch(OOPS);
    	}
    }	
    private void			LabelDocument_PrintPage	 (object sender, PrintPageEventArgs e)
    {	Font			LabelFont	=	null;
    	StringBuilder	LabelString =	null;
    	try
    	{	LabelFont = new System.Drawing.Font	(	 "Arial"
    												,24.0f
    												,System.Drawing.GraphicsUnit.Point); 
    		LabelString = new StringBuilder();
    		LabelString.Append("Customer Name\n");
    		LabelString.Append("Customer Login Name\n");
    		LabelString.Append("Customer Password\n");
    		e.Graphics.DrawString		(	 LabelString.ToString() 
    										,LabelFont 
    										,Brushes.Black
    										,100
    										,100);	
    		e.HasMorePages = false;
    	}	
    	catch (Exception OOPS)
    	{	OOPS.Data.Add("LabelString.ToString()", LabelString.ToString());
    		MTGStatic.StartCatch (OOPS);
    		return;
    	}	
    }		
    private void			LabelDocument_EndPrint (object sender, PrintEventArgs e)
    {	
    }	
    private void			LabelDocument_BeginPrint (object sender, PrintEventArgs e)
    {	
    }	

    Tom
    Wednesday, February 3, 2010 7:44 PM

Answers

  • I put the above code in a new Forms project and it worked fine. I converted it to use WPF components that those worked fine. Perhaps these two do not get along together very well. I can use the WPF controls for this print work.

    Thanks to all who gave this some thought.


    Tom
    • Marked as answer by eryang Friday, February 5, 2010 1:53 AM
    Wednesday, February 3, 2010 9:51 PM

All replies

  • Maybe there is an exception so HasMorePage is always true.
    Move following line to after catch, also remove the return from catch block, Please:

    e.HasMorePages = false;

    Please help us improve this community forum for visitors by marking the replies as answers if they help and unmarking them if they provide no help.
    Thanks.
    Wednesday, February 3, 2010 8:20 PM
  • I did not see an exception as I stepped through the code. The StartCatch method dumps data to a log and there was no data there. None the less I tried that with the same results. I am suspecting some interaction between some of the WPF components and the Forms components in this application might be causeing the problem.

    Thanks for taking the time to look at this.


    Tom
    Wednesday, February 3, 2010 8:55 PM
  • I put the above code in a new Forms project and it worked fine. I converted it to use WPF components that those worked fine. Perhaps these two do not get along together very well. I can use the WPF controls for this print work.

    Thanks to all who gave this some thought.


    Tom
    • Marked as answer by eryang Friday, February 5, 2010 1:53 AM
    Wednesday, February 3, 2010 9:51 PM