Dear All,

I am new to VB2010 and writing a MDI child form to open another form as dialogbox, then pass a value back to the MDI child form, the value is successfully passed back to the MDI child form but it can't be shown on a text box, my code as below, can somebody help on this

(on mdiForm1)

Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
Dim F As New Form
F = Form2
F.ShowDialog()
End Sub

Public Sub setText(ByVal nsc As String)
MessageBox.Show(nsc) 'it shows the correct value on screen
txtStaffCode.Text = nsc
End Sub

(on Form2)

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim nsc As String = lblNewCode.Text
Call mdiForm1.setText(nsc)
Me.Close()
End Sub

you can use that code

lblNewCode.Text = mdiForm1.txtStaffCode.Text

Thanks for your reply, Fa3hed!
Sorry that I have confused, should I put your code on Form2? But I need to pass the value from Form2 to the mdiForm1. And I also test the code below on mdiForm1, but not work neither.

Me.txtStaffCode.Text = Form2.lblNewCode.Text

attach your project

here is my project

the mdi form name is "frmAdminMainMenu"
the value will be passed back from "frmAddNewStaffCode"

Suppose you are having two forms example..form1 and form2 both contain textbox...
abd you want to pass values from textbox of form1 to textbox of form2
You have to First Of All have to load Form2 and should enter following code...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              Form2.Show()
        Form2.TextBox1.Text = Me.TextBox1.Text
    End Sub


Similarly you can do for MDI form

the solution doesn't work because i am using vb 2008

@Fa3hed : Sorry,not getting you.......

you made your project in vb2010 i am using vb2008

Suppose you are having two forms example..form1 and form2 both contain textbox...
abd you want to pass values from textbox of form1 to textbox of form2
You have to First Of All have to load Form2 and should enter following code...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              Form2.Show()
        Form2.TextBox1.Text = Me.TextBox1.Text
    End Sub


Similarly you can do for MDI form

Yes, you are correct, I want to pass value from textbox of form1 to mdichild form2.
Firstly, on mdichild form2, it's included a button to show the form1 by the code below

form1.ShowDialog()

Secondly, on form1, it will do some calculation and show the result on a textbox, and it will then pass the value to the mdichild form2 and show the value on a textbox whick in the mdichild form2 when I click the button on form1, I'm using send ByVal to passed the value back to mdichild fomr2

I tried to using the same coding on both standard window form instead of using mdichild form, it works fine and the value can be shown on the textbox in form2. I really don't know what's wrong when using mdichild form

the solution doesn't work because i am using vb 2008

I cannot find a function to convert my project to vb 2008, sorry

you made your project in vb2010 i am using vb2008

Hi there! I just created a new project in vb 2008 that with the same problem, you should able to open it :)

In your context only Form1 is a child form....
Am I right.....

See weather its working or not....
its in 2010...

commented: nice1 +0

In your context only Form1 is a child form....
Am I right.....

yes

Is that attached project working....

Is that attached project working....

yes, that's work, once you running the project, a MDIParent form will be shown on the screen, click the button marked with "Form1" on the toolstrip to open the MDIChild form "Form1", an "Add" button and a textbox on "Form1", you can click the "Add" button to open the "Form2" as a dialog, then you can type a word on the textbox which on "Form2" and click the "OK" button, it should then send the value back to "Form1" and placed in the textbox which in "Form1"

PS. the value was passed back to "Form1", I can use the Message.Show(a) to prompt the value on screen, but the value could not be placed in the textbox.

I have fixed it, finally, I changed the coding on Form2 as below, thanks guys!

Public Class Form2
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim r As String = txtResult.Text
Dim f As Form
For Each f In MDIParent1.MdiChildren
If f.Name = "Form1" Then
Dim a As Form1 = f
Call a.setText(r)
a.Focus()
End If
Next
Me.Close()
End Sub
End Class

commented: v.good your friend fa3hed +1

Ok....
I thought you want to pass value from Form1->MDIChild to Form2->Not an MDIchild...
The Code in The attached WindowsApplication1 works on this context and not vice versa...
any ways you have got the code you want....

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.