I had question about vb.net. I know normally this is simple. But in this case it's dificult. Let me explain this. I had three forms. Form1 is mdi parent form. Others 2 parent forms are child forms which named form2 and form3. I want to send form3 text box value to the form2 textbox when form3 close time.
In normal situation (not MDI child or parent) it's easy to send data :

form3.textbox1.text=form2.textbox1.text
me.close()

But in my case I used above code. But it doesn't work. please help me with more code examples. I appreciate it :)

Recommended Answers

All 10 Replies

The thing is, you are using vb.net and could set a break on line 2 to see what line 1 is doing. I'm sure there's a lot more code than you shared and those other forms are not going to magically update. That is, forms do not update without, well, you're a vb.net developer so your question is two fold. First you use the tools to see what's up with line 1 then next you break in the app to examine the target form values.

Didn't anyone show you how to use the breaks?

What was your target form ? Form3 or Form2. Perfectly determine it.

My target is form2. When I click button in form 2 it shows form3. Then I enter value for form3 textbox1. later clik save and close button. Then the value in form3 textbox must show in form 2 textbox.

form3.textbox1.text=form2.textbox1.text

How could you do your job by uing this. Here your target Form is Form3
Your codes should be

form2.textbox1.text=form3.textbox1.text

hope it can help you.

It looks to me that you're trying to use the class types as instances. One, relatively simple, way to do what you want, is to pass an instance of Form2 to Form3 in the constructor.

In Form3 code:

Public Class Form3
    Public targetForm As New Form2
    Public Sub New(target As Form2)

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

        ' Add any initialization after the InitializeComponent() call.
        targetForm = target
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        targetForm.TextBox1.Text = TextBox1.Text
        Close()
    End Sub
End Class

In the MDI parent something like this:

Public Class Form1
    Public newForm2 As New Form2
    Public newForm3 As New Form3(newForm2)

    Public Sub New()

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

        ' Add any initialization after the InitializeComponent() call.
        newForm2.MdiParent = Me
        newForm3.MdiParent = Me
    End Sub

    Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
        newForm2.Show()
    End Sub

    Private Sub ToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem2.Click
        newForm3.Show()
    End Sub
End Class

This is just a bare bones example but it should help you with the concepts that you need.

I tryied above. But when I close the form again I can't open it.

If you want to show it again you could call Hide instead of Close. Since you'll be re-using the form you will want to clear the textboxes:

Public Class Form3
    Public targetForm As New Form2
    Public Sub New(target As Form2)

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

        ' Add any initialization after the InitializeComponent() call.
        targetForm = target
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        targetForm.TextBox1.Text = TextBox1.Text
        For Each tb As TextBox In Me.Controls.OfType(Of TextBox)
            tb.Text = ""
        Next
        Hide()
    End Sub

Another way to go is to initialize the forms just before you show them:

Public Class Form1

    Public Sub New()

        ' This call is required by the designer.

        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
        Dim newForm2 = New Form2
        newForm2.MdiParent = Me
        newForm2.Show()
    End Sub

End Class

Public Class Form2
    Public Sub New()

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

        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newForm3 = New Form3(Me)
        newForm3.MdiParent = Me.MdiParent
        newForm3.Show()
    End Sub
End Class  

Public Class Form3
    Dim targetForm As Form2
    Public Sub New(target As Form2)

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

        ' Add any initialization after the InitializeComponent() call.
        targetForm = target
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        targetForm.TextBox1.Text = TextBox1.Text
        Close()
    End Sub
End Class

Sure your last option helped me. But I can't understand this. Could you explain me code ?

Here's the code again with more comments that should help explain it a bit more.

Public Class Form1
    Public Sub New()
        ' This call is required by the designer.'
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.'
    End Sub
    'The click that opens an instance of form2'
    Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
        Dim newForm2 = New Form2
        newForm2.MdiParent = Me
        newForm2.Show()
    End Sub
End Class
Public Class Form2
    Public Sub New()
        ' This call is required by the designer.'
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.'
    End Sub
    'The click that opens Form3'
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newForm3 = New Form3(Me)
        newForm3.MdiParent = Me.MdiParent
        newForm3.Show()
    End Sub
End Class  
Public Class Form3
    Dim targetForm As Form2
    'The constructor that accepts an instance of Form2 so that Form3 can
    'access the controls.'
    Public Sub New(target As Form2)
        ' This call is required by the designer.'
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.'
        targetForm = target
    End Sub
    'The click that moves the info from Form3 to Form2 and closes the
    'instance of Form3'
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        targetForm.TextBox1.Text = TextBox1.Text
        Close()
    End Sub
End Class

If you're passing information from a lot of controls, name them the same then just loop through the controls and populate the ones on Form2 with the same name

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.