|
-
Mar 7th, 2012, 09:03 AM
#1
Thread Starter
Frenzied Member
Print multiple copies
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
-
Mar 7th, 2012, 10:26 AM
#2
Hyperactive Member
Re: Print multiple copies
The standard way of preparing data for printing is in a report, printing a screen shot of a form seems like a bit of a hack.
rdlc reporting comes with VS standard, if just using express, there are a some free options:
http://www.fast-report.com/en/downlo...-download.html
http://www.gotreportviewer.com/
HTH
Rico
Using: VB.net & MS SQL
-
Mar 7th, 2012, 03:45 PM
#3
Re: Print multiple copies
I have to kinda agree with enrico. If all you were doing is making a screenshot of the form then making use of the printform would be much easier than what you're doing. In fact it's just printform1.print(). As for printing in .net I would do a search in the codebank for a person by the name of Merrion. He is like the forum's .net-printing-guru! Actually, Imma make it easy and post a link... Here :]
-
Mar 7th, 2012, 08:07 PM
#4
Thread Starter
Frenzied Member
Re: Print multiple copies
I need the form that comes up to be exactly like a 4x7 or a 3x5 index card. So the person using it can see if all of the ingredients and the text process will fit on the card. I have .Net Express so not sure if reporting is in it. but the main thread that shows this code is from .paul. and listed here
http://www.vbforums.com/showthread.php?t=641994
using printform also prints the title bar and the forms border.
-
Mar 7th, 2012, 08:46 PM
#5
Re: Print multiple copies
 Originally Posted by enrico1982
The standard way of preparing data for printing is in a report
The "standard" way to print from a .NET application is using a PrintDocument. Using a report is "cheating" by getting someone else to handle the actual printing for you. The RDLC format you speak of is SQL Server Reporting Services.
The PrintDocument has a PrinterSettings property of type PrinterSettings. The PrinterSettings class has a Copies property, which is the number of copies of the output that you want to print.
This:
Code:
Dim dpiX As Integer = CInt(Me.CreateGraphics().DpiX)
Dim dpiY As Integer = CInt(Me.CreateGraphics().DpiY)
is very, very bad. You should never do something like create two Graphics objects when you only need one. In situations like this, you should be assigning the result of the method to a variable and then using the variable multiple times. You should always be disposing objects that support it too, and the Graphics class supports it. The proper way to do that would be:
Code:
Dim dpiX As Integer
Dim dpiY As Integer
Using g = Me.CreateGraphics()
dpiX = CInt(g.DpiX)
dpiY = CInt(g.DpiY)
End Using
-
Mar 7th, 2012, 10:43 PM
#6
Thread Starter
Frenzied Member
Re: Print multiple copies
thanks jmcilhinney, but it doesn't solve my problem. By default it should only print once, but certain times they may want 2 copies. Without giving the user a choice I would just like to print it once. but if you click print again right after you print it doesn't print correctly. closing the form and re-opening it will allow you to print again.
But I am open to suggestions. If there is a better way to print just the text the user sees and not the whole form, title and border, then I would like to do that.
In vb6 I did use printform and it worked great, but .net prints the whole form, title and border and everything, I can't use that.
-
Mar 7th, 2012, 11:01 PM
#7
Re: Print multiple copies
If it's not printing properly the second time then there's something wrong with your code. Have you debugged it to see what that might be? That's how you find and fix bugs in your code. Obviously there is something left over from the first print run that is affecting the second. You need to find out what it is and make sure that it doesn't happen.
As for not printing the whole form, the way to not do it is to not do it. In the PrintPage event you can draw whatever you want. You are creating a Bitmap containing a direct visual representation of the form and drawing that, so that's what gets printed. If that's not what you want then don't do it. If you just want the words "Hello World" printed then you call DrawString and pass that String as an argument. It's up to you to decide what you want printed and to draw that and only that.
-
Apr 8th, 2012, 12:36 PM
#8
Thread Starter
Frenzied Member
Re: Print multiple copies
so are you saying I should use printform and not printdocument?
-
Apr 8th, 2012, 01:12 PM
#9
Thread Starter
Frenzied Member
Re: Print multiple copies
I went with printform again and although I must say I finally got it to print how I want, problem is its a little blurry when it comes out of the printer? anyway to adjust that? Even PrintPreview is blurry
jmcilhinney:
Of course if I don't want it to print like that don't code it to do that, thats is so obvious, but if a person doesn't know it is wrong they will not know how to fix it. You say use printform and you can control what it prints. Same with this, if you don't know anything about ClientAreOnly option then you wouldn't know to look for it. So saying "if you don't want it, don't code" it doesn't help anybody and makes the OP frustrated. I looked at the PrintForm page on msdn and it is not obvious that you can use options like that. I understand not writing it for them, makes perfect sense, but giving them the ideas to know what to look for is a lot better than telling them to don't code it if you don't want it to do it.
Last edited by phpman; Apr 8th, 2012 at 01:19 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|