Closing multiple new form [VB.NET]

2019-09-11 15:12发布

I have a problem with my sw: i have three forms: the main form(A), a secondary form (B) and third form (C). The form A must be always visibile and usable, the form B opening from form A and the form C opening form the form B. I need to open multiple instance of form C (i do not know how much instance are), i'm using this code in a command button on form B:

            Dim newform As New modifica_normale
            newform.LoadOrders(commessa_da_modificare, id_da_modificare, False)
            newform.Show()

When the form B is close i need to close all of multiple instance of form C, so in the closing event i've tried to put:

            modifica_normale.Close()

but do not work. I know that i can define newform as global variable and call newform.close(), but i don't know the number of newform that the user will open.

Thanks, Pietro.

标签: vb.net
1条回答
叛逆
2楼-- · 2019-09-11 15:41

This code will only close the common instance of the form and not each newly instantiated form object:

 modifica_normale.Close()

Instead you could try adding the forms to a forms collection as you create each form. Create a form level list on form B.

 Dim currentForms As List(Of Form) = New List(Of Form)

Add to it each time an instance of form C is created

Dim fNew As New modifica_normale
currentForms.Add(fNew)

When you want to close them, loop back through the forms collection closing each instance.

For Each frmCheck As Form In currentForms
    frmCheck.Close
Next
查看更多
登录 后发表回答