0

I'm playing around in vb.net and would like to create a menu for my application. The application has an Main which is the MDI Parent and then has the sub forms that open up as the user browses the menu structure. I would like to track which form the user came from so that when they close their current form, it opens up the previous form they had open.

What I have so far is a public variable "FormTracking" which is a list of Form. The open form function works fine, but when they close the form and it calls the "OpenPreviousForm" function it fails as the previous form has been closed/disposed. How can i create a new instance of the form as calling "NEW" doesn't seem to work

Public FormTracking As New List(Of Form)

Public Sub OpenForm(theNewForm As Object)
    'First Declare the new form
    Dim f As Form = DirectCast(theNewForm, Form)
    'Set the Forms parent
    f.MdiParent = frmMain2
    'Add the form to the tracking
    FormTracking.Add(theNewForm)
    'Show the form
    f.Show()
End Sub

Public Sub OpenPreviousForm()
    If modMenu.FormTracking.Count > 1 Then

        Dim f As New Form
        f = modMenu.FormTracking(modMenu.FormTracking.Count - 2)
        'Set the Forms parent
        f.MdiParent = frmMain2
        'Remove the last item
        modMenu.FormTracking.RemoveAt(modMenu.FormTracking.Count - 1)
        'Show the form
        f.Show()
    Else
        MessageBox.Show("Whoops, run out of forms.... ", "modMenu - OpenPreviousForm")
    End If
End Sub

When opening a form it it calls:

modMenu.OpenForm(menupage_1)
me.close

And when closing a form, i try to call

modMenu.OpenPreviousForm()

The error i am getting when calling the above code is

"An unhandled exception of type 'System.ObjectDisposedExeption' occured in System.Windows.Forms.dll
Additional Information: Cannot access a disposed object

I understand i need to create the object again, but not sure how to using the functionality as above. Alternatively, if someone can present a better way of doing an application menu, please feel free to let me know. Cheers

5
  • Dim f As New Form doesnt create an instance of a specific form, but the generic base class all forms are based on. Dim frm As New MainForm where "MainForm" is the form or class name in your project. The key is that each form is its own class or type as it shows at the top of each of them: Public Class MainForm Commented Jun 3, 2015 at 11:31
  • @Plutonix While true, that is not the cause of the exception. it's caused by the following code line that assigns the last (disposed) form to f. Commented Jun 3, 2015 at 11:34
  • @ZoharPeled I was answering the explicit question embedded in the first part How can i create a new instance of... Stephen, you also arent actually using your new form instance if the next line assigns a different for instance to it: f = modMenu.Form... The New instance is being replaced with something else. Commented Jun 3, 2015 at 11:36
  • @Stephen: Do you need to open the previous form in the last state it was, or do you need a new form that is from the type of the last form? Commented Jun 3, 2015 at 12:09
  • Hi @ZoharPeled, I am happy to open a new instance of the form as it's a menu structure, so the form only contains buttons. Commented Jun 3, 2015 at 21:12

1 Answer 1

0

Use Me.Hide instead of Me.Close so you do not dispose the form.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that this will show the previous form exactly as the user left it. While this would probably be the desired outcome, it contradicts Stepen's last comment on the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.