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.
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.
Re: Print two richtextboxes plustwo labels on form
Originally Posted by jmcilhinney
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.
Re: Print two richtextboxes plustwo labels on form
Originally Posted by .paul.
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.
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
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.
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:
Public Class Form1
Dim img As Bitmap
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dpiX As Integer = CInt(Me.CreateGraphics().DpiX)
Dim dpiY As Integer = CInt(Me.CreateGraphics().DpiY)
img = New Bitmap(3 * dpiX, 5 * dpiY)'3" by 5"
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(Label2.Text, Label2.Font, New SolidBrush(Label2.ForeColor), Label2.Left, Label2.Top) 'specify printed location
Dim p As Point = RichTextBox1.Location 'specifies printed location
p.Offset(1, 1)
Dim s As Size = Size.Subtract(RichTextBox1.Size, New Size(5, 5))
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
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.
Re: Print two richtextboxes plustwo labels on form
to print accurately:
vb Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage