{"id":5743,"date":"2007-07-12T16:41:00","date_gmt":"2007-07-12T16:41:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2007\/07\/12\/using-the-printform-component-in-visual-basic-matt-gertz\/"},"modified":"2024-07-05T14:43:17","modified_gmt":"2024-07-05T21:43:17","slug":"using-the-printform-component-in-visual-basic-matt-gertz","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/using-the-printform-component-in-visual-basic-matt-gertz\/","title":{"rendered":"Using the PrintForm component in Visual Basic (Matt Gertz)"},"content":{"rendered":"<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">(This is the fourth and final part of the Paint-by-Numbers series)<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Late last year, some clever guys on our Visual Basic team released the PrintForm component on the web.<span>&nbsp; <\/span>The idea behind the PrintForm component was to make printing and previewing your form very easy in .NET.<span>&nbsp; <\/span>I\u2019m going to leverage this component to enable printing of the Paint-by-Number puzzles created in the application I\u2019ve been building in this series.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">First of all, I need to download the component, which is available <\/font><a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?familyid=286111b0-6965-46cc-bf6f-c5ee63b1f98c&amp;displaylang=en\"><font face=\"Calibri\" size=\"3\">here<\/font><\/a><font face=\"Calibri\" size=\"3\"> on the Microsoft site \u2013 it\u2019s about 500KB.<span>&nbsp; <\/span>Once downloaded, I\u2019ll install it (you can even do this while Visual Studio is open), and now I can add the PrintForm control in the component to my toolbox.<span>&nbsp; <\/span>With my form selected, I right-click on the \u201cPrinting\u201d category in the toolbox and select \u201cChoose Items\u2026\u201d<span>&nbsp; <\/span>In the resulting dialog, I\u2019ll check the box next to \u201cPrint Form\u201d on the .NET tab, and that\u2019ll do it once I press &#8220;OK.&#8221;<span>&nbsp; <\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\"><span>Now, <\/span>I\u2019ll drag an instance of the PrintForm control to the component tray and rename it to be \u201cVBPBNPrintForm.\u201d We don\u2019t need to change any properties on it otherwise, so on the form, I\u2019ll drop down the File menu item and double-click on \u201cPrint Preview\u201d to generate its event handler for that command \u2013 now I can start coding!<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">The PrintForm command really just has one important verb \u2013 Print.<span>&nbsp; <\/span>It can take several options, but overall it\u2019s just a print job to either a printer, a file, or a preview window.<span>&nbsp; <\/span>Which of those it does depends on the value you assign to its PrintAction property.<span>&nbsp; <\/span>In this case, I want to just preview the document, and so I\u2019ll need to set it to \u201cPrintToPreview.\u201d<span>&nbsp; <\/span>The code looks like this:<\/font><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Private<\/span> <span>Sub<\/span> PrintPreviewToolStripMenuItem_Click(<span>ByVal<\/span> sender <span>As<\/span> System.Object, <span>ByVal<\/span> e <span>As<\/span> System.EventArgs) _<\/span><\/p>\n<p class=\"MsoNormal\"><span>Handles<\/span><span> PrintPreviewToolStripMenuItem.Click<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrintAction = Printing.PrintAction.PrintToPreview<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.Print(<span>Me<\/span>, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Sub<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Note that I\u2019ve used an overload of Print which tells the control which form to print (which is my main form in this case), and also indicates that I just want to print the client area and not the window borders and so on.<span>&nbsp; <\/span>However, if you run this, you\u2019ll notice that the menu strip is printing in the preview!<span>&nbsp; <\/span>This is because it\u2019s in the client area.<span>&nbsp; <\/span>Print doesn\u2019t use document technology \u2013 it will in fact print anything visible in the client area, which works to our benefit when printing a puzzle solution (View\/Show Solution is checked) or just the puzzle (View\/Show Solution is unchecked), but not in this case.<span>&nbsp; <\/span>So, for the duration of the printing job, I\u2019ll simply hide the Menu, and I\u2019ll wrap that in a try\/catch\/finally so that the menu will always get turned back on even if an exception is thrown from printing.<span>&nbsp; <\/span>(This is hackery, plain and simple, but the benefits are enough to outweigh any sense of guilt I might feel about that.)<span>&nbsp; <\/span>Now the code looks like this:<\/font><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Private<\/span> <span>Sub<\/span> PrintPreviewToolStripMenuItem_Click(<span>ByVal<\/span> sender <span>As<\/span> System.Object, <span>ByVal<\/span> e <span>As<\/span> System.EventArgs) _<\/span><\/p>\n<p class=\"MsoNormal\"><span>Handles<\/span><span> PrintPreviewToolStripMenuItem.Click<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Try<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNMenuStrip.Visible = <span>False<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrintAction = Printing.PrintAction.PrintToPreview<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.Print(<span>Me<\/span>, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Catch<\/span> ex <span>As<\/span> Exception<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Finally<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNMenuStrip.Visible = <span>True<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Try<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Sub<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">and that will work nicely.<span>&nbsp; <\/span>You can zoom in and out, resize the display, and even print to the default printer from the dialog that pops up.<span>&nbsp; <\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Now, for the actual Print command from the file menu, I could write very similar code (the one change is underlined below):<\/font><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Private<\/span> <span>Sub<\/span> PrintToolStripMenuItem_Click(<span>ByVal<\/span> sender <span>As<\/span> System.Object, <span>ByVal<\/span> e <span>As<\/span> System.EventArgs) <span>Handles<\/span> PrintPreviewToolStripMenuItem.Click<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Try<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrinterSettings = <span>Me<\/span>.VBPBNPrintDialog.PrinterSettings<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNMenuStrip.Visible = <span>False<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrintAction = Printing.PrintAction.<b><u>PrintToPrinter<\/u><\/b><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.Print(<span>Me<\/span>, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Catch<\/span> ex <span>As<\/span> Exception<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span>Finally<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNMenuStrip.Visible = <span>True<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Try<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Sub<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">But that would only allow us to print to the default printer, and I want to be a little more sophisticated.<span>&nbsp; <\/span>I\u2019ll drag out a PrintDialog control (which I\u2019ll rename VBPBNPrintDialog) and use it to capture settings before doing the actual printing.<span>&nbsp; <\/span>The PrintForm control uses the same PrinterSettings object that the PrintDialog does, which makes things very easy \u2013 I just assign one to the other before doing the actual printing.<span>&nbsp; <\/span>Then, I check to see if we\u2019re printing to a file or to a printer.<span>&nbsp; <\/span>If printing to a printer, then I have everything I need; if a file, I\u2019ll need to pop up a \u201csave file\u201d dialog to get the filename from the user first.<span>&nbsp; <\/span>The code now looks like this:<\/font><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Private<\/span> <span>Sub<\/span> PrintToolStripMenuItem_Click(<span>ByVal<\/span> sender <span>As<\/span> System.Object, <span>ByVal<\/span> e <span>As<\/span> System.EventArgs) _<\/span><\/p>\n<p class=\"MsoNormal\"><span>Handles<\/span><span> PrintToolStripMenuItem.Click<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8216; Bring up the print dialog:<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Dim<\/span> res <span>As<\/span> DialogResult = <span>Me<\/span>.VBPBNPrintDialog.ShowDialog<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>If<\/span> res = Windows.Forms.DialogResult.OK <span>Then<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Try<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8216; Copy the settings from the dialog<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrinterSettings = <span>Me<\/span>.VBPBNPrintDialog.PrinterSettings<\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8216; Turn off the menu strip<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNMenuStrip.Visible = <span>False<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>If<\/span> <span>Me<\/span>.VBPBNPrintForm.PrinterSettings.PrintToFile = <span>True<\/span> <span>Then<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8216; The user wants to print to a file, so bring up a &#8220;Save&#8221; dialog and get the file name<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNSaveFileDialog.Title = <span>My<\/span>.Resources.TITLE_SavePrintFile<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNSaveFileDialog.Filter = <span>My<\/span>.Resources.FILT_PrintFilter<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>If<\/span> <span>Me<\/span>.VBPBNSaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK <span>Then<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span>&#8216; Go ahead &amp; print to the file<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrintFileName = <span>Me<\/span>.VBPBNSaveFileDialog.FileName<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrintAction = Printing.PrintAction.PrintToFile<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.Print(<span>Me<\/span>, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>If<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Else<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8216; Print to whatever printer was selected<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrintAction = Printing.PrintAction.PrintToPrinter<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.Print(<span>Me<\/span>, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>If<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Catch<\/span> ex <span>As<\/span> Exception<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Finally<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&#8216; Make sure that the menu turns back on!<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNMenuStrip.Visible = <span>True<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Try<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>If<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Sub<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">and we\u2019re done with the Print code.<span>&nbsp; (The resource FILT_PrintFilter is similar to the other filter I used for saving files in the previous post, but aimed at .EPS files instead of .VBPBN files, and TITLE_SavePrintFile is just a title for the window.) <\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\"><span><\/span>But, hold on \u2013 the preview can also use the PrinterSettings object.<span>&nbsp; <\/span>For example, if the user chose \u201cPrint,\u201d clicked &#8220;Apply&#8221; after changing some settings, but then chose Cancel, the settings would persist for the next call to Print, so we\u2019d expect those to apply to the preview as well (otherwise, it wouldn\u2019t be much of a preview, since there&#8217;s no other way to do settings in a preview). So, we\u2019ll add one more line to the Print Preview handler (underlined):<\/font><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Private<\/span> <span>Sub<\/span> PrintPreviewToolStripMenuItem_Click(<span>ByVal<\/span> sender <span>As<\/span> System.Object, <span>ByVal<\/span> e <span>As<\/span> System.EventArgs) <span>Handles<\/span> PrintPreviewToolStripMenuItem.Click<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Try<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><b><u><span>Me<\/span>.VBPBNPrintForm.PrinterSettings = <span>Me<\/span>.VBPBNPrintDialog.PrinterSettings<\/u><\/b><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNMenuStrip.Visible = <span>False<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.PrintAction = Printing.PrintAction.PrintToPreview<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNPrintForm.Print(<span>Me<\/span>, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Catch<\/span> ex <span>As<\/span> Exception<\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Finally<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Me<\/span>.VBPBNMenuStrip.Visible = <span>True<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Try<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Sub<\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">And that\u2019s it!<span>&nbsp; <\/span><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\"><span><\/span>PrintForm is a great alternative to fiddling about with print documents if you don\u2019t need to do a lot of reformatting for the printout you generate.<span>&nbsp; <\/span>(In this case, all I had to do was hide a menu strip temporarily, which is much easier that defining a print document , etc.) <\/font><font size=\"3\"><font face=\"Calibri\">The final application is attached to this post.<span>&nbsp; <\/span>I hope you\u2019ve enjoyed this series \u2013 it was a lot of fun to write, and already I\u2019m thinking about the improvements that could be made, such as arbitrarily-sized grids, select\/copy\/paste of arbitrary bitmaps into and within the puzzle, creating a \u201cpuzzle viewer\u201d to solve the puzzle on the PC rather than on paper, adding authoring information to the saved puzzle, supporting the red\/black Paint-by-Number puzzles that have been so popular recently, and so on \u2013 it\u2019s hard to stop!<span>&nbsp; <\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><span><\/span>I\u2019ll be around for another week or so to answer any comments on these posts, but after that I\u2019ll be on a month-long vacation (ahhhhhh\u2026. <\/font><span><span>J<\/span><\/span><font face=\"Calibri\">), after which I\u2019ll be digging out of e-mail for days, so I\u2019m going to be silent for a bit.<span>&nbsp; <\/span>I hope everyone enjoys their summer (or winter, depending on where you are), and I\u2019ll probably post again at the end of August.<span>&nbsp; <\/span>I hope to come up with a lot of blog application ideas as my family and I drive around the east coast of the U.S.<span>&nbsp; <\/span>\u2018Til then\u2026<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">&#8211;Matt&#8211;*<\/font><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/03\/83\/66\/12\/VB%20Paint-by-Numbers.zip\">VB Paint-by-Numbers.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>(This is the fourth and final part of the Paint-by-Numbers series) Late last year, some clever guys on our Visual Basic team released the PrintForm component on the web.&nbsp; The idea behind the PrintForm component was to make printing and previewing your form very easy in .NET.&nbsp; I\u2019m going to leverage this component to enable [&hellip;]<\/p>\n","protected":false},"author":258,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[22,195],"tags":[101,165],"class_list":["post-5743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matt-gertz","category-visual-basic","tag-matt-gertz","tag-vb2005"],"acf":[],"blog_post_summary":"<p>(This is the fourth and final part of the Paint-by-Numbers series) Late last year, some clever guys on our Visual Basic team released the PrintForm component on the web.&nbsp; The idea behind the PrintForm component was to make printing and previewing your form very easy in .NET.&nbsp; I\u2019m going to leverage this component to enable [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/5743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/258"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=5743"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/5743\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=5743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=5743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=5743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}