Convert a Public Class Private Sub into Module?

Joined
Oct 20, 2006
Messages
21
Programming Experience
Beginner
I'm new to VB.NET and I'm not the best programmer so please don’t be too hard on me. The following is the code which is declared in the Public Class. Private Sub PrintMe_PrintPage is basically what is called when I want to print something in VB.NET. What I would like to do is move this code to the module where some other code is at. I have an idea of what needs to be done base on how the other code is being called but I 'm still lost. Private Sub PrintMe_PrintPage is called when the users clicks the print button.


VB.NET:
Public Class frmBarCode
    Inherits System.Windows.Forms.Form

    Private Sub PrintMe_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintMe.PrintPage
        Dim MyGraphicsPage As Graphics = e.Graphics
        MyGraphicsPage.PageUnit = GraphicsUnit.Pixel
        'Dim myPen As New System.Drawing.Pen(Color.Black, 0.01)
        'MyGraphicsPage.DrawLine(myPen, Convert.ToSingle(txtXS.Text), Convert.ToSingle(txtYS.Text), Convert.ToSingle(txtXE.Text), Convert.ToSingle(txtYE.Text))
        'MyGraphicsPage.DrawImage(PictureBox1.Image)
        'MyGraphicsPage.DrawImage(Me.PictureBox1.Image, 1, 1)

        'start of modification

        Dim strEANCode As String
        'Dim objPicBox As PictureBox
        Dim sngX1 As Single '= (-1)
        Dim sngY1 As Single '= (-1)
        Dim sngX2 As Single '= (-1)
        Dim sngY2 As Single '= (-1)
        Dim FontForText As Font = Nothing
        Dim K As Single
        Dim sngPosX As Single
        Dim sngPosY As Single
        Dim sngScaleX As Single
        Dim strEANBin As String
        Dim strFormat As New StringFormat()
        sngY2 = txtY2.Text
        sngX2 = txtX2.Text
        sngY1 = txtY1.Text
        sngX1 = txtX1.Text
        'objPicBox = Me.PictureBox1
        strEANCode = Me.txtEAN.Text
        '*
        '* Convert the code on its binary representation
        '*
        strEANBin = EAN2Bin(strEANCode)

        '*
        '* Define the font to be printed
        '*
        If (FontForText Is Nothing) Then
            FontForText = New Font("Courier New", 8)
        End If

        '*
        '* Defines the boundaries to the barcode
        '*
        ' If sngX1 = (-1) Then sngX1 = 0
        'If sngY1 = (-1) Then sngY1 = 0
        'If sngX2 = (-1) Then sngX2 = 100
        'If sngY2 = (-1) Then sngY2 = 100

        '*
        '* Defines the boundaries of the barcode
        '*
        sngPosX = sngX1
        'sngPosY = sngY2 - CSng(1.5 * 1.5) 'FontForText.Height)
        sngPosY = sngY2 - CSng(1.5 * FontForText.Height)

        '*
        '* Clears the area
        '*
        'MyGraphicsPage.FillRectangle(New System.Drawing.SolidBrush(Color.White), sngX1, sngY1, sngX2 - sngX1, sngY2 - sngY1)

        '*
        '* Calculates the scale
        '*
        sngScaleX = (sngX2 - sngX1) / strEANBin.Length

        '*
        '* Draw the BarCode
        '*
        For K = 1 To Len(strEANBin)
            If Mid(strEANBin, K, 1) = "1" Then

                MyGraphicsPage.FillRectangle(New System.Drawing.SolidBrush(Color.Black), sngPosX, sngY1, sngScaleX, sngPosY)
            End If
            sngPosX = sngX1 + (K * sngScaleX)
        Next K

        '*
        '* Draw the human-friendly code
        '*
        strFormat.Alignment = StringAlignment.Center
        strFormat.FormatFlags = StringFormatFlags.NoWrap
        MyGraphicsPage.DrawString(strEANCode, FontForText, New System.Drawing.SolidBrush(Color.Black), CSng((sngX2 - sngX1) / 2), CSng(sngY2 - FontForText.Height), strFormat)

        'end of modification

    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        PrintMe.Print()
    End Sub

    Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click

        PreviewMe.ShowDialog()
    End Sub
End Class
 
That method is an event handler so it cannot be moved. Also, it cannot be called directly because you will not be passing the appropriate objects to the arguments. You could take the code out of that method and place it in another method, which you can then call from the event handler, but I very much doubt that that is required in this case. The only reason to do that would be if you wanted to use the same drawing code for printing as well as some other purpose and you just wanted to be able to pass any Graphics object to the drawing routine. Is that what you want. It's possible but I doubt it. Please explain the actual reason that you want to do this and we can advise whether it is good idea and whether it is even feasible. I'm guessing neither at this stage, but I'll reserve final judgement until I have all the relevant information.
 
VB.NET:
Private Sub GenerateEAN(Byval MyGraphicsPage As Graphics) as Graphic
           MyGraphicsPage.PageUnit = GraphicsUnit.Pixel
        'Dim myPen As New System.Drawing.Pen(Color.Black, 0.01)
        'MyGraphicsPage.DrawLine(myPen, Convert.ToSingle(txtXS.Text), Convert.ToSingle(txtYS.Text), Convert.ToSingle(txtXE.Text), Convert.ToSingle(txtYE.Text))
        'MyGraphicsPage.DrawImage(PictureBox1.Image)
        'MyGraphicsPage.DrawImage(Me.PictureBox1.Image, 1, 1)

        'start of modification

        Dim strEANCode As String
        'Dim objPicBox As PictureBox
        Dim sngX1 As Single '= (-1)
        Dim sngY1 As Single '= (-1)
        Dim sngX2 As Single '= (-1)
        Dim sngY2 As Single '= (-1)
        Dim FontForText As Font = Nothing
        Dim K As Single
        Dim sngPosX As Single
        Dim sngPosY As Single
        Dim sngScaleX As Single
        Dim strEANBin As String
        Dim strFormat As New StringFormat()
        sngY2 = txtY2.Text
        sngX2 = txtX2.Text
        sngY1 = txtY1.Text
        sngX1 = txtX1.Text
        'objPicBox = Me.PictureBox1
        strEANCode = Me.txtEAN.Text
        '*
        '* Convert the code on its binary representation
        '*
        strEANBin = EAN2Bin(strEANCode)

        '*
        '* Define the font to be printed
        '*
        If (FontForText Is Nothing) Then
            FontForText = New Font("Courier New", 8)
        End If

        '*
        '* Defines the boundaries to the barcode
        '*
        ' If sngX1 = (-1) Then sngX1 = 0
        'If sngY1 = (-1) Then sngY1 = 0
        'If sngX2 = (-1) Then sngX2 = 100
        'If sngY2 = (-1) Then sngY2 = 100

        '*
        '* Defines the boundaries of the barcode
        '*
        sngPosX = sngX1
        'sngPosY = sngY2 - CSng(1.5 * 1.5) 'FontForText.Height)
        sngPosY = sngY2 - CSng(1.5 * FontForText.Height)

        '*
        '* Clears the area
        '*
        'MyGraphicsPage.FillRectangle(New System.Drawing.SolidBrush(Color.White), sngX1, sngY1, sngX2 - sngX1, sngY2 - sngY1)

        '*
        '* Calculates the scale
        '*
        sngScaleX = (sngX2 - sngX1) / strEANBin.Length

        '*
        '* Draw the BarCode
        '*
        For K = 1 To Len(strEANBin)
            If Mid(strEANBin, K, 1) = "1" Then

                MyGraphicsPage.FillRectangle(New System.Drawing.SolidBrush(Color.Black), sngPosX, sngY1, sngScaleX, sngPosY)
            End If
            sngPosX = sngX1 + (K * sngScaleX)
        Next K

        '*
        '* Draw the human-friendly code
        '*
        strFormat.Alignment = StringAlignment.Center
        strFormat.FormatFlags = StringFormatFlags.NoWrap
        MyGraphicsPage.DrawString(strEANCode, FontForText, New System.Drawing.SolidBrush(Color.Black), CSng((sngX2 - sngX1) / 2), CSng(sngY2 - FontForText.Height), strFormat)

        'end of modification
return MyGraphicsPage 
    End Function

from your event

VB.NET:
Private Sub PrintMe_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintMe.PrintPage
        Dim MyGraphicsPage As Graphics = GenerateEAN(e.Graphics)
end sub

the function GenerateEAN can be in any class and can be accessable from any object, just make reference to the assembly and instantiate a new instance of the object
 
Back
Top