Printing

  • Thread starter Thread starter StrandElectric
  • Start date Start date
S

StrandElectric

Well, I've reached a major snag and Google does not help. I want to print a
form that's visible on the screen (Me) WITHOUT reams of code. Surely we
could just have had Printer.Print(Me)? But no. The proposed solutions are
hopelessly convoluted. After all, it's pretty basic to print a form or a
variable and line them up. In the REAL business world, applications print
out data from variables all the time. It is one of the most basic business
operations. So if the Net team was geared to the real world they would have
provided for it? Right? Maybe I'm too thick to see it yet.

But, please forget about that for a moment, let's just concentrate on
printing the form.

So here's your challenge. I really need Dennis type answers, if you are
still around this ng Dennis!
 
Armin Zingler said:
Am 14.01.2011 21:21, schrieb StrandElectric:

http://msdn.microsoft.com/en-us/library/6he9hz8c(VS.90).aspx

It looks as if there's one line missing. Insert

e.HasMorePages = False

after the line

e.Graphics.DrawImage(memoryImage, 0, 0)

Thanks Armin for your prompt help. But as it says at the top, that is is for
'Part of the development process'. (Absurd, when you think that Print is
available as a menu option in the IDE for code (but not for the form image,
which it is for vb6)).

But, no. This is not fordevelopment. It is to be an option for the end user,
accessed by one of a number of buttons. In my vb6 app, the user just clicked
on a command button. The code given appears to be standalone and I can't see
how to slot it into my app under the button procedure.
 
Am 14.01.2011 22:15, schrieb StrandElectric:
Thanks Armin for your prompt help. But as it says at the top, that is is for
'Part of the development process'. (Absurd, when you think that Print is
available as a menu option in the IDE for code (but not for the form image,
which it is for vb6)).

But, no. This is not fordevelopment. It is to be an option for the end user,
accessed by one of a number of buttons. In my vb6 app, the user just clicked
on a command button. The code given appears to be standalone and I can't see
how to slot it into my app under the button procedure.

I don't know what you want. The button procedure is part of the example.
Just copy&paste into a new application and try it (don't forget the one line
to insert as I said). Shorter is not possible. There is no keyword "PrintTheActiveForm".
 
Am 14.01.2011 22:58, schrieb Armin Zingler:
Am 14.01.2011 22:15, schrieb StrandElectric:

I don't know what you want. The button procedure is part of the example.
Just copy&paste into a new application and try it (don't forget the one line
to insert as I said). Shorter is not possible. There is no keyword "PrintTheActiveForm".


Paste these lines into your Form class if you don't want to waste any thought:

Private WithEvents printDocument1 As New PrintDocument
Dim memoryImage As Bitmap

Private Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
memoryGraphics.Dispose 'added by AZ
End Sub

Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
printDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0)
e.HasMorePages = False 'added by AZ
End Sub

And in your Button_Click event handler, paste the following two lines:

CaptureScreen()
printDocument1.Print()
 
Armin Zingler said:
Am 14.01.2011 22:58, schrieb Armin Zingler:


Paste these lines into your Form class if you don't want to waste any
thought:

Private WithEvents printDocument1 As New PrintDocument
Dim memoryImage As Bitmap

Private Sub CaptureScreen()
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = Me.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0,
s)
memoryGraphics.Dispose 'added by AZ
End Sub

Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
printDocument1.PrintPage
e.Graphics.DrawImage(memoryImage, 0, 0)
e.HasMorePages = False 'added by AZ
End Sub

And in your Button_Click event handler, paste the following two lines:

CaptureScreen()
printDocument1.Print()

OK. I understand that better. Funnily enough The fact that clicking the
user's 'Print' command button then invoked the Capture screen and then
printdocument subs was the part I understood. But not all that other stuff
on theMSDN site you gave me.

Looking at the original MSDN article, I find that none of the Import lines
are needed Or the Inherits form (as indeed you have omitted them). Also I
got an error with the line first in your list saying Private with events
etc, and it told me already declared! So I commented out that line and it
worked fine!. No mention of the MSDN New or main? What gives? If so many
lines are not needed then put them in? It works fine without them.

Apart from these questions, can I SIMPLY open the dialogue box to choose a
printer and perhaps landscape?

BTW, My wife says 'Strand' refers to my hair style...
 
StrandElectric said:
OK. I understand that better. Funnily enough The fact that clicking the
user's 'Print' command button then invoked the Capture screen and then
printdocument subs was the part I understood. But not all that other stuff
on theMSDN site you gave me.

Looking at the original MSDN article, I find that none of the Import lines
are needed Or the Inherits form (as indeed you have omitted them). Also I
got an error with the line first in your list saying Private with events
etc, and it told me already declared! So I commented out that line and it
worked fine!. No mention of the MSDN New or main? What gives? If so
many lines are not needed then put them in? It works fine without them.

Apart from these questions, can I SIMPLY open the dialogue box to choose a
printer and perhaps landscape?

BTW, My wife says 'Strand' refers to my hair style...
More progress! It seems that the reason the private WithEvents
printDocument line was not needed (already declared friend) was that I had
previously placed a print control below the form. So I took that off and now
I DID need that line (obviously they are both ways of achieving the same
thing). Next it would not work so I experimentally brought back in Imports
System.Drawing.Printing which I didn't need before, and it worked so I do
now!

Armin, if you have more spare hours(! you are in line for a medal here), you
could try explaining in SIMPLE terms what each line in the MSDN article does
(but NOT the Subs Capture Screen, PrintDocument1, or the calling Sub, as I
do understand those. And why you chose to mit some stuff in your fragment of
code (but I omitted even more!).

And I would love to go to the dialogue bax and choose printers etc. Or even
hardcode Landscape or Portrait (as I could in VB6). Reason: The form on my
project happens to be too wide for A4 Portrait.

Don't ask much, do I? No doubt you've resigned from your day job...
 
Am 15.01.2011 01:39, schrieb StrandElectric:
OK. I understand that better. Funnily enough The fact that clicking the
user's 'Print' command button then invoked the Capture screen and then
printdocument subs was the part I understood. But not all that other stuff
on theMSDN site you gave me.

I happened to find the "how to:..." and thought the sample code is exactly
what you need. I didn't analyze the rest of the text in respect of the
it's didactic value.
Looking at the original MSDN article, I find that none of the Import lines
are needed Or the Inherits form (as indeed you have omitted them).

I accidently omitted them. They are there for guys that want
running sample code without further instructions. :-)
Also I
got an error with the line first in your list saying Private with events
etc, and it told me already declared! So I commented out that line and it
worked fine!.

Then you had already placed a PrintDocument on your Form, before.
(yep, as I see in your next post now)
No mention of the MSDN New or main? What gives? If so many
lines are not needed then put them in? It works fine without them.

see above. MSFT (and me too) doesn't know which lines you already have
and which project-wide Namespace you have already imported. They
try to make it work in as many cases as possible. (I've never been there
but I anticipate.)
Apart from these questions, can I SIMPLY open the dialogue box to choose a
printer and perhaps landscape?

I don't know. Never done that. If you're looking for
"How to: Choose the Printers Attached to a User's Computer in Windows Forms
then the following topic is pretty close to the previous one:

http://msdn.microsoft.com/en-us/library/cfkddyc2(VS.90).aspx
BTW, My wife says 'Strand' refers to my hair style...

Hmmm, I don't know which one it is:

http://dict.leo.org/ende?lp=ende&la...=relaxed&sectHdr=on&spellToler=&search=strand

So many meanings.... ;-)
 
Am 15.01.2011 01:57, schrieb StrandElectric:
Armin, if you have more spare hours(! you are in line for a medal here),

Only if you're looking into my multi-threading issues in my other thread! ;-)
you
could try explaining in SIMPLE terms what each line in the MSDN article does
(but NOT the Subs Capture Screen, PrintDocument1, or the calling Sub, as I
do understand those. And why you chose to mit some stuff in your fragment of
code (but I omitted even more!).

Ok. Just ignore the lines that you're not interested in (I'm not exactly sure which):

Yes, REALLY SIMPLE, I know!

I hope you're using a fixed-width font to view this message.



Public Class Form1 'everything between "Public Class" and "End Class" belongs to your Form.

Inherits Form 'Your Form gets all features that MSFT put into another class called "Form".
'Without this line, you can not even show your Form. It's like everything
'they wrote there is copied into your Form. Not really but similar.

Private WithEvents printButton As New Button
Private WithEvents printDocument1 As New PrintDocument

Public Sub New() 'Whenever your Form is created (at program startup), "Sub New" is always
'the Sub that is _automatically_ executed first. It's a special name with
'this special meaning. So, if you want to initalize variables, do it in "Sub New".

printButton.Text = "Print Form" 'change Text on the Button

Me.Controls.Add(printButton) '"Controls" references a list of all the controls on the Form.
'The line adds the printButton to the list. Without the line,
'the button is not visible on the Form.

End Sub

Dim memoryImage As Bitmap 'References a Bitmap used to store the screen capture in.


Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
printDocument1.PrintPage

e.Graphics.DrawImage(memoryImage, 0, 0) 'Draw the previously captured image on the printer

End Sub

Public Shared Sub Main() 'Only required for a new program. Therefore omitted by me.
'It is the "entry point" of the program, i.e. the first Sub called.

Application.Run(New Form1()) 'The method "Run" Shows the Form and starts an (almost) infinite loop.
'The loop is there to process all the mouse-clicks and keyboard input.
'and passes them to the appropriate window/Form. The loop exits as soon
'as the Form closes. Without the loop, your program either wouldn't
'respond or quit immediatelly after start.
End Sub
End Class
And I would love to go to the dialogue bax and choose printers etc. Or even
hardcode Landscape or Portrait (as I could in VB6). Reason: The form on my
project happens to be too wide for A4 Portrait.

Does the link in my prev post help? Haven't done this yet, too.
Don't ask much, do I? No doubt you've resigned from your day job...

No job ATM. Underqualified probably, I don't know. (BTW, I'm "here" from
1 PM til 4 AM usually, which is 10 PM - 2 PM your time.)
 
Hi Armin

My comments with ****. I'm not trying to argue. Just observing what is
happening here. Perhaps what is being shown is that there are several ways
to do a job.

Armin Zingler said:
Public Class Form1 'everything between "Public Class" and "End Class"
belongs to your Form.

****Yes, I understand that an it was already in my app that I'm now fitting
the printing into as one of the button driven options.
Inherits Form 'Your Form gets all features that MSFT put
into another class called "Form".
'Without this line, you can not even show
your Form. It's like everything
'they wrote there is copied into your Form.
Not really but similar.

***This puzzles me. Before I put your printing suggestion into the app, it
worked perfectly without that line. My app starts with dims (eg Private),
functions, and a structure and the first sub is Form1_Load which *does* show
the form!
Private WithEvents printButton As New Button
Private WithEvents printDocument1 As New PrintDocument

***I'm only using the second line
Public Sub New() 'Whenever your Form is created (at program
startup), "Sub New" is always
'the Sub that is _automatically_ executed
first. It's a special name with
'this special meaning. So, if you want to
initalize variables, do it in "Sub New".

***Again, I've never used this and yet my app works fine, even the printing
now, thanks to you!
printButton.Text = "Print Form" 'change Text on the Button

Me.Controls.Add(printButton) '"Controls" references a list of all
the controls on the Form.
'The line adds the
printButton to the list. Without the line,
'the button is not visible
on the Form.

***I originally put the 'Print' button on the form using designer, and later
added the part of the code that you nominated. It is visible and works fine.
Dim memoryImage As Bitmap 'References a Bitmap used
to store the screen capture in.

***Got it
Public Shared Sub Main() 'Only required for a new program.
Therefore omitted by me.
'It is the "entry point" of the
program, i.e. the first Sub called.

***Mine is Form1_Load, as explained. It shows some data before any action is
taken
Application.Run(New Form1()) 'The method "Run" Shows the Form
and starts an (almost) infinite loop.
'The loop is there to process all
the mouse-clicks and keyboard input.
'and passes them to the appropriate
window/Form. The loop exits as soon
'as the Form closes. Without the
loop, your program either wouldn't
'respond or quit immediatelly after
start.

***That wasn't in your 'if you don't want to waste any thought' extract, so
I didn't put it in and everything works perectly

Cheers
Alan
 
Am 15.01.2011 07:23, schrieb StrandElectric:
***This puzzles me. Before I put your printing suggestion into the app, it
worked perfectly without that line. My app starts with dims (eg Private),
functions, and a structure and the first sub is Form1_Load which *does* show
the form!

The sample code assumes a new, empty project. As you already had a Form, the line
"Inherits" was already there, hence no need to paste it. Yes, it was there! You
don't see it because it's in a hidden file called "Form1.Designer.vb". Where it
is? I show you the steps:

http://www.abload.de/image.php?img=idehiddenfilesuiav.png

As you see now, your Form is actually split into to two source code files named
Form1.vb and Form1.designer.vb.

***Again, I've never used this and yet my app works fine, even the printing
now, thanks to you!

Again, you mustn't mix my general explanation suited to the MSFT sample
with your situation in which you have had already a Form.
Now that you know that there is a *.Designer.vb file for your Form, you'll
also find "Sub New" in it. The existence of a hidden file explains a lot, doesn't it?
***I originally put the 'Print' button on the form using designer, and later
added the part of the code that you nominated. It is visible and works fine.

Again, the MSFT sample assumed a different situation in which you do need all
that you are saying it would work without. It doesn't work without. It is
just hidden. This is to keep your own code file (Form1.vb) "clean".
***Mine is Form1_Load, as explained. It shows some data before any action is
taken

Untrue. Your application also has a Sub Main but you don't see it. In order to keep
it short, I only drop the word "Application Framework". I don't elaborate on this
(unless you will ask). True is that Form1_Load handles the Form's Load event which
occurs straight before the Form is shown the first time. Even Sub New is executed
before Form_Load. Sub Main is the first thing that is executed in every
application - always.

If I may say only two abstract sentences: Not only your source code is compiled into
the Exe file. Also additional code is written into it whenever you compile the project.
There is no souce code for this additional code in order to hide tasks typically
needed in new, simple applications.
***That wasn't in your 'if you don't want to waste any thought' extract, so
I didn't put it in and everything works perectly

The hidden "Sub Main" in your project contains the same line.
Again, the MSFT sample assumed a different situation in which you do need
the line.

I didn't write (or ever read) the instructions how to prepare for using a MSFT
sample, but if I think about it now, it can really be irritating.
 
The preparation for using the MSFT example is:

1. Create a new "Windows Forms Application"
2. Delete "Form1" from the project
3. Open the project properties (menu Project -> XY properties)
4. On the "Application" tab, uncheck the option "[X] Enable Application Framework"
5. Add a new Class to the project (menu Project -> add class)
6. Replace the two lines in the new file with the sample code

If you did it this way, you would have needed _all_ the lines
from the sample code.

I wrote this if you want to try more samples. However, I can't guarantee
that you will have to take exactly the same steps. We will see.
 
I really need Dennis type answers, if you are
still around this ng Dennis!

Sorry, but I don't print from my app's very often ... when I do I
usually find some code on the web and tweak to make it work with my app.
 
Back
Top