Results 1 to 4 of 4

Thread: [Resolved] Print margins problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    3

    Resolved [Resolved] Print margins problem

    Hi there

    I am having issues with getting print preview to work reliably with C#.
    I have viewed the help files/docs on msdn and scoured many other articles on the web, its quite tricky to piece together how printing is meant to work as there is a lack of documentation on the issue however i have struggled on to finally get my little app working to some degree only to find what i think is a possible bug! (but probly bad code on my behalf)

    I am making a windows forms app that has a rich text box, some buttons to print preview/page settings/print and also a print preview.
    The problem arises when i open the page settings dialog, every time i open this the settings for the margins are changed to be made smaller, this even happens if myPageSetupDialog.AllowMargins = false.
    I am fairly sure that this is not down to my code as i do not touch the margins in anyway except when the printPage event is fired and i print a line with the MarginBounds settings.
    I dont think the values of the margins should be changing at all.

    Code:
    namespace SimplePrinting
    {
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.ComponentModel.Container components = null;
    		private PrintDocument myPrintDoc = new PrintDocument();
    		private System.Windows.Forms.RichTextBox richTextBox1;
    		private PrintPreviewDialog myPrintPreviewDialog = new PrintPreviewDialog();
    		private PrintDialog myPrintDialog = new PrintDialog();
    		private PageSettings myPageSettings = new PageSettings();
    		private System.Windows.Forms.PrintPreviewControl printPreviewControl1;
    		private System.Windows.Forms.Button btnUpdate;
    		private System.Windows.Forms.ComboBox comboPrinters;
    		PrinterSettings myPrinterSettings = new PrinterSettings();
    		
    		public Form1()
    		{
    			myPrintDoc.PrintPage += new PrintPageEventHandler(myPrintDoc_PrintPage);
    			
    
    			MenuItem fileMenuItem = new MenuItem("&File");
    			MenuItem filePageSetupMenuItem = new MenuItem("Page Set&up...", 
    				new EventHandler(filePageSetupMenuItem_Click));
    			MenuItem filePrintPreviewMenuItem = new MenuItem("Print Pre&view", 
    				new EventHandler(filePrintPreviewMenuItem_Click));
    			MenuItem filePrintMenuItem = new MenuItem("&Print...", 
    				new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP);
    
    			fileMenuItem.MenuItems.Add(filePageSetupMenuItem);
    			fileMenuItem.MenuItems.Add(filePrintPreviewMenuItem);
    			fileMenuItem.MenuItems.Add(filePrintMenuItem);
    
    			this.Menu = new MainMenu();
    			this.Menu.MenuItems.Add(fileMenuItem);
    			InitializeComponent();
    
    		}
    
    		
    		private void Form1_Load(object sender, System.EventArgs e) {
    			richTextBox1.LoadFile("C:\\Temp\\test.rtf");
    			fillComboPrinters();
    		}
    //- Event Handlers for the menu --------------
    		private void filePageSetupMenuItem_Click(Object sender, EventArgs e){
    			//Page Setup event handler
    			PageSetupDialog myPageSetupDialog = new PageSetupDialog();
    			myPageSetupDialog.PageSettings = myPageSettings;
    			myPageSetupDialog.PrinterSettings = myPrinterSettings;
    			myPageSetupDialog.AllowOrientation = true;
    			myPageSetupDialog.AllowMargins = true;
    			myPageSetupDialog.AllowPrinter = true;
    			
    
    			if(myPageSetupDialog.ShowDialog() == DialogResult.OK){
    				myPrintDoc.DefaultPageSettings = myPageSetupDialog.PageSettings;
    				myPageSettings = myPageSetupDialog.PageSettings;
    				printPreviewControl1.Document = myPrintDoc;
    			}
    		}
    
    		private void filePrintPreviewMenuItem_Click(Object sender, EventArgs e){
    			//Print Preview event handler
    			myPrintDoc.DefaultPageSettings = myPageSettings;
    			myPrintPreviewDialog.Document = myPrintDoc;
    			myPrintPreviewDialog.ShowDialog();
    
    		}
    
    		private void filePrintMenuItem_Click(Object sender, EventArgs e){
    			//Print event handler
    			//myPrintDoc.Print() will fire off the newly defined
    			//myPrintDoc_PrintPage() event where we make the content to send.
    			//but first we set the documents print settings
    			//then we set the printdialogs document
    			//otherwise it doesnt know what the fsck is going on!
    			//then finally show the print dialog box
    			myPrintDoc.DefaultPageSettings = myPageSettings;
    			myPrintDialog.Document = myPrintDoc;
    			if(myPrintDialog.ShowDialog() == DialogResult.OK){
    				myPrintDoc.Print();
    			}
    		}
    //- End of menu Event Handlers --------------------
    
    
    		private void myPrintDoc_PrintPage(Object sender, PrintPageEventArgs e){
    			//PrintPage event handler, here we acctually print stuff
    			//using the PrintPageEventArgs object.
    			//its .Graphics property represents a print page and we
    			//can use its methods to print stuff!
    			
    			Brush myBrush = new SolidBrush(richTextBox1.ForeColor);
    			//set the margins
    			int leftMargin = e.MarginBounds.Left;
    			int topMargin = e.MarginBounds.Top;
    			int y = 0;
    			
    			foreach(String line in richTextBox1.Lines){
    				e.Graphics.DrawString (line, richTextBox1.Font, myBrush, leftMargin, topMargin + y);
    				y +=15;
    				
    				if(y >= e.MarginBounds.Bottom){
    					e.HasMorePages = true;
    					y = 0;
    					return;
    				}
    				else{
    					e.HasMorePages = false;
    				}
    			}
    		}
    
    		private void btnUpdate_Click(object sender, System.EventArgs e) {
    			printPreviewControl1.Document = null;
    			myPrintDoc.DefaultPageSettings = myPageSettings;
    			printPreviewControl1.Document = myPrintDoc;
    		}
    
    		private void comboInstalledPrinters_SelectionChanged(object sender, System.EventArgs e) {
    			System.Console.WriteLine("Combo changed!");
    			if(comboPrinters.SelectedIndex != -1){
    				myPrintDoc.PrinterSettings.PrinterName = comboPrinters.Text;
    				myPrinterSettings.PrinterName = comboPrinters.Text;
    				myPageSettings.PrinterSettings.PrinterName = comboPrinters.Text;
    			}
    		}
    
    		private void fillComboPrinters(){
    			string strCurPrinter = null;
    			for(int i = 0;  i < PrinterSettings.InstalledPrinters.Count -1; i++){
    				strCurPrinter = PrinterSettings.InstalledPrinters[i];
    				comboPrinters.Items.Add(strCurPrinter);
    				System.Console.WriteLine(strCurPrinter);
    			}
    		}
    	}
    }
    this is really just a tarted up version of http://www.ondotnet.com/pub/a/dotnet.../printing.html which i would have thought would work, but i still get this funny anomaly.


    any help would be much appreciated

    thanks alot
    Eddy Parris
    Last edited by Hack; Mar 28th, 2006 at 09:17 AM. Reason: Added green "resolved" checkmark
    Edward Parris : http://www.communecation.net/
    Social Engineer : http://socialengineer.communecation.net
    Debian Linux : http://www.debian.org : How Gnu are You?

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    3

    Re: Print margins problem

    Solved.. alomost:

    http://support.microsoft.com/?id=814355

    this is also not fixed in vs2005
    bit of a joke if you ask me... having to change a system-wide setting in order to print properly!
    Edward Parris : http://www.communecation.net/
    Social Engineer : http://socialengineer.communecation.net
    Debian Linux : http://www.debian.org : How Gnu are You?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [Resolved] Print margins problem

    They had "more important" things to do than fix existing bugs when creating VS 2005/.NET 2.0. Let's hope this and a few other things get fixed when they release service packs later in the year.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    3

    Re: [Resolved] Print margins problem

    Yeah, its a bit of a joke really, i might have found another bug in the print review control. if it is a bug and not my code that will be my 2nd, one bug for every week ive been using .net.
    The printing stuff is particularly annoying as i work for a printing company and being able to print properly is essential for our move to .net
    Edward Parris : http://www.communecation.net/
    Social Engineer : http://socialengineer.communecation.net
    Debian Linux : http://www.debian.org : How Gnu are You?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width