Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
VB
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
VB
 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
VB
Protected Overrides Sub OnPrintPage(e As PrintPageEventArgs)
       MyBase.OnPrintPage(e)

       Dim g As Graphics = e.Graphics
       g.PageUnit = GraphicsUnit.Inch

       'convert to inches
       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
Posted
Updated 18-Jun-15 3:44am
v2
Comments
Ralf Meier 18-Jun-15 10:32am    
What code must be changed by you for the new instance of your class ?
Perhaps, if this is a question of settings, you could realize that with Properties ...

If you're learning then you're better learning c# rather than vb.net as more people do it and it's easier to find code examples for.

Create a base class (an abstract class) that has all your common properties, methods etc, and give that class an subtract method called onPrintPageEvent with the required parameter. Now create a class for each of your reports, and make that class inherit your base class, and implement (override) the onPrintpageEvent method to supply its own concrete implementation.

If you really only want one class that you pass something to to execute your methods then you can use delegates where you create a delegate for your onPrintPageEvent method then pass in to your function different concrete print methods to the print function. Google using delegates for more info, it is basically using methods as parameters rather than variables.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jun-15 11:05am    
5ed, which should be a fair mark for answering such a vague question.

There are more important arguments in favor of C#: it is ECMA and ISO standard, while VB.NET will probably never be standardized. But the major thing is cultural and social. Many serious developers will always consider anything called VB* as amateurish thing, not matter how good the language actually is. Nobody can deny that all "VB" are stemmed for the idea to provide a language for those who wanted something cheap and "easy", not wanting to learn anything seriously, and so on. Even Microsoft doesn't take VB.NET as serious as C# and other .NET languages, it can be clearly seen on the history of features. Naturally, better developers try to keep to something more solid, that's why getting help in C# is much easier.

—SA
JNDIONYSIM 18-Jun-15 12:16pm    
As for the argument , I wrote my first code in qbasic , and i was trying to change the godzila with the bananes sample code and the snake sample code :) , So far i am a hobist and i am writing code just for me and for my friends , I hardly find any differances between the 2 languages (at my level) of course i know that they are serious differances but i can do anything i can imagine with Visual Basic , so no need to go to C# but you never know :)
Okkk Many Thanks ... I solved my problem with delegates

In my Report Class i add a add a type definition of a delegate and a property to this delegate , i also add a single lile of code in the overide sub OnPrintPage to invoke the delegate here is the code i used

VB
Public Class Report
    Inherits Printing.PrintDocument

    Public Delegate Sub OnPrintPageDel(ByVal e As PrintPageEventArgs)

    Property DocumentForPrint() As OnPrintPageDel

  
    Protected Overrides Sub OnPrintPage(e As PrintPageEventArgs)
        MyBase.OnPrintPage(e)


        DocumentForPrint.Invoke(e)


    End Sub



So now on my main progam i can create an object of my Report class and pass in DocumentForPrint Property all the code i previous had in OnPrintPageEvent like this
VB
Dim prn As New Report
      prn.DocumentForPrint = Sub(a As Printing.PrintPageEventArgs)

                                 Dim g As Graphics = a.Graphics
                                 g.PageUnit = GraphicsUnit.Inch

                                 Dim txt As New pText
                                 'convert to inches
                                 Dim LeftMargin As Single = a.MarginBounds.Left / 100
                                 Dim RightMargin As Single = a.MarginBounds.Right / 100
                                 Dim TopMargin As Single = a.MarginBounds.Top / 100
                                 Dim BottomMargin As Single = a.MarginBounds.Bottom / 100
                                 Dim Width As Single = a.MarginBounds.Width / 100
                                 Dim Height As Single = a.MarginBounds.Height / 100

                                 Dim currentPosition As PointF = New PointF(TopMargin, LeftMargin)
                                 Dim currentSize As New SizeF

                                 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(a)

                                 txt.iBrush = Brushes.Gray
                                 txt.iFont = New Font("Segoi UI", 14, FontStyle.Regular)
                                 txt.iText = "FIRE FIGHTING EQUIPMENT - INSPECTOR AND SUPPLIERS "
                                 currentPosition = txt.PrintText(a)

                                 txt.iText = "MARINE FIRE FIGHTING AND LIFE SAVING"
                                 currentPosition = txt.PrintText(a)

                                 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(a)
                                 txt.iText = "Tel: (+30) 210 - 42.00.029  Cell : (+30) 210 - 694.53.96.696 Fax : (+30) 211 - 79.04.556"
                                 currentPosition = txt.PrintText(a)
                                 txt.iText = "www.paraloshellas.gr - Simoudis@paraloshelas.gr"
                                 currentPosition = txt.PrintText(a)

                                 txt.IncreacePosition_Height()
                                 LinePoint2 = New PointF(Width, txt.iPosition.Y)
                                 g.DrawLine(P, txt.iPosition, LinePoint2)

                                 txt.IncreacePosition_Height()

                                 txt.iFont = New Font("Times New Roman", 24, FontStyle.Bold)
                                 txt.iBrush = Brushes.Black
                                 txt.iFormat.Alignment = StringAlignment.Center
                                 txt.iText = "CERTIFICATE FOR FIRE EXTINGUISHERS"
                                 currentPosition = txt.PrintText(a)

                                 g.DrawRectangle(P, txt.iPosition.X, txt.iPosition.Y, Width, 2)

                             End Sub

      prn.DefaultPageSettings = pgSettings
      PrintPreviewControl1.Document = prn



Its Amazing !!!! I think i can improve the report class and build something really good.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900