VS 2010 Ultimate VB.NET Project form won't compile/show up
When press F5 to compile a project, there are no开发者_如何转开发 errors or warnings but the form won't show up. What's up?
Every time that you try to run your code, it starts by creating an instance of frmMain
, your default form and the one that is shown at application startup.
When this form is created, it automatically instantiates an instance of Form3
because you instantiate a variable of that type called modifyForm
at the top of this form's code file:
Dim modifyForm As New Form3 'modify student
The problem is that, when the runtime goes to instantiate an object of type Form3
, it gets called right back to where it was because of this statement at the top of the Form3
code file:
Dim frmMain As New frmMain
Rinse, lather, and repeat. Your code turns into an infinite loop, trying to instantiate an instance of frmMain
, which tries to instantiate an instance of Form3
, which tries to instantiate an instance of frmMain
, ad nauseum. Eventually, this will overflow your available memory and cause a StackOverflowException
.
It's important to note that all of this happens before the default instance of frmMain
is even shown, because these variables are declared outside of any methods in your code. Because the computer never can escape this infinite loop, it never gets a chance to move on and actually display your form.
And the moment you've all been reading so patiently for:
Fix it by removing the declaration of frmMain
at the top of the Form3
code file. I don't know what that's there for, anyway.
EDIT: Hopefully, I can clear up a little confusion regarding passing values between forms. Upon further study of your code, my instincts tell me that the best solution for you is to overload the constructor for Form3
to accept the calling form (the existing instance of frmMain
) as an argument. This will allow you to access all of the public members of frmMain
from within your Form3
class.
Here's a rough sketch of how you might do this in your code:
Public Class frmMain
''#The private data field that stores the shared data
Private _mySharedData As String = "This is the data I want to share across forms."
''#A public property to expose your shared data
''#that can be accessed by your Form3 object
Public Property MySharedData As String
Get
Return _mySharedData
End Get
Set(ByVal value As String)
_mySharedData = value
End Set
End Property
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
''#Do all of your other stuff here...
''#Create a new instance of Form3, specifying this form as its caller
Dim otherForm As New Form3(Me)
''#Show the new instance of Form3
otherForm.Show()
End Sub
End Class
Public Class Form3
''#The private field that holds the reference to the main form
''#that you want to be able to access data from
Private myMainForm As frmMain
Public Sub New(ByVal callingForm As frmMain)
InitializeComponent()
''#Save the reference to the calling form so you can use it later
myMainForm = callingForm
End Sub
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
''#Access anything that you need from the main form
MessageBox.Show(myMainForm.MySharedData)
End Sub
End Class
As you can see, frmMain
exposes a public property (backed by a correspondingly-named private variable) called MySharedData
. This can be absolutely anything you want, and you can have as many of these as you want.
Also notice that how the constructor (the New
method) for Form3
accepts an instance of frmMain
as an argument. Whenever you create a new Form3
object from frmMain
, you just specify Me
, which indicates that you want to pass the current instance of frmMain
. In the constructor method, Form3
stores that reference to its calling form away, and you can use this reference any time you like in Form3
's code to access the public properties exposed by frmMain
.
In VS2010 menu, go to Build -> Configuration Manager, does your project have the checkbox in the "Build" column enabled?
If it's project upgraded from an older Visual Studio version it may be that it is not targeting .NET Framework 4.0. In that case you should change it as explained here.
To analyze the problem press F8 (or F10, depends on your default keyboard settings) to step into the code instead of running the app. This should take you to the main method where the main form would be initialized.
精彩评论