0

I am trying to get an integer value of one form (Form1) inside another form (Form2). I have tried to access it via the below code but not getting it. Can someone please tell me what I am doing wrong.

Public Class Form1
    Public Points As Integer = 100

    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.Text = Points
    End Sub

    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.Show()
        Me.Hide()
    End Sub
End Class

Public Class Form2
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim FinalPoints As New Form1
        Label1.Text = FinalPoints.Label1.Text
    End Sub
End Class
2

2 Answers 2

1

The problem: As you are showing your Form2 (Form2_Load is executed), a new Form1 is created. This newly created Form1 has NOT executed the Form1_Load function yet!

You would need to show the newly created FinalPoints (Form1) with FinalPoints.Show() like that:

Dim FinalPoints As New Form1
FinalPoints.Show()
Label1.Text = FinalPoints.Label1.Text

to let the Form1_Load function execute, which is then setting your FinalPoints.Label1.Text. But that would just opens a new Form1.

Also you can just get the public Points variable inside the Form2_Load like that (you also do not have to create a new Form1):

Label1.Text = Form1.Points

Alternatively: Just use a public variable inside Form2 and assign your value to it, before you show the form.

Public Class Form1
    Public Points As Integer = 100

    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.Text = Points.ToString
    End Sub

    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FinalPoints As New Form2
        FinalPoints.StringFromForm1 = Label1.Text
        FinalPoints.Show()
        Me.Hide()
    End Sub
End Class


Public Class Form2
    Public Property StringFromForm1 As String

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.Text = StringFromForm1
    End Sub
End Class
Sign up to request clarification or add additional context in comments.

Comments

0

This line: Dim FinalPoints As New Form1 creates a new instance of Form1, but what you want is to refer to an existing instance of Form1. There are different techniques you can try. For example, overload the Show method of Form2.

Something like this:

Form1: pass the value to Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f As New Form2

    Me.Hide()

    f.Show(Points)
End Sub

Form2: fetch the value from caller (Form1)

Public Class Form2

    Public Overloads Sub Show(ByVal Points As Integer)
        Me.Label1.Text = Points.ToString
        MyBase.Show()
    End Sub

End Class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.