VB.NET按变量调用表单

时间:2015-06-03 11:22:16

标签: vb.net forms

我在vb.net玩游戏,想为我的应用程序创建一个菜单。应用程序有一个Main,它是MDI Parent,然后是用户浏览菜单结构时打开的子表单。我想跟踪用户来自哪个表单,这样当他们关闭当前表单时,它会打开他们打开的上一个表单。

到目前为止我所拥有的是一个公共变量“FormTracking”,它是一个Form列表。打开表单函数工作正常,但当它们关闭表单并调用“OpenPreviousForm”函数时,它会失败,因为前一个表单已被关闭/处理。如何创建表单的新实例,因为调用“NEW”似乎不起作用

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

打开表单时,它会调用:

modMenu.OpenForm(menupage_1)
me.close

关闭表单时,我会尝试调用

modMenu.OpenPreviousForm()

调用上述代码时出现的错误是

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

我理解我需要再次创建对象,但不确定如何使用上述功能。或者,如果有人可以提供更好的方式来执行应用程序菜单,请随时告诉我。 干杯

1 个答案:

答案 0 :(得分:0)

使用Me.Hide代替Me.Close,这样就不会丢弃表单。