Getting a reference to the startup form?

C

Chris Ashley

I have a class called App set as the startup object with the following code:

Friend Class App
Shared Sub Main()
Dim FrmMain As New MainForm
Application.Run(FrmMain)
End Sub
End Class

In another form I use the following code:

Private Sub AddCustomer_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FrmMain.Text = "Add Customer"
End Sub

I get the error that FrmMain is not declared. Didn't I get a reference to it
called FrmMain in my Sub Main? How else can I get a reference to the startup
form?

Regards,

Chris
 
C

Chris Ashley

Chris Ashley said:
I have a class called App set as the startup object with the following
code:

Friend Class App
Shared Sub Main()
Dim FrmMain As New MainForm
Application.Run(FrmMain)
End Sub
End Class

In another form I use the following code:

Private Sub AddCustomer_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FrmMain.Text = "Add Customer"
End Sub

I get the error that FrmMain is not declared. Didn't I get a reference to
it called FrmMain in my Sub Main? How else can I get a reference to the
startup form?

Regards,

Chris

I sorted this by using a shared property in my startup class. Here's the
code I'm using now:

Public Class App
Private Shared m_FrmMain As MainForm
Public Shared Property FrmMain() As MainForm
Get
Return m_FrmMain
End Get
Set(ByVal Value As MainForm)
m_FrmMain = Value
End Set
End Property
<STAThread()> _
Shared Sub Main()
Dim FrmStart As New MainForm
FrmMain = FrmStart
FrmMain.ShowDialog()
End Sub
End Class

Now I can access my main form through App.FrmMain - was this the right way
to do this?

Regards,

Chris
 
C

Cor Ligthert

Chris,

I never use that classic startup method you show as something extra however
VB6 and C diehards like it.

Your mainform can in VBNet be the startup object it needs nothing more than
that you do not change the application settings and keep it with the name of
that form (class).

However there is nothing wrong with changing the name of that object and
than you have to change the startup object in the application properties.
(When you rename it, that goes not automaticly what you would suspect)

Solution explorere -> Application right click -> properties -> Startup
object and set the startup form.

I hope this helps?

Cor
 
K

Ken Tucker [MVP]

Hi,

You declared frmMain inside a subroutine. It is only available in
the subroutine.

Ken
--------------------
I have a class called App set as the startup object with the following code:

Friend Class App
Shared Sub Main()
Dim FrmMain As New MainForm
Application.Run(FrmMain)
End Sub
End Class

In another form I use the following code:

Private Sub AddCustomer_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FrmMain.Text = "Add Customer"
End Sub

I get the error that FrmMain is not declared. Didn't I get a reference to it
called FrmMain in my Sub Main? How else can I get a reference to the startup
form?

Regards,

Chris
 
H

Herfried K. Wagner [MVP]

Chris Ashley said:
I have a class called App set as the startup object with the
following code:

Friend Class App
Shared Sub Main()
Dim FrmMain As New MainForm
Application.Run(FrmMain)
End Sub
End Class

In another form I use the following code:

Private Sub AddCustomer_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FrmMain.Text = "Add Customer"
End Sub

I get the error that FrmMain is not declared.

Providing a reference to an application’s main form
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=accessmainform>
 

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