none
How to Call active child form method from parent Form in vb.net RRS feed

  • Question

  • How to Call active child form method from parent Form in vb.net?

    In Child Form1:

    public sub Prepare(byval id as integer)

    -------

    --------

    End sub

    In Child Form2:

    public sub Prepare(byval id as integer)

    -------

    --------

    End sub

    In Parent Form:

    Me.ActiveMdiChild.prepare(2)

    i got following error:

    prepare is not a member of system.windows.form

    Wednesday, March 5, 2014 2:28 PM

Answers

  • You can not call any function or procedure without refering form object you need to refer it by instance of form or by form.FunctionOrProcedureName in case of static function. In your case u can refer the form as

    If TypeOf(Me.ActiveMdiChild) Is form1 Then
    
    ctype(Me.ActiveMdiChild,Form1).prepare(2)
    
    End If
    


    Ashish Pandey


    Thursday, March 6, 2014 6:16 AM
  • Hi,

     You are going to have to know which form it is so you can cast it to that form. For example, here i know that the active form is Form2 so, i cast the activemdichild to Form2.

    Form1 code

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim F2 As New Form2
            F2.MdiParent = Me
            F2.Show()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim frm As Form2 = DirectCast(Me.ActiveMdiChild, Form2)
            frm.DoSomething(5)
        End Sub
    End Class
    

    Form2 code

    Public Class Form2
        Public Sub DoSomething(ByVal num As Integer)
            Me.Text = num.ToString
        End Sub
    End Class

    However, you could declare the forms in form1 as class scoped and just use that to access the sub like this.

    Public Class Form1
        Private F2 As Form2
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            F2 = New Form2
            F2.MdiParent = Me
            F2.Show()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            F2.DoSomething(3)
        End Sub
    End Class
    

     

    Wednesday, March 5, 2014 2:58 PM

All replies

  • Hi,

     You are going to have to know which form it is so you can cast it to that form. For example, here i know that the active form is Form2 so, i cast the activemdichild to Form2.

    Form1 code

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim F2 As New Form2
            F2.MdiParent = Me
            F2.Show()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim frm As Form2 = DirectCast(Me.ActiveMdiChild, Form2)
            frm.DoSomething(5)
        End Sub
    End Class
    

    Form2 code

    Public Class Form2
        Public Sub DoSomething(ByVal num As Integer)
            Me.Text = num.ToString
        End Sub
    End Class

    However, you could declare the forms in form1 as class scoped and just use that to access the sub like this.

    Public Class Form1
        Private F2 As Form2
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            F2 = New Form2
            F2.MdiParent = Me
            F2.Show()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            F2.DoSomething(3)
        End Sub
    End Class
    

     

    Wednesday, March 5, 2014 2:58 PM
  • Thanks for reply.Here i have many child forms.So i dont know which form is child form at runtime.For above explanation used for form2 is child form,but i have 10 forms are child form.So i need to create object for 10 forms.is there any other solution for above question? Thanks.
    Thursday, March 6, 2014 5:59 AM
  • You can not call any function or procedure without refering form object you need to refer it by instance of form or by form.FunctionOrProcedureName in case of static function. In your case u can refer the form as

    If TypeOf(Me.ActiveMdiChild) Is form1 Then
    
    ctype(Me.ActiveMdiChild,Form1).prepare(2)
    
    End If
    


    Ashish Pandey


    Thursday, March 6, 2014 6:16 AM