GeniuS BraiN GeniuS BraiN - 6 months ago 23
Vb.net Question

How to dynamically access members of the owner form

In a Winforms application, consider the following Form Class:

Public Class frmOwner
Public MyProperty As String = "Test"
Private Sub btnShowDialog_Click(sender As Object, e As EventArgs) _
Handles btnShowDialog.Click
Using frm As New frmDialog
frm.ShowDialog(Me)
End Using
End Sub
End Class


Now, whenever I want to access
MyProperty
from the dialog form, I use something like this:

MessageBox.Show(DirectCast(Me.Owner, frmOwner).MyProperty) 'Test


So far so good. The problem comes when I have another form (e.g.
frmOwner2
) that also has the member
MyProperty
and it also shows an instance of
frmDialog
.

My question is: how can I determine -from
frmDialog
- which form is the owner to be able to access its public members?

Answer

If You want to reach the owner's certain public properties from the dialog, but the owner might be different classes, then you can choose two different approach: 1., class inheritance 2., implement interface.

In both cases, You cast to the appropriate parent or interface and then call the property name of that object.

Another way could be using Reflection, but in Your case that would be a cannon against little birds.

1., Class inheritance

You define Your wished public properties in a parent form. Then You derive the other forms from this parent using Inherits ParentFormdirective.

Public Class frmOwnerBase

  Protected _MyProperty1 As String
  Public Property MyProperty1() As String
    Get
      Return _MyProperty1
    End Get
    Set(ByVal value As String)
      _MyProperty1 = value
    End Set
  End Property

  Protected _MyProperty2 As String
  Public Property MyProperty2() As String
    Get
      Return _MyProperty2
    End Get
    Set(ByVal value As String)
      _MyProperty2 = value
    End Set
  End Property

End Class

Then in the frmOwner1.Designer.vb and frmOwner2.Designer.vb files You re-write the ancestor:

Partial Class frmOwner1
  Inherits frmOwnerBase

Partial Class frmOwner2
  Inherits frmOwnerBase

2., Interface implementation

You define the type and name of the properties in an interface. Then in the frmOwner and frmOwner2 classes You apply Implements InterfaceName at their declarations. Then You define Your properties with the directive Public MyProperty As String Implements InterfaceName.MyProperty

Public Interface InterfaceOwner

  Property MyProperty1() As String

  Property MyProperty2() As String

End Interface
Public Class frmOwner
  Implements InterfaceOwner
  Private _MyProperty1 As String
  Public Property MyProperty1() As String Implements InterfaceOwner.MyProperty1
    Get
      Return _MyProperty1
    End Get
    Set(ByVal value As String)
      _MyProperty1 = value
    End Set
  End Property

  Private _MyProperty2 As String
  Public Property MyProperty2() As String Implements InterfaceOwner.MyProperty2
    Get
      Return _MyProperty2
    End Get
    Set(ByVal value As String)
      _MyProperty2 = value
    End Set
  End Property

End Class