Ank
Ank

Reputation: 6270

Calling a method from different class to write text on a form. VB.NET

I have two forms Form1 and Form 2. I am passing a function with three values from Form1 and want the load event of form2 to trigger this function.. Basically I want to "paint" the values of qseq, midline and hseq on form2

Public Class Form1 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2
            Form2.Visible=True
            f2.DrawString(qseq, midline, hseq)
    End Sub
End Class

Form2:

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub DrawString(ByVal qseq As String, ByVal midline As String, ByVal hseq As String)
        Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
        Dim drawString As String = qseq 'and similarly for midline and hseq
        Dim drawFont As New System.Drawing.Font("Arial", 16)
        Dim drawBrush As New  _
            System.Drawing.SolidBrush(System.Drawing.Color.Black)
        Dim x As Single = 200.0
        Dim y As Single = 100.0
        Dim drawFormat As New System.Drawing.StringFormat
        formGraphics.DrawString(drawString, drawFont, drawBrush, _
                                x, y, drawFormat)
        drawFont.Dispose()
        drawBrush.Dispose()
        formGraphics.Dispose()
    End Sub
End Class

When I run this, nothing gets printed on form2 as when load event is triggered thee drawstring method is not called. how can I call drawstring from load method as drawstring takes in arguments and is called from Form1 class.

Upvotes: 0

Views: 3105

Answers (1)

competent_tech
competent_tech

Reputation: 44931

You should rewrite form2 such that the items to be drawn are passed in the constructor of the form and then used for the actual drawing in the form's Paint event.

For example:

Public Class Form2

    Private QSeq As String
    Private Midline As String
    Private HSeq As String

    Protected Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(ByVal sQseq As String, ByVal sMidline As String, ByVal sHseq As String)
        Me.New()

        QSeq = sQseq
        Midline = sMidline
        HSeq = sHseq
    End Sub

    Private Sub Form2_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        Dim drawString As String = QSeq 'and similarly for midline and hseq
        Dim x As Single = 200.0
        Dim y As Single = 100.0
        Dim drawFormat As New System.Drawing.StringFormat

        Using drawFont As New System.Drawing.Font("Arial", 16)
            Using drawBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Black)
                e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat)
            End Using
        End Using
    End Sub
End Class

Then form1 would call form 2 using the new constructor. Also note that you must make the newly created instance (f2) visible and not the default instance (Form2) that you have in your question.

For example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f2 As New Form2("test", "test2", "test3")
    f2.Visible = True
End Sub

Upvotes: 2

Related Questions