简体   繁体   English

vb.net-如何多次使用SUB打开同一表格

[英]vb.net - how to open the same form multiple times USING A SUB

You probably think this is a duplicate of other questions about this. 您可能认为这与其他有关此问题的重复。 But it isn't 但这不是

I have seen several different questions about this but none of them answer this. 我已经看到了几个与此有关的问题,但没有一个回答。

I would to open a form multiple times USING A SUB. 我会多次使用SUB打开一个表单。 Where I pass in a Form and it creates a new instance of this form. 我在其中传递Form ,它创建了该表单的新实例。 However I keep having problems with the Form being disposed if I run it twice 但是,如果我两次运行该Form ,就会遇到处理Form问题

This is the sub that does not work: 这是无效的子项:

Public Shared Sub OpenProgram(ByRef formToOpen As Form)
    Dim newApp As Form
    newApp = formToOpen
    Try
        newApp.Show()
        newApp.BringToFront()
    Catch ex As Exception
        MsgBox("Unable to open program :(")
    End Try
End Sub

I have also tried Dim newApp As New Form and 我也尝试过将Dim newApp As New Form
Dim newApp As New formToOpen however none of these work Dim newApp As New formToOpen但是这些都不起作用

I only have the problem if I try and run the sub, then close the form, then run the sub again. 我只有在尝试运行子程序,然后关闭窗体,然后再次运行子程序时才遇到问题。

The error I get is: 我得到的错误是:

Can not access a disposed object 无法访问已处置的对象
Object Name: 'shop' 对象名称:“商店”

shop is just a form shop只是一种形式

Thanks in advance 提前致谢

Option one, with generics : 选项一, 具有泛型

Public Function OpenProgram(Of F As {Form, New})(ByVal ThisOne As F) As F
    Return New F()
End Function

Option two, without generics : 选项二, 不带泛型

Public Function OpenProgram(ByVal ThisOne As Form) As Form
    Return CType(Activator.CreateInstance(ThisOne.GetType()), Form)
End Function

Usage: 用法:

Dim newApp As Form = OpenProgram(shop)
newApp.Show

Given a master form with 2 buttons and the following code, and two other forms, the buttons can be clicked repeatedly to open multiples of the forms, and closing them has no effect on opening more. 给定一个具有2个按钮和以下代码的主窗体,以及两个其他窗体,可以重复单击这些按钮以打开多个窗体,而关闭它们不会影响打开更多窗体。

Public Class MasterForm
    Private Sub btnOpenForm1_Click(sender As Object, e As EventArgs) Handles btnOpenForm1.Click
        ShowForm(New Form1)
    End Sub

    Private Sub btnOpenForm2_Click(sender As Object, e As EventArgs) Handles btnOpenForm2.Click
        ShowForm(New Form2)
    End Sub

    Private Sub ShowForm(WhichForm As Form)
        With WhichForm
            .Show()
            .BringToFront()
        End With
    End Sub
End Class

在此处输入图片说明

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim x As String = Application.ExecutablePath

            Process.Start(x)

        Catch ex As Exception
            MsgBox("Executable path does not exist", MsgBoxStyle.Information, "New Window")
        End Try
    End Sub

Your "closed " form is not closed until you dispose of it, it is just not shown. 您的“已关闭”表单在您处理之前不会关闭,只是不会显示。 You need to loop through your forms and show the form that you want to view again. 您需要遍历表单并再次显示要查看的表单。 You will probably use something like this - 您可能会使用类似这样的内容-

Private Sub cmdOpenForm_Click(sender As Object, e As EventArgs) Handles  cmdOpenForm.Click

    Dim frmCollection = System.Windows.Forms.Application.OpenForms
    If frmCollection.OfType(Of frmShop).Any Then
        frmCollection.Item("frmShop").Activate()
    Else
        Dim frmShop As New frmShop
        frmShop.Show()
    End If

    Me.Close()
    Me.Dispose()
End Sub

The close and dispose part is pointing to the form you are currently in. As forms are objects you need to activate them again for them to be used or you need to recreate them again to show a new instance of that form. close和dispose部分指向您当前所在的表单。由于表单是对象,因此您需要再次激活它们才能使用它们,或者您需要重新创建它们以显示该表单的新实例。

Hope this is what you were looking for. 希望这就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM