|
-
Jan 26th, 2009, 04:18 PM
#1
Thread Starter
Hyperactive Member
[2008] PrintDocument Button
Hello,
I followed an example online for using Printdocument.()
When I run the code no error but I haven't seen the printbutton which should be on the form.
Why?
Thanks
The code
Code:
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO
Namespace Microsoft.Samples.WinForms.VB.PrintingExample1
Public Class PrintingExample1
Inherits System.Windows.Forms.Form
private printFont As Font
private streamToPrint As StreamReader
Public Sub New ()
MyBase.New()
PrintingExample1 = Me
'This call is required by the Windows Forms Designer.
InitializeComponent()
'Wire up event handler for print button
AddHandler printButton.Click, AddressOf printButton_Click
End Sub
'Event fired when the user presses the print button
Private Sub printButton_Click(sender As object, e As System.EventArgs)
Try
streamToPrint = new StreamReader ("PrintMe.Txt")
Try
printFont = new Font("Arial", 10)
Dim pd as PrintDocument = new PrintDocument() 'Assumes the default printer
AddHandler pd.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf Me.pd_PrintPage)
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show("An error occurred printing the file - " + ex.Message)
End Try
End Sub
'Event fired for each page to print
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String
'Work out the number of lines per page
'Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
'Now iterate over the file printing out each line
'NOTE WELL: This assumes that a single line is not wider than the page width
'Check count first so that we don't read line that we won't print
line = streamToPrint.ReadLine()
While (count < lpp And line <> Nothing)
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics))
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count = count + 1
If (count < lpp) Then
line = streamToPrint.ReadLine()
End If
End While
'If we have more lines then print another page
If (line <> Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region " Windows Form Designer generated code "
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
Private WithEvents printButton As System.Windows.Forms.Button
Private WithEvents PrintingExample1 As System.Windows.Forms.Form
' NOTE: The following code is required by the Windows Forms Designer
' Do not modify it
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.printButton = New System.Windows.Forms.Button()
Me.Text = "Print Example 1"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 381)
printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat
printButton.Size = New System.Drawing.Size(136, 40)
printButton.TabIndex = 0
printButton.Location = New System.Drawing.Point(32, 112)
printButton.Text = "Print the file"
Me.Controls.Add(printButton)
End Sub
#End Region
'The main entry point for the application
<STAThread()> Shared Sub Main()
System.Windows.Forms.Application.Run(New PrintingExample1())
End Sub
End Class
End Namespace
-
Jan 26th, 2009, 04:44 PM
#2
Re: [2008] PrintDocument Button
why do you have
Code:
PrintingExample1 = Me
in your constructor?
-
Jan 26th, 2009, 04:53 PM
#3
Thread Starter
Hyperactive Member
Re: [2008] PrintDocument Button
I don't know. I just copy the code from there.
Now it is still not working even I comment it.
-
Jan 26th, 2009, 05:06 PM
#4
Thread Starter
Hyperactive Member
Re: [2008] PrintDocument Button
-
Jan 26th, 2009, 06:09 PM
#5
Re: [2008] PrintDocument Button
so you just copied all that code into your form?
Try going into the project properties, and change the startup object to the correct selection of Microsoft.Samples.WinForms.VB.PrintingExample1
-
Jan 27th, 2009, 09:29 AM
#6
Thread Starter
Hyperactive Member
Re: [2008] PrintDocument Button
Yes. You are right.
One more question.
Why the file "PrintMe.TXT" should be placed in the folder ..\bin\debug?
May I put it in somewhere else such as ..\bin\release?
And I get an error as well.
Code:
An error occurred printing the file - The operation completed successfully
Thanks.
Last edited by zhshqzyc; Jan 27th, 2009 at 09:41 AM.
-
Jan 27th, 2009, 09:54 AM
#7
Re: [2008] PrintDocument Button
bin\debug is the location the code gets compiled to as an exe and runs from when you are in debug mode. If you change it to release mode, then the exe is compiled to bin\release by default, and that is where the printme.txt will go as well.
You should look up the help about debug and release mode compiling.
The error you are getting is an odd one, this is one one site says about it:
Error code: 0 (The operation completed successfully) - This would be recorded in the event log when a print job is cancelled by the owner. It is an "error" because the printing basically failed but the "error" is "successful" because that was the intention of the print job owner.
-
Jan 27th, 2009, 11:09 AM
#8
Thread Starter
Hyperactive Member
Re: [2008] PrintDocument Button
I don't know. I set "Build output path" as "bin\Release" and drag the file to the release folder, still wrong.
-
Jan 27th, 2009, 11:40 AM
#9
Re: [2008] PrintDocument Button
you shouldn't change debug compile paths to the release directory. Again you need to read about debug and release mode, so you can understand the concepts behind them.
http://msdn.microsoft.com/en-us/library/wx0123s5.aspx
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
|