Hello , i am a beginer and amateur programmer , I am trying to build a class so i can easily design differant forms and reports for printing and print preview with PrintDocument and printpreview dialog in vb.net
So far i came up with something like this .
I have an interface for properties i need for a text to be printed like this
Interface ITextStyle
Property iFont As Font
Property iBrush As Brush
Property iFormat As StringFormat
Property iText As String
Property iPosition As PointF
Property iSize As SizeF
Function PrintText(ByVal e As PrintPageEventArgs, Optional ByVal OnlySize As Boolean = False, Optional ByVal StayInLine As Boolean = False) As PointF
Sub IncreacePosition_Height(Optional ByVal HStep As Single = 0.1)
Sub IncreacePosition_Width(Optional ByVal WStep As Single = 0.5)
End Interface
Then i have a class named pText that implements this interface , i have some gets and sets for the properties and passing default fonts or brushes and i have an function that prints the text described in the above interface
Public Function PrintText(ByVal e As PrintPageEventArgs, Optional ByVal OnlySize As Boolean = False, Optional ByVal StayInLine As Boolean = False) As PointF Implements ITextStyle.PrintText
Dim g As Graphics = e.Graphics
Dim Wdth = g.MeasureString(iText, iFont, _iSize, iFormat).Width
Dim Heht = g.MeasureString(iText, iFont, _iSize, iFormat).Height
If Not OnlySize Then
g.DrawString(_iText, _iFont, _iBrush, New RectangleF(_iPosition, _iSize), _iFormat)
If StayInLine Then
_iPosition.X += Wdth
Else
_iPosition.Y += Heht
End If
End If
End function
Finally i have a class named report that inherits from the PrintDocument object and in the OnPrintPage event i am creating an object from the class pText and printing it to the document
Protected Overrides Sub OnPrintPage(e As PrintPageEventArgs)
MyBase.OnPrintPage(e)
Dim g As Graphics = e.Graphics
g.PageUnit = GraphicsUnit.Inch
Dim leftMargin As Single = e.MarginBounds.Left / 100
Dim rightMargin As Single = e.MarginBounds.Right / 100
Dim topMargin As Single = e.MarginBounds.Top / 100
Dim bottomMargin As Single = e.MarginBounds.Bottom / 100
Dim width As Single = e.MarginBounds.Width / 100
Dim height As Single = e.MarginBounds.Height / 100
Dim currentPosition As PointF = New PointF(topMargin, leftMargin)
Dim currentSize As New SizeF
Dim txt As New pText
g.DrawImage(My.Resources.Logo, New RectangleF(leftMargin, topMargin - 0.15, 1, 1))
txt.iBrush = Brushes.DarkBlue
txt.iFont = New Font("Segoi UI", 28, FontStyle.Bold)
txt.iFormat = StringFormat.GenericTypographic
txt.iFormat.FormatFlags = StringFormatFlags.FitBlackBox
txt.iFormat.Alignment = StringAlignment.Center
txt.iFormat.Trimming = StringTrimming.EllipsisPath
txt.iSize = New SizeF(width, 0)
txt.iPosition = New PointF(topMargin, leftMargin)
txt.iText = "P a r a l o s H e l l a s C o . "
currentPosition = txt.PrintText(e)
txt.iBrush = Brushes.Gray
txt.iFont = New Font("Segoi UI", 14, FontStyle.Regular)
txt.iText = "FIRE FIGHTING EQUIPMENT - INSPECTOR AND SUPPLIERS "
currentPosition = txt.PrintText(e)
txt.iText = "MARINE FIRE FIGHTING AND LIFE SAVING"
currentPosition = txt.PrintText(e)
txt.IncreacePosition_Height()
Dim LinePoint2 As New PointF(width, txt.iPosition.Y)
Dim P As New Pen(Brushes.Black, 0.01)
g.DrawLine(P, txt.iPosition, LinePoint2)
txt.iFont = New Font("Segoi UI", 12, FontStyle.Regular)
txt.iText = "54 Dramas Str. Piraeus 185 43"
currentPosition = txt.PrintText(e)
txt.iText = "Tel: (+30) 210 - 42.00.029 Cell : (+30) 210 - 694.53.96.696 Fax : (+30) 211 - 79.04.556"
currentPosition = txt.PrintText(e)
txt.iText = "www.paraloshellas.gr - Simoudis@paraloshelas.gr"
currentPosition = txt.PrintText(e)
txt.IncreacePosition_Height()
LinePoint2 = New PointF(width, txt.iPosition.Y)
g.DrawLine(P, txt.iPosition, LinePoint2)
End Sub
The above code produce a very satisfing printing .
Now on my main program i am creating an object from class report , puting this object to aprintpreviewdialog.document and all worksgreat bu its not what i want :) becouse if i need to create 10 differant reports i have to create 10 differant classes and change only the code inside onPrintPageEvent so i can build 10 differants reports.
How can i have One class that building the reports and then some how call the onPrintPageEvent to print it ???
I am sorry for my english :)
And thanks for your time