【问题标题】:Open the same form more than once多次打开同一个表单
【发布时间】:2011-07-17 05:28:10
【问题描述】:

是否可以多次打开表单?

按钮1 form2.show

按下按钮1 form2 打开

再次按下按钮1 另一个 form2 在旧 form2 旁边打开

如果可能,Form1 上的按钮能否杀死所有打开的 Form2 窗口?

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:

    当然可以。只需将相同表单的两个实例变暗即可。

    Public Class Form1
    
        Private m_WindowList As New List(Of Form2)
    
        Private Sub OpenWindowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenWindowButton.Click
            OpenWindow()
        End Sub
    
        Private Sub CloseWindowsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseWindowsButton.Click
            CloseWindows()
        End Sub
    
        Private Sub OpenWindowsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenWindowsButton.Click
            Dim WindowCount As Int32
            If Int32.TryParse(WindowCountTextBox.Text, WindowCount) Then
                OpenWindows(WindowCount)
            End If
        End Sub
    
        Private Sub OpenWindow()
            Dim NewWindow As New Form2
            m_WindowList.Add(NewWindow)
            NewWindow.Show()
        End Sub
    
        Private Sub OpenWindows(ByVal Count As Int32)
            For i = 1 To Count
                OpenWindow()
            Next
        End Sub
    
        Private Sub CloseWindows()
            For Each Window In m_WindowList
                Window.Close()
                Window.Dispose()
            Next
            m_WindowList.Clear()
        End Sub
    
    End Class
    

    【讨论】:

    • 对象引用未设置为对象的实例。该行的 m_WindowList.Add(NewWindow) 问题
    • 啊,对不起,在 m_WindowList 的声明中添加 New。我已经在上面的答案中修复了它。
    • 嘿嘿,别问我为什么要这么做,但我把它清理干净并添加了你提到的额外的多开功能。
    【解决方案2】:
    Dim MyNewForm2 = New Form2
    MyNewForm2.Show
    

    【讨论】:

    • 只是出于好奇。有没有办法让一个按钮1打开多少个窗口作为文本框中描述的数字?并且还要关闭所有 form2 以禁用那里的工作?
    • @SirAudens,有几种方法可以实现它,一种简单的方法是在本地数组中保留对所有新窗口的引用,然后在第二个按钮处单击关闭/全部杀死它们。
    • 顺便说一句,如果解决了您的问题,请不要忘记将帖子标记为答案。
    • TY 正是我想要的。我希望我能有超过 1 个被接受的答案,因为你有电话帮助我。但遗憾的是,电话有更多我需要的信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多