|
-
Jan 11th, 2008, 03:33 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Why is this not showing on form?
working on creating a form that I can print and am trying to practice my gdi+ skills
Code:
Private Sub FormPrint_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
PrintHeader(e.Graphics)
End Sub
Private Sub PrintHeader(ByVal g As Graphics)
'Setup StringFormat to be used through the printing process
Dim CN_Format As New StringFormat
CN_Format.Trimming = StringTrimming.Word
CN_Format.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.LineLimit Or StringFormatFlags.NoClip
CN_Format.Alignment = HeaderAlignment
'Print CompanyName
If CompanyName.Length > 0 Then
Dim CN_Rect As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - _
CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(CompanyName, ReportFont).Height)
g.DrawString(CompanyName, ReportFont, New SolidBrush(Color.Black), CN_Rect, CN_Format)
CurrentY += g.MeasureString(CompanyName, ReportFont).Height
End If
If ReportTitle.Length > 0 Then
'Print the report title
Dim RT_Rect As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - _
CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(CompanyName, ReportFont).Height)
g.DrawString(ReportTitle, ReportFont, New SolidBrush(Color.Black), RT_Rect, CN_Format)
CurrentY += g.MeasureString(ReportTitle, ReportFont).Height
End If
If ShowReportDate = True Then
If ReportDate.ToString.Length = 0 Then ReportDate = Now.Date
Dim RptDate As String = String.Format("00/00/0000", ReportDate.ToString)
Dim DateRect As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - _
CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(RptDate, ReportFont).Height)
g.DrawString(RptDate, ReportFont, New SolidBrush(Color.Black), DateRect, CN_Format)
CurrentY += g.MeasureString(RptDate, ReportFont).Height
End If
If ShowPageNumber = True Then
Dim Page As String = "Page: " & PageNumber.ToString
Dim PageRect As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - _
CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(Page.ToString, ReportFont).Height)
g.DrawString(Page, ReportFont, New SolidBrush(Color.Black), PageRect, CN_Format)
CurrentY += g.MeasureString(Page, ReportFont).Height
End If
End Sub
However when I load the form, nothing at all shows up. What am I doing wrong??
Thanks,
D
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you. 
Please remember to mark threads as closed if your issue has been resolved.
Reserved Words in Access | Connection Strings
-
Jan 11th, 2008, 03:41 PM
#2
Frenzied Member
Re: [2005] Why is this not showing on form?
Did you try setting a break point? Is the event handler running? Is the method actually being called?
-
Jan 11th, 2008, 03:43 PM
#3
Thread Starter
Fanatic Member
-
Jan 11th, 2008, 03:44 PM
#4
Frenzied Member
Re: [2005] Why is this not showing on form?
Could you post some code? Usually if you add controls properly to a form and then instantiate it and call the Show method during execution, it should look just like it does during designer mode.
-
Jan 11th, 2008, 03:47 PM
#5
Re: [2005] Why is this not showing on form?
dminder, use the printpreview control instead of printing onto the form directly if this is all to get you familiar with report printing using GDI+.
The printpreview will allow you to use a printdocument object, which is what you will use with GDI+ to draw a report to the printer. This will give you more control, (and teach you some non GDI+ things that are important) like multiple page handling etc...
As for why you are getting no output on your form with the above code, I am not sure, as it looks like it should work unless your logic for your rectangles is flawed.
Did you try simply calling DrawRectangle passing in the rectangles you are creating for your text? I usually do that to make sure the rectangles I am creating to hold text are actually set with the x/y/height/width values I am expecting.
Also just a side note, if you are simply drawing text with a black font, you can use the shared brushes.black value instead of creating a new solidbrush everytime passing in black as the color.
-
Jan 11th, 2008, 03:48 PM
#6
Re: [2005] Why is this not showing on form?
 Originally Posted by Fromethius
