0

我最近对接口以及在默认状态下使用它们之外进一步自定义它们的能力非常感兴趣。

我一直在专门研究 IList(of T)。与 ArrayLists 相比,使用泛型列表的优势让我大吃一惊。这是一张测试的照片。这是进一步解释测试的站点。

所以,很自然地我想尝试一下。当我第一次使用 ForNext 方法遍历列表时,代码工作正常。第二次我无法访问列表中表单的名称,因为它已被释放。任何人都知道如何访问列表中的表单属性。

Public Class frmMain

    Dim Cabinet As List(Of Form) = New List(Of Form)
    Dim FormA As New Form1
    Dim FormB As New Form2
    Dim FormC As New Form3

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles _Me.Load

        Cabinet.Add(FormA)
        Cabinet.Add(FormB)
        Cabinet.Add(FormC)

    End Sub



    Sub displayForm(ByVal aForm As Form)

        Dim myFormName As String = ""

        Stopwatch.Start()

        If aForm.IsDisposed = False Then
            aForm.Show()
        Else
            myFormName = aForm.(How do I access this objects Name?) 
            aForm = New Form  '<----- I would rather simply use aForm = New(aForm) 
            aForm.Name = myFormName
            aForm.Show()

        End If

        Stopwatch.Stop()

        Dim RealResult As Decimal = (Stopwatch.ElapsedMilliseconds / 1000)

        Debug.WriteLine(RealResult)

        Stopwatch.Reset()

    End Sub


    Private Sub btnForEach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForEach.Click

        'Dim instance as List 
        'Dim action as Action(of T) 

        'instance.ForEach(action) 
        'action = delegate to a method that performs an action on the object passeed to it

        Cabinet.ForEach(AddressOf displayForm)
    End Sub

我真的不明白为什么如果VB知道这是一个通用列表,这意味着它知道列表的类型,并且对象都被限制为表单;为什么我不能在列表中的项目上调用构造函数。前任。aForm = 新 aForm 或 aForm = 新内阁.aForm

撕开这个给我的人。谢谢。

4

2 回答 2

2

您不能构造“aForm”的新实例,因为它不是类型,它是 Form 类型的实例。

如果您想阻止 ObjectDisposedException,您可以隐藏表单而不是关闭它。将以下代码放在每个表单代码后面:

Public Class Form1

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Dim form = CType(sender, Form)
        form.Visible = False
        e.Cancel = True
    End Sub

End Class

但是,这有点 hacky,但是您不需要Else块中的代码。

编辑 你可以试试这个:

Private Sub displayForm(ByVal aForm As Form)

    Dim indexOfCab As Integer = Cabinet.IndexOf(aForm)
    If indexOfCab <> -1 Then

        If aForm.IsDisposed Then
            aForm = CreateForm(aForm.GetType())
            Cabinet(indexOfCab) = aForm
        End If

        aForm.Show()
    End If

End Sub

Private Shared Function CreateForm(formType As Type) As Form
    Return CType(Activator.CreateInstance(formType), Form)
End Function

你不需要那么大的Select声明。

于 2012-08-26T19:27:34.257 回答
0

这是我能够让它工作的唯一方法。然而,我觉得它的效率极低,希望有人能让我走上一条更好的方法来做到这一点。以下是我想要实现的目标。

子 displayForm(ByVal aForm As Form)

   Dim myFormName As String = ""

   If Cabinet.Contains(aForm) Then

       Dim indexOfCab As Integer = Cabinet.IndexOf(aForm)
       Dim ObjForm As Form = Cabinet.Item(indexOfCab)

       If aForm.IsDisposed Then

           Select Case indexOfCab

               Case 0
                   aForm = Nothing
                   aForm = New Form1
                   Cabinet.Item(indexOfCab) = aForm
                   Cabinet.Item(indexOfCab).Show()
               Case 1
                   aForm = Nothing
                   aForm = New Form2
                   Cabinet.Item(indexOfCab) = aForm
                   aForm.Show()
               Case 2
                   aForm = Nothing
                   aForm = New Form3
                   Cabinet.Item(indexOfCab) = aForm
                   Cabinet.Item(indexOfCab).Show()
           End Select

       Else
           Cabinet.Item(indexOfCab).Show()
       End If

   End If

结束子

于 2012-08-26T22:45:29.797 回答