referncing parent form

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I have opened a dialog (form) from within a form using the ShowDialog(). How
can I now access methods in the parent form, from this dialog?

Thanks

Regards
 
Hi John,

You'll have to cast it to the correct type. This will make its methods
visible to Intellisense and available to you.

Regards,
Fergus
 
Not sure how to do this. My main form is frmMain which calls frmdialog like
this;

Dim y As String
y = frmdialog.ShowDialog().ToString

How does frmdialog access frmMain methods?

Thanks

Regards
 
send a reference of frmMain to frmdialog

John said:
Not sure how to do this. My main form is frmMain which calls frmdialog like
this;

Dim y As String
y = frmdialog.ShowDialog().ToString

How does frmdialog access frmMain methods?

Thanks

Regards
 
Doen't matter how you are showing it, you can send a reference in several
ways....

First Method (which I prefer to use due to various advantages...but may not
work in all situations):

Create frmMain using a 'singleton' pattern, so that only a single instance
is created no matter how ever many times you instantiate the class.

Second Method:

Create a new consturctor which takes frmMain as an argument

Public Sub New(ByVal frm As frmMain)

MyBase.New()

InitializeComponent()

End Sub

Third Method:

Declare a property in frmDialog which takes frmMain as value.

Good Luck!
 
Hi

I have tried this;

x = DirectCast(Me.Owner, frmClients).txtCompany.Text ' frmclients is the
design name of the calling form

and I am getting a 'Object reference not set to an instance of an object.'
error. What am I doing wrong?

Basically I ma trying to pick up the value of the company in the calling
form, from within the called dialog form. Is there another way to pass data
between a calling and a called from?

Thanks

Regards
 
Hi John,

I don't believe there's an obligation for the calling Form to call
ShowDilaog. It can call the secondary Form and ask it to show <itself>.

This, then, provides a method on the secondary Form that allows you to
pass everything and its dog to the dialogue. It also allows the Dialogue Form
to return results.

Class DlgForm
Public Function ShowYourself _
(ItsDog As Dog) As SomeResultType
'Do something with ItsDog... (Save or use)
Me.ShowDialog 'This calls DlgForm_Load

'Package up the results and send them back.
Dim oResult As SomeResultType
oResult.This = SomeValue
oResult.That = SomeOtherValue
Return oResult
End Sub

In FormMain
Dim frmDlg As new DlgForm
Dim oResult As SomeResultType _
= frmDlg.ShowYourself ("Hello")
'oResult now contains juicy goodies.

Regards,
Fergus
 
Back
Top