Could you post some code? Usually if you add controls properly to a form and then instantiate it and call the Show method during execution, it should look just like it does during designer mode.
he meant there are no controls and that is on purpose.. he was just saying he is using the form as a drawing canvas to write on it at runtime with GDI+
-
Jan 11th, 2008, 03:49 PM
#7
Thread Starter
Fanatic Member
Re: [2005] Why is this not showing on form?
I thought that was code I posted above.......Here is the entire page from top to bottom:
Code:
Option Explicit On
Option Strict On
Imports System.Data.OleDb
Imports System.ComponentModel
Public Class FormPrint
Inherits System.Windows.Forms.Form
Friend WithEvents MyPrintDialog As PrintDialog
#Region " Print Form Properties "
Private _CompanyName As String
Private _ReportTitle As String
Private _ReportFont As Font = New Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point)
Private _HeaderFont As Font = New Font("Tahoma", 8, FontStyle.Bold, GraphicsUnit.Point)
Private _HeaderAlignment As System.Drawing.StringAlignment = StringAlignment.Center
Private _FormData As DataSet
Public Overloads Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(ByVal value As String)
_CompanyName = value
End Set
End Property
Public Property HeaderAlignment() As System.Drawing.StringAlignment
Get
Return _HeaderAlignment
End Get
Set(ByVal value As System.Drawing.StringAlignment)
_HeaderAlignment = value
End Set
End Property
Public Property ReportTitle() As String
Get
Return _ReportTitle
End Get
Set(ByVal value As String)
_ReportTitle = value
End Set
End Property
Public Property ReportFont() As Font
Get
Return _ReportFont
End Get
Set(ByVal value As Font)
_ReportFont = value
End Set
End Property
Public Property FormData() As DataSet
Get
Return _FormData
End Get
Set(ByVal value As DataSet)
If value IsNot Nothing Then _FormData = value : _FormData = New DataSet
End Set
End Property
Public ReadOnly Property LeftMargin() As Single
Get
Return 0.5
End Get
End Property
Public ReadOnly Property RightMargin() As Single
Get
Return 0.5
End Get
End Property
Public ReadOnly Property TopMargin() As Single
Get
Return 0.5
End Get
End Property
Public ReadOnly Property BottomMargin() As Single
Get
Return 0.5
End Get
End Property
Public ReadOnly Property PageHeight() As Single
Get
Return CSng(11 * 1440)
End Get
End Property
Public ReadOnly Property PageWidth() As Single
Get
Return CSng(8.5 * 1440)
End Get
End Property
#End Region
Public Event Print(ByVal source As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
'Global Variables
Private CurrentX As Single
Private CurrentY As Single
Private ShowReportDate As Boolean
Private ShowPageNumber As Boolean
Private ReportDate As Date = Now.Date
Private PageNumber As Integer = 1
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
FormData = New DataSet
CompanyName = String.Empty
ReportTitle = String.Empty
End Sub
Public Sub New(ByVal ShowReportDate As Boolean, ByVal ShowPageNumber As Boolean, _
Optional ByVal PrintableCompanyName As String = "Company Name", _
Optional ByVal PrintableReportTitle As String = "Report Title")
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
FormData = New DataSet
CompanyName = PrintableCompanyName
ReportTitle = PrintableReportTitle
End Sub
Private Sub FormPrint_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
PrintHeader(e.Graphics)
End Sub
Private Sub PrintInventory_Print(ByVal source As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.Print
'Make sure there is data for a report
If FormData Is Nothing Then
MessageBox.Show("Unable to print blank report")
Exit Sub
ElseIf FormData.Tables.Count = 0 Then
MessageBox.Show("Unable to print blank report")
Exit Sub
End If
'Get graphics object
Dim g As Graphics = CType(New Object, Graphics)
'Set the startpoints for the top and left of the document
CurrentY = CType(TopMargin, Single)
CurrentX = CType(LeftMargin, Single)
'Calculate how many pages there are
Dim PrintableLines As Integer = CInt((PageHeight - TopMargin - BottomMargin) / ReportFont.Height)
Dim BodyLines As Integer = FormData.Tables(0).Rows.Count
End Sub
Private Sub PrintHeader(ByVal g As Graphics)
'Setup StringFormat to be used through the printing process
Dim CN_Format As New StringFormat
CN_Format.Trimming = StringTrimming.Word
CN_Format.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.LineLimit Or StringFormatFlags.NoClip
CN_Format.Alignment = HeaderAlignment
'Print CompanyName
If CompanyName.Length > 0 Then
Dim CN_Rect As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - _
CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(CompanyName, ReportFont).Height)
g.DrawString(CompanyName, ReportFont, New SolidBrush(Color.Black), CN_Rect, CN_Format)
CurrentY += g.MeasureString(CompanyName, ReportFont).Height
End If
If ReportTitle.Length > 0 Then
'Print the report title
Dim RT_Rect As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - _
CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(CompanyName, ReportFont).Height)
g.DrawString(ReportTitle, ReportFont, New SolidBrush(Color.Black), RT_Rect, CN_Format)
CurrentY += g.MeasureString(ReportTitle, ReportFont).Height
End If
If ShowReportDate = True Then
If ReportDate.ToString.Length = 0 Then ReportDate = Now.Date
Dim RptDate As String = String.Format("00/00/0000", ReportDate.ToString)
Dim DateRect As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - _
CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(RptDate, ReportFont).Height)
g.DrawString(RptDate, ReportFont, New SolidBrush(Color.Black), DateRect, CN_Format)
CurrentY += g.MeasureString(RptDate, ReportFont).Height
End If
If ShowPageNumber = True Then
Dim Page As String = "Page: " & PageNumber.ToString
Dim PageRect As RectangleF = New RectangleF(CType(LeftMargin, Single), CurrentY, CType(PageWidth, Single) - _
CType(RightMargin, Single) - CType(LeftMargin, Single), g.MeasureString(Page.ToString, ReportFont).Height)
g.DrawString(Page, ReportFont, New SolidBrush(Color.Black), PageRect, CN_Format)
CurrentY += g.MeasureString(Page, ReportFont).Height
End If
End Sub
End Class
D
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you. 
Please remember to mark threads as closed if your issue has been resolved.
Reserved Words in Access | Connection Strings
-
Jan 11th, 2008, 03:54 PM
#8
Frenzied Member
Re: [2005] Why is this not showing on form?
Yea, I forgot about your original post, but more code surely helps.
Oh, and did kleinma resolve your problem? I presume he posted while you were replying to my post, so you may not have noticed.
-
Jan 11th, 2008, 04:00 PM
#9
Thread Starter
Fanatic Member
-
Jan 11th, 2008, 04:08 PM
#10
Frenzied Member
Re: [2005] Why is this not showing on form?
 Originally Posted by dminder
I still want to know why it is not showing on my form.
Just quickly looking through your code, I noticed something that looked pretty ominous.
Dim g As Graphics = CType(New Object, Graphics)
Perhaps you are looking for to assign g to something returned from the shared functions of the Graphics class?
-
Jan 11th, 2008, 04:12 PM
#11
Thread Starter
Fanatic Member
-
Jan 11th, 2008, 04:19 PM
#12
Frenzied Member
Re: [2005] Why is this not showing on form?
I share your pain. Too often people make things way too complicated. Most of the things in the .NET Framework, be it printing, reading or writing from a database, displaying a form or file dialog, can be achieved in a few lines of code. Those 1,500 lines are usually crap. I'd stay away from them. The simpler the better, in my book. KISS.
Anyways, to answer your question. I've found these after doing a google search:
http://www.knowdotnet.com/articles/printform.html
http://www.daniweb.com/forums/thread8575.html
You can probably shorten the amount of that code on those examples pretty easily too.
-
Jan 11th, 2008, 04:37 PM
#13
Re: [2005] Why is this not showing on form?
Once you get the hang of GDI+ it is really super simple for form printing. Sure it is a bit time consuming, but you get 100% full control over what gets printed. I am just finishing up a project where I had to custom draw 40 application forms. Basically I had to scan in the clients paper forms (they didnt have in electronic format for some reason) and then I draw the form as the background of the page, and fill in all the fields with text. Everything had to be lined up exactly right to fit in the spaces.
Alternatives are the VB powerpack print form control, which basically lets you take a screenshot of a form, and print that. So you lay out your form how you would want it to look printed (think white background form for simple receipt printing or something)
Also a basic version of crystal reports comes with visual studio and you can use crystal reports which is much easier than using GDI+ to custom draw forms, but also requires you distribute extra runtime files with your app.
-
Jan 11th, 2008, 04:45 PM
#14
Thread Starter
Fanatic Member
Re: [2005] Why is this not showing on form?
I have tried the printform control and it does not seem to work very well with the forms when they have scrollbars on them. I will have to come up with something very soon, this is the biggest hang up on my project atm.
Thank you all again, will mark as resolved.
D
Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP
Please Rate If I helped you. 
Please remember to mark threads as closed if your issue has been resolved.
Reserved Words in Access | Connection Strings
-
Jan 11th, 2008, 04:48 PM
#15
Re: [RESOLVED] [2005] Why is this not showing on form?
how complex is the actual report? I am pretty versed in doing these reports with GDI+ now, so you can ping me if you have some specific questions.
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
|