formMain.exitApp() can't work
inside form2, when the exit button is clicked, it will call
formMain's exitApp()
Form2's Code:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileExit.Click
Dim formMain1 As frmMain
formMain1.exitApp()
End Sub
FormMain1's Code:
Public Sub exitApp()
Dim dlg As DialogResult
dlg = MessageBox.Show("Exit?")
If dlg = DialogResult.Yes Then
Me.Close()
End If
End Sub
However the error given is:
Object reference not set to an instance of an object! :(
Re: formMain.exitApp() can't work
1. Try to use code tags.
2. Try this:
VB Code:
Dim formMain1 As new frmMain()
eDIT:
And is it frmMain or frmMain1. Create instance of the appropriate form
Re: formMain.exitApp() can't work
Quote:
Originally Posted by john83
However the error given is:
Object reference not set to an instance of an object! :(
That is the error in your code precicly.
To use that method, you must apply it to an INSTANCE of that form, not the class like you are doing.
Do you must find where you hold an instance of frmMain and then call the exitApp() method on that instance.
rjv_rnjn's code wouldn't work because it creates a new instance, then closes that instance, but it still doesn't solve the problem.
Re: formMain.exitApp() can't work
Quote:
Originally Posted by Ideas Man
rjv_rnjn's code wouldn't work because it creates a new instance, then closes that instance, but it still doesn't solve the problem.
I think I've tried the same thing as you've suggested. The code piece written above was to create an instance of the 'frmMain' and then call the Public function subsequently. Where was the object destroyed? Or is it something that I'm missing totally?
Re: formMain.exitApp() can't work
I think you're missing it completely. His method is in what appears to be the start up form. Using your method, all you are doing is creating a new instance of the form, then closing it, effectively doing nothing. This is because you are referencing the method in the new instance of the form, not the current instance,
Re: formMain.exitApp() can't work
Hi
If you look at the posted code
VB Code:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileExit.Click
Dim formMain1 As frmMain
formMain1.exitApp()
End Sub
There are a couple of errors.
A new instance called formMain1 has NOT been created.
If an earlier instance of formmain WAS created and is still open, you could refer to that instance directly, presumable as
[vbcode
formmain.exitApp()
[/Highlight]
or if you want to be longwinded and frmmain is an instance of formMain (your choice of form Main's in this thread is rather confusing)
VB Code:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileExit.Click
Dim formMain1 As form
formmain1 = frmmain
formMain1.exitApp()
End Sub
If there is no existing instance of formmain then you need to use NEW
VB Code:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileExit.Click
Dim formMain1 As New frmMain
formMain1.exitApp()
End Sub
But that would still not work because for
to work, frmmain would have to have been the startup object AND frm2 must not have been opened with Show.Dialog.
Your further comments are needed.