Newbie: access function of Form1 from Form2

S

steve

Hi,
As the subject says:

I open Form1 then open Form2 as Modal. Form1 is accessible after Form2 has
been killed.
How can I access a function defined in Form1 from Form2?

Thanx in advance
-steve
 
A

Armin Zingler

steve said:
Hi,
As the subject says:

I open Form1 then open Form2 as Modal. Form1 is accessible after
Form2 has been killed.
How can I access a function defined in Form1 from Form2?

Whenever you want to access an object, you need a reference. If you don't
have it you must make it accessible. This is usually done by passing it as a
property value or as a procedure argument.

Armin
 
S

steve

Yes, but as i said, Form 1 is "alive" in the background. The object exists.
Wouldnt it be an overkill to instantiate a new (non-visible) form1 as New
myProject.Form1 ?

Since it exists I just want to access its methods from form2.

If i had killed it and instantiated a Form2, I would understand.
 
A

Armin Zingler

steve said:
Yes, but as i said, Form 1 is "alive" in the background. The object
exists. Wouldnt it be an overkill to instantiate a new (non-visible)
form1 as New myProject.Form1 ?

Since it exists I just want to access its methods from form2.

If i had killed it and instantiated a Form2, I would understand.

I did not write that you should create a new instance. You must pass a
reference to the existing instance.

Armin
 
K

kevininstructor

I believe this is what others are saying... So in Form2 you would write
AppProgram.MainForm.YourFunction...

Imports System.Windows.Forms
....
Module StartUp
Friend m_MainForm As frmMainForm
Public Class DemoWinFormAtDOR
Inherits System.Windows.Forms.Form
<System.STAThread()> Public Shared Sub Main()
m_MainForm = New frmMainForm
System.Windows.Forms.Application.Run(m_MainForm)
End Sub
End Class
' part of my program class
Public Class AppProgram
Public Shared ReadOnly Property MainForm() As frmMainForm
Get
Return m_MainForm
End Get
End Property
End Class
end module
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top