Set property of parent class from child

goldford

Member
Joined
May 6, 2009
Messages
12
Programming Experience
1-3
Hi,

I would like to have a form class that is called from a button on a parent form and would like a button on the child form to set a property on the parent form. Can anyone explain the best way to go about this or direct me to a good article?

In the parent class:
VB.NET:
Private Sub cmdChngLineCls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChngLineCls.Click

        m_sLayerType = "line"
        Dim MyForm As New Project.ChooseHabParam(m_sLineLayer, m_sLayerType)
        If MyForm.Form_Initialize(m_app) Then
            MyForm.ShowDialog()
        End If
    End Sub

One twist is the property in the parent class is a generic list(of T):
VB.NET:
 Private m_LLayersFields As List(Of LayerToAdd) = New List(Of LayerToAdd)
Public Property LHabParamList()
        Get
            Return m_LLayersFields
        End Get
        Set(ByVal value)

            Dim pLayerToAdd As LayerToAdd

            pLayerToAdd = value
            Dim sLayer As String = pLayerToAdd.Layer
            Dim comparer As New CompareLayerNamePred(sLayer)

            ' Need an object to hold returned object, if it exists...
            Dim CheckedLayerToAddObj As LayerToAdd
            Dim iIndex As Integer

            ' This find function will pass in the m_LLayersFields object to the comparerobject function
            ' which has its variable -_name- set above
            CheckedLayerToAddObj = m_LLayersFields.Find(AddressOf comparer.CompareNames)
            iIndex = m_LLayersFields.FindIndex(AddressOf comparer.CompareNames)

            ' If there is a match found then change the other parameters of the object
            With CheckedLayerToAddObj
                If .Layer IsNot Nothing Then
                    ' Change the fields to equal incoming object's
                    .ClsField = pLayerToAdd.ClsField
                    .QuanField = pLayerToAdd.QuanField

                    ' Now need to delete the old object and put it the modified one back into the list.
                    m_LLayersFields.Item(iIndex).ClsField = pLayerToAdd.ClsField
                    m_LLayersFields.Item(iIndex).QuanField = pLayerToAdd.QuanField

                Else 'Otherwise just add the object to the list
                    m_LLayersFields.Add(pLayerToAdd)
                End If
            End With

        End Set
    End Property

The object contained in the list is:
VB.NET:
Public Class LayerToAdd
    Public Layer As String
    Public QuanField As String
    Public ClsField As String

    Public Sub New(ByVal layer As String, ByVal QuanField As String, ByVal ClsField As String)
        Me.Layer = layer
        Me.QuanField = QuanField
        Me.ClsField = ClsField
    End Sub

End Class

In the child form I would create a new object LayerToAdd and populate its three fields and then pass this to the parent class property to add to the list. At least this is the way I think I'm going to go about it. Any advice?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,368
Location
Sydney, Australia
Programming Experience
10+
The child form would need a reference to the parent form, which the parent form would have to supply. The child form would need to provide a property of method parameter of the parent form's type that the parent form can pass Me to. The child form would then assign that reference to a member variable that it can refer to later.
 

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
VB.NET:
Private Sub cmdChngLineCls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChngLineCls.Click
        m_sLayerType = "line"
        Dim MyForm As New Project.ChooseHabParam(m_sLineLayer, m_sLayerType)
        If MyForm.Form_Initialize(m_app) Then
            MyForm.ShowDialog()
        End If
    End Sub

I would start by changing the new form code slightly, to :-

VB.NET:
Private Sub cmdChngLineCls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChngLineCls.Click

        m_sLayerType = "line"
        Using MyForm As New Project.ChooseHabParam(m_sLineLayer, m_sLayerType)
            MyForm.ShowDialog()
        End Using
    End Sub

This ensures that your form is disposed properly (assuming, that is, that you dont need this copy of the form again).

Just before the "End Using" line, your child form still exists - and all variables defined in it are still accessible. So rather than thinking of it as "set parent from child", try "parent reads from child". For example:-

VB.NET:
Private Sub cmdChngLineCls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChngLineCls.Click

        m_sLayerType = "line"
        Using MyForm As New Project.ChooseHabParam(m_sLineLayer, m_sLayerType)
            MyForm.ShowDialog()
            ValueYouWantToReadFromChild = MyForm.ChildVariable
        End Using
    End Sub
 
Top Bottom