Results 1 to 6 of 6

Thread: formMain.exitApp() can't work

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    106

    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!

  2. #2
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: formMain.exitApp() can't work

    1. Try to use code tags.
    2. Try this:
    VB Code:
    1. Dim formMain1 As new frmMain()
    eDIT:
    And is it frmMain or frmMain1. Create instance of the appropriate form

  3. #3
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    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.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  4. #4
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    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?

  5. #5
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    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,
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: formMain.exitApp() can't work

    Hi

    If you look at the posted code

    VB Code:
    1. Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileExit.Click
    2. Dim formMain1 As frmMain
    3. formMain1.exitApp()
    4. 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:
    1. Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileExit.Click
    2. Dim formMain1 As form
    3. formmain1 = frmmain
    4. formMain1.exitApp()
    5. End Sub

    If there is no existing instance of formmain then you need to use NEW

    VB Code:
    1. Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileExit.Click
    2. Dim formMain1 As New frmMain
    3. formMain1.exitApp()
    4. End Sub

    But that would still not work because for

    VB Code:
    1. Me.Close()

    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.
    Last edited by taxes; May 12th, 2005 at 04:06 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width