How to call a sub in mdi parent from child form


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to call a sub in mdi parent from child form

  1. #1
    cat_dog Guest

    How to call a sub in mdi parent from child form

    i use mdi to manage several forms which one of the child form "frmJobList" contain a datagrid,
    how can i update another child form "frmNote" by clicking the datagrid only
    i use databinding method in "frmNote"

    The parent form is called "frmMain"

    There is a sub in "frmNote" which can update the bound textbox

    Public Sub MoveTo(ByVal intJobId As Long)
    SqlDvNote.Sort = "JobID"
    Me.BindingContext(SqlDvNote).Position() = SqlDvNote.Find(intJobId)
    End Sub

    There is another sub in "frmMain"

    Public Sub UpdateAllForms(ByVal intJobId As Long)
    If ChildFormExist("frmNote") Then
    chdNote.MoveTo(intJobId)
    End If
    End Sub

    i tried to call the sub in "frmMain" from the "frmJoblist" datagrid CurrentCellChanged event like below

    Public Sub dgJobList_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgJobList.CurrentCellChanged
    longJobID = dgJobList(dgJobList.CurrentRowIndex(), 0)
    dim frm as new frmMain
    frm.UpdateAllforms(longJobID)
    End Sub

    But it fails

    How can i refer to the parent from the child form?

    Hope u can understand

    Many thx
    Attached Files Attached Files

  2. #2
    IbrahimMalluf Guest

    Re: How to call a sub in mdi parent from child form

    Hello Cat_Dog

    Try this example
    Create an MDI form (named mdi) and add a groupbox docked to right
    Create a form (named form1) and add a command button
    Create another form (named form2) and add a label
    In MDI form load event:

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

    Dim MyForm As New Form1

    MyForm.MdiParent = Me

    MyForm.Show()

    Dim MyOtherForm As New Form2

    MyOtherForm.MdiParent = Me

    MyOtherForm.Show()

    End Sub

    In Form1: '

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim MyParent As mdi

    Dim MyOtherForm As Form

    Dim FoundForm As Form2

    MyParent = DirectCast(Me.ParentForm, mdi)

    For Each MyOtherForm In MyParent.MdiChildren

    If MyOtherForm.Text = "Form2" Then

    FoundForm = DirectCast(MyOtherForm, Form2)

    FoundForm.Label1.Text = "Hello World"

    End If

    Next

    End Sub

    Explanation:

    In the command button click event I dimenshion an object as an instance of the mdi form named MyParent, an object as a form2 object named FoundForm, and another object as a generic form object named MyOtherForm.

    I then instantiate the MDIParent object with a direct cast of the Form1's MDIParent reference. This gives you a reference to all available properties and methods of the MDI Parent.

    I then use a for-each construct to iterate through the MDIParent object's MDIChildren collection testing the TEXT property of each item in the collection for the form I desire. In a more robust application, you will have to develop a criteria that uniquely identifies the form you want to work with. For this example, this works

    Everytime a matching text property is found I then cast the instantiate the FoundForm object using the MyOtherForm object as the source. This gives you the target form with all of its exposed properties and methods.

    Hope this helps.

    Ibrahim











    "cat_dog" <x@hotmail.com> wrote in message news:3f251917@tnews.web.devx.com...
    i use mdi to manage several forms which one of the child form "frmJobList" contain a datagrid,
    how can i update another child form "frmNote" by clicking the datagrid only
    i use databinding method in "frmNote"

    The parent form is called "frmMain"

    There is a sub in "frmNote" which can update the bound textbox

    Public Sub MoveTo(ByVal intJobId As Long)
    SqlDvNote.Sort = "JobID"
    Me.BindingContext(SqlDvNote).Position() = SqlDvNote.Find(intJobId)
    End Sub

    There is another sub in "frmMain"

    Public Sub UpdateAllForms(ByVal intJobId As Long)
    If ChildFormExist("frmNote") Then
    chdNote.MoveTo(intJobId)
    End If
    End Sub

    i tried to call the sub in "frmMain" from the "frmJoblist" datagrid CurrentCellChanged event like below

    Public Sub dgJobList_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgJobList.CurrentCellChanged
    longJobID = dgJobList(dgJobList.CurrentRowIndex(), 0)
    dim frm as new frmMain
    frm.UpdateAllforms(longJobID)
    End Sub

    But it fails

    How can i refer to the parent from the child form?

    Hope u can understand

    Many thx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center