简体   繁体   中英

Close all open forms except for some VB.Net

as the title, I am trying to close all open forms except some in VB.Net but the forms don't close. Here is the code I used:

Dim lista As New FormCollection
lista = Application.OpenForms
For Each a As Form In lista
    If Not a.Text = "formLogout" And a.Text = "arresto" And a.Text = "riavvio" And a.Text = "formOpen" Then
        a.Close()
    End If
Next
scrivania.Close()
Me.Close()

Grazie.

Same as @Fabio 's answer without the extra collection and loop.

    Dim keepOpen As New List(Of String) From {Me.Text, Form2.Text, Form3.Text}
    For index = Application.OpenForms.Count - 1 To 0 Step -1
        If Not keepOpen.Contains(Application.OpenForms(index).Text) Then
            Application.OpenForms(index).Close()
        End If
    Next

If statement will return true when all provided conditions are true , which is not possible because you compare same form.Text with different values.
Notice that in your example Not will be applied only for the first condition

You possibly can rewrite condition as follow:

If Not (form.Text = "formLogout" OrElse form.Text = "arresto") Then ..

Suggest to use a collection of form names, which should not be closed

Dim remainOpenForms As New HashSet(Of String)
remainOpenForms.Add("formLogout")
remainOpenForms.Add("arresto")

' Create collection of forms to be closed
Dim formsToClose As New List(Of Form)
For Each form As Form In Application.OpenForms
    If remainOpenForms.Contains(form.Text) = False Then formsToClose.Add(form)
Next

For Each form As Form In formsToClose
    form.Close()
Next

i know its to late but for some here is my answer

what this does is close all form except "exceptthisform"

     Dim formNames As New List(Of String)

        For Each currentForm As Form In Application.OpenForms

            If currentForm.Name <> "exceptthisform" Then

                formNames.Add(currentForm.Name)

            End If

        Next

        For Each currentFormName As String In formNames
            Application.OpenForms(currentFormName).Close()

        Next

I replaced form. toString with form. Name from Fabio's solution, and it worked like a charm!

    Try
        Dim remainOpenForms As New HashSet(Of String)
        remainOpenForms.Add("FormMainMenu")

        Dim formsToClose As New List(Of Form)
        For Each form As Form In Application.OpenForms
            If Not remainOpenForms.Contains(form.Name) Then
                formsToClose.Add(form)
                Debug.Print("closing: " & form.Name)
            Else
                Debug.Print("keep open: " & form.Name)
            End If
        Next

        For Each form As Form In formsToClose
            form.Close()
        Next

        Close()
    Catch ex As Exception
        WriteErrors("FMM: WAN2", ex.ToString)
    End Try

In case your wondering why I closed it after: I received an InvalidOperationException Collection was modified. This is my Exit application module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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