Reference between child window of MDI in vb 2005
The interface of my MDI Program:
My MDI program code source:
¡® This is the MDI main window (the container), use it¡¯s menu option to open the child window as many as you want.
Public Class frmMdi
Private Sub mNewSub1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mNewSub1.Click
Dim f As New frmSub1
f.MdiParent = Me
f.Show()
End Sub
Private Sub mNewSub2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mNewSub2.Click
Dim f As New frmSub2
f.MdiParent = Me
f.Show()
End Sub
End Class
¡® Child window 1. From here, want to call the function or excute the procedure in child window 2.
Public Class frmSub1
' Call the function named fSum in child window 2.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each f As Form In frmMdi.MdiChildren
If TypeOf f Is frmSub2 Then ¡® see Note
' I want to call fSum in f, something like this: textbox1.text=f.fSum(4)
' but it is no fSum object in f. How can explore the fSum object in frmSub2?
End If
Next
End Sub
¡®Note: In practice, I have to use another IF statement within this IF statement to locate one of the frmSub2
' Excute the procedure named pSum in child window 2.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Same problem, how to explore the pSum object in frmSub2 and can excute it here.
End Sub
End Class
¡® Child window 2. have the function and procedure being called or excuted by child window 1.
Public Class frmSub2
Friend Sub pSum(ByVal j As Integer)
Dim i As Integer, sum As Integer
For i = 1 To j
sum = sum + i
Next
TextBox1.Text = sum
End Sub
Friend Shared Function fSum(ByVal j As Integer) As Integer
Dim i As Integer
For i = 1 To j
fSum = fSum + i
Next
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = fSum(Val(TextBox1.Text))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call pSum(TextBox1.Text)
End Sub
End Class
|