Passing Values Between Forms

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
Hello All,

To start off, I am new at programming.

I have a school project that has me stumped and for some time now at that.

Form1 has textboxes and a button. When the button is pressed, thevalues of these textboxes are passed to the textboxes on Form2. WhenForm2 loads, it creates a child form, Form3 and at the same time,passesthe values of Form2's textboxes to the textboxes in Form3.There is abutton on Form2 and when pressed, it passes the values of Form2'stextboxes to Form4. Form4 then loads and the load event generates asimple message.

This all works the first time around, however - the second time around,I change the values of Form1's textboxes and click the button...Form2shows them fine, the child form, Form3 does not and when I click thebutton on Form2, the values are passed to Form4 fine, but the loadevent of Form4 doesn't happen?

Form1:
VB.NET:
Public Class Form1
    Inherits System.Windows.Forms.Form
'Windows Form Designer generated code

    Private FirstForm As New Form2

    Private Sub btnShowForm_Click(ByVal sender AsSystem.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click

        FirstForm.Label1.Text = Me.TextBox1.Text
        FirstForm.Label2.Text = Me.TextBox2.Text
        FirstForm.Label3.Text = Me.TextBox3.Text
        FirstForm.Label4.Text = Me.TextBox4.Text

        FirstForm.Show()

    End Sub

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

    End Sub
Form2:
VB.NET:
Public Class Form2
    Inherits System.Windows.Forms.Form
'Windows Form Designer generated code

    Private ChildForm As New Form3
    Private MessageForm As New Form4

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        Me.Hide()

    End Sub

    Private Sub btnShowForm_Click(ByVal sender AsSystem.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click


        MessageForm.TextBox1.Text = Me.TextBox1.Text
        MessageForm.TextBox2.Text = Me.TextBox2.Text
        MessageForm.TextBox3.Text = Me.TextBox3.Text
        MessageForm.TextBox4.Text = Me.TextBox4.Text

        Me.Hide()
        MessageForm.Show()


    End Sub

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


        ChildForm.TopLevel = False
        ChildForm.Parent = Me
        ChildForm.Location = (New Point(16, 16))

        ChildForm.Show()

        ChildForm.TextBox1.Text = Me.TextBox1.Text
        ChildForm.TextBox2.Text = Me.TextBox2.Text
        ChildForm.TextBox3.Text = Me.TextBox3.Text
        ChildForm.TextBox4.Text = Me.TextBox4.Text


    End Sub
Form4
VB.NET:
Public Class Form4
    Inherits System.Windows.Forms.Form
'Windows Form Designer generated code

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


        MsgBox("The Form Is Working!")
        Me.Hide()


    End Sub

I have been working at this and have been to many forums without a fix. I hope I can get this issue resolved.

Thank you,

Scott
 
Last edited:

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
The load procedure only fires when the form loads (go figure :)).
When you hide a form, then use the Show method again, it simply makes it visible and doesn't load it again.
Instead of using the Show method, you could create another method (Open for example) that contains the code in the current Load procedure, along with a Me.Show call.

Here's a great article that explains different methods of sharing info between forms: Multiple Forms in VB.NET. Part 4 - Accessing Controls and Data ...
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
You could also use the form_event visiblechanged.
Or instead of using hide you could close, that way it would be reloading (you'd need to dim the form in the click sub too).
 

taxes

Member
Joined
Feb 6, 2005
Messages
5
Programming Experience
10+
Paszt said:
Instead of using the Show method, you could create another method (Open for example) that contains the code in the current Load procedure, along with a Me.Show call.

Or you could put the code in the form Activated event.
 
Top Bottom