Results 1 to 13 of 13

Thread: Print two richtextboxes plustwo labels on form

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Print two richtextboxes plustwo labels on form

    Ok, so I done some reading and searching and I am confused on what is the best way to print my form that has 2 labels, 2 richtextboxes on it on get it to print to a 3x5 card or 4x7. I had this built in vb6 and used a picturebox but that method doesn't work in .NET. So what is easier, printform or printdocument or DrawString. I tried the printform but I don't want the whole form printed. Also read about DrawString as well. Honestly, I am confused on all of it.

    I only have 4 controls on the form

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Print two richtextboxes plustwo labels on form

    can you post a screenshot of the form so i can see what you want to print?
    looks like the printdocument is the best choice

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Print two richtextboxes plustwo labels on form

    you can copy the rtb's + labels to a new bitmap + print that using e.graphics.drawimage in your printdocument's printpage event

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

    Re: Print two richtextboxes plustwo labels on form

    To print in .NET you use a PrintDocument. That's basically it. The PrintForm component is something that makes it easier to print an entire form by taking a screen shot and printing it using a PrintDocument internally. DrawString is not an alternative to a PrintDocument. DrawString is a GDI+ method and a PrintDocument uses GDI+. You create a PrintDocument, call its Print method, handle its PrintPage event and, in the event handler, you call Graphics.DrawString to print text. If you want to print the contents of two Labels and two RichTextBoxes then you will probably call DrawString at least four times.
    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

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Print two richtextboxes plustwo labels on form

    Quote Originally Posted by jmcilhinney View Post
    To print in .NET you use a PrintDocument. That's basically it. The PrintForm component is something that makes it easier to print an entire form by taking a screen shot and printing it using a PrintDocument internally. DrawString is not an alternative to a PrintDocument. DrawString is a GDI+ method and a PrintDocument uses GDI+. You create a PrintDocument, call its Print method, handle its PrintPage event and, in the event handler, you call Graphics.DrawString to print text. If you want to print the contents of two Labels and two RichTextBoxes then you will probably call DrawString at least four times.
    a bitmap copied from the controls would print your rtb's with any formatting, pictures, etc, in several lines of code. but it all depends on the layout of your form.

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

    Re: Print two richtextboxes plustwo labels on form

    Quote Originally Posted by .paul. View Post
    a bitmap copied from the controls would print your rtb's with any formatting, pictures, etc, in several lines of code. but it all depends on the layout of your form.
    You'd have to be guaranteed that all the text in the RTB was visible, which I would expect was unlikely. I guess that that's what you mean when you say that it depends on the layout.
    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

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Print two richtextboxes plustwo labels on form

    Sure, screenshot attached. The code I have in it now is the PrintDocument, it prints a blank screen. One form (3x5), I have a picturebox in teh background set to transparent, it prints and you can see the outline but no RTB or text on it. The 4x7 form I don't have a picturebox and it prints a blank page. This is the 4x7 form. I wanted to just get it to print and then I will figure out how to do it on a 4x7 card. But it prints nothing.

    Code:
    Imports System.Drawing.Printing
    
    Public Class Frm4x7
        Dim WithEvents mPrintDocument As New PrintDocument
        Dim mPrintBitMap 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
            FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
            Me.ControlBox = False
            Me.Text = ""
            MenuStrip1.Visible = False
            ' Copy the form's image into a bitmap.
            mPrintBitMap = New Bitmap(Me.Width, Me.Height)
            Dim lRect As System.Drawing.Rectangle
            lRect.Width = Me.Width
            lRect.Height = Me.Width
            Me.DrawToBitmap(mPrintBitMap, lRect)
    
    
            ' Make a PrintDocument and print.
            mPrintDocument = New PrintDocument
            mPrintDocument.Print()
            FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
            MenuStrip1.Visible = True
            Me.ControlBox = True
        End Sub
    
        Private Sub SaveToFileToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveToFileToolStripMenuItem.Click
    
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            ' Draw the image centered.
            Dim lWidth As Integer = 60 + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
            Dim lHeight As Integer = ((e.MarginBounds.Height - mPrintBitMap.Height) \ 2) - 65
            e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
    
            ' There's only one page.
            e.HasMorePages = False
        End Sub
    End Class
    Attached Images Attached Images  

  8. #8
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Print two richtextboxes plustwo labels on form

    Printing from a bitmap will print at your screen resolution and will only print the visible part of the form over one page.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Print two richtextboxes plustwo labels on form

    Which makes me not want to do that as I need it to go on a 3x5 and 4x7 index cards. at screen resolution it will be as big as the screen is correct? no scaling? Doing it at as a bitmap doesn't print the text on the form, only a blank form.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Print two richtextboxes plustwo labels on form

    that's why i asked for a screenshot. you can create a bitmap with a copy of your labels + rtb's at any position you choose. the only problem would be if your rtb's have scrollbars or are otherwise not completely visible:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim img As Bitmap
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim dpiX As Integer = CInt(Me.CreateGraphics().DpiX)
    7.         Dim dpiY As Integer = CInt(Me.CreateGraphics().DpiY)
    8.  
    9.         img = New Bitmap(3 * dpiX, 5 * dpiY)'3" by 5"
    10.         Dim gr As Graphics = Graphics.FromImage(img)
    11.         gr.Clear(Color.White)
    12.         gr.DrawString(Label1.Text, Label1.Font, New SolidBrush(Label1.ForeColor), Label1.Left, Label1.Top) 'left + top properties
    13.         gr.DrawString(Label2.Text, Label2.Font, New SolidBrush(Label2.ForeColor), Label2.Left, Label2.Top) 'specify printed location
    14.  
    15.         Dim p As Point = RichTextBox1.Location 'specifies printed location
    16.         p.Offset(1, 1)
    17.         Dim s As Size = Size.Subtract(RichTextBox1.Size, New Size(5, 5))
    18.         gr.CopyFromScreen(RichTextBox1.PointToScreen(New Point(1, 1)), p, s)
    19.  
    20.         p = RichTextBox2.Location 'specifies printed location
    21.         p.Offset(1, 1)
    22.         s = Size.Subtract(RichTextBox2.Size, New Size(5, 5))
    23.         gr.CopyFromScreen(RichTextBox2.PointToScreen(New Point(1, 1)), p, s)
    24.  
    25.         PrintDocument1.Print
    26.     End Sub
    27.  
    28.     Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    29.         e.Graphics.DrawImage(img, e.MarginBounds.Location)
    30.     End Sub
    31. End Class

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Print two richtextboxes plustwo labels on form

    my rtb's will never have scrollbars. I disabled them. I tried your code and changed what I need but it prints a blank piece of paper.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Print two richtextboxes plustwo labels on form

    nvm, I had an error in the PrintPage function, cleared it and now it prints fine. But I am afraid of the location of the print on the paper is not a 3x5 and with out having the cards on hand it will be hard to test. I will have to get some. So let me play around with it and see how close it is.

    Thanks Paul for you help.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Print two richtextboxes plustwo labels on form

    to print accurately:

    vb Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.        
    3.     PrintDocument1.DefaultPageSettings.Margins.Left = 0
    4.     PrintDocument1.DefaultPageSettings.Margins.Top = 0
    5.     PrintDocument1.OriginAtMargins = True
    6.     Dim offsetX As Single = PrintDocument1.DefaultPageSettings.HardMarginX
    7.     Dim offsetY As Single = PrintDocument1.DefaultPageSettings.HardMarginY
    8.     dim p as point = e.MarginBounds.Location
    9.     p.offset(offsetX,offsetY)
    10.     e.Graphics.DrawImage(img, p)
    11.  
    12. End Sub

    edit:that should be p.offset(-offsetX,-offsetY)
    Last edited by .paul.; Feb 23rd, 2011 at 07:32 PM.

Tags for this Thread

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