I have the little recipe program that lets you print the selected recipe on to a 3x5 or 4x7 card. It works but only the very first time you open up the form. Once I click on the 4x7 menu and it brings up the recipe to print, I click the print button, it prints, but if I click the print button again it prints but it cuts the recipe off and only prints like a few letters of the ingredients and that is mostly off the left side of the page.
The class is below used to print the 4x7. it basically just takes a screenshot of the form and makes it a bitmap. Is there a better way maybe?
Code:Option Explicit On Imports System Imports System.IO Imports System.Text Public Class Frm4x7 Dim img As Bitmap Private Sub Frm4x7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'loads the name of the recipe into the label LblName.Text = Trim(FrmMain.tvwMain.SelectedNode.Text) 'loads the process in the process box TxtProcessTest.Text = Trim(FrmMain.RichTextBox2.Text) ' loads all the ingredients into the list box one line at a time TxtIngredients.Text = Trim(FrmMain.RichTextBox1.Text) End Sub Private Sub PrintToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click Dim dpiX As Integer = CInt(Me.CreateGraphics().DpiX) Dim dpiY As Integer = CInt(Me.CreateGraphics().DpiY) img = New Bitmap(6 * dpiX, 10 * dpiY) '4" by 7" Dim gr As Graphics = Graphics.FromImage(img) gr.Clear(Color.White) gr.DrawString(Label1.Text, Label1.Font, New SolidBrush(Label1.ForeColor), Label1.Left, Label1.Top) 'left + top properties gr.DrawString(LblName.Text, LblName.Font, New SolidBrush(LblName.ForeColor), LblName.Left, LblName.Top) 'specify printed location Dim p As Point = TxtIngredients.Location 'specifies printed location p.Offset(1, 1) Dim s As Size = Size.Subtract(TxtIngredients.Size, New Size(5, 5)) gr.CopyFromScreen(TxtIngredients.PointToScreen(New Point(1, 1)), p, s) p = TxtProcessTest.Location 'specifies printed location p.Offset(1, 1) s = Size.Subtract(TxtProcessTest.Size, New Size(5, 5)) gr.CopyFromScreen(TxtProcessTest.PointToScreen(New Point(1, 1)), p, s) PrintDocument1.Print() End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage PrintDocument1.DefaultPageSettings.Margins.Left = 0 PrintDocument1.DefaultPageSettings.Margins.Top = 0 PrintDocument1.OriginAtMargins = True Dim offsetX As Single = 100 ' default marginbound Dim offsetY As Single = 100 Dim p As Point = e.MarginBounds.Location p.Offset(-offsetX, -offsetY) e.Graphics.DrawImage(img, p) End Sub End Class




Reply With Quote