0
  • given below is the code i wrote

    Private Sub CloseTransactionForms()
      Dim ActiveFroms As New List(Of String)
      Dim formToClose As New List(Of Form)
      Dim j As Integer
      ActiveFroms.Add("FrmSale")
      ActiveFroms.Add("FrmpPurchase")
      ActiveFroms.Add("FrmSaleReturn")
      ActiveFroms.Add("FrmPurchaseReturn")
        Try
          For Each frm As Form In My.Application.OpenForms
            For j = 0 To ActiveFroms.Count - 1
                If frm.Name.ToString() = ActiveFroms.Item(j) Then
                    formToClose.Add(frm)
                End If
            Next
        Next
        If formToClose.Count > 0 Then
            Dim i As Integer
            For i = 0 To formToClose.Count - 1
                Dim xform As Form = formToClose.Item(i)
                xform.Close()
            Next
        End If
      Catch ex As Exception
     End Try
    End Sub
    
  • this code will iterate through the open forms in my application and close the defined forms from the application

  • but it seems not good for me (using 3 for loops in it and it took sometimes while iterating via for loop) i think there will be another good method, please suggest a good solution for me

Note : i have already seen this question in SO

Community
  • 1
  • 1
  • This looks like a duplicate of the question you linked, and additionally unclear what you're asking. Why does the code seem "not good for you"? You might be able to filter forms more elegantly using LINQ ([IEnumerable.Any](http://msdn.microsoft.com/en-us/library/vstudio/bb337697(v=vs.100).aspx)), but that doesn't have much to do with closing forms. – vgru Jul 21 '14 at 12:17
  • duplicate > not its not because in my case i need to close only 4 forms from the `openfoms` .............. not good for you > as you can see i have used 3 `for loop` in that code –  Jul 21 '14 at 12:19
  • You modify the collection that you are iterating. That always ends up poorly. You must iterate it backwards from Count-1 to 0 to avoid accidents. – Hans Passant Jul 21 '14 at 12:24
  • @Hans: he's actually creating a separate list of forms to close. – vgru Jul 21 '14 at 12:25
  • Okay, no idea what "vlose" was supposed to mean :) – Hans Passant Jul 21 '14 at 12:27
  • Apparently, it's about vlosing some active froms. – vgru Jul 21 '14 at 12:29

1 Answers1

2

You can use LINQ to find the forms you want to close and List.ForEach to close them:

Dim ActiveFroms = New List(Of String) From {"FrmSale", "FrmpPurchase", "FrmpSaleReturn", "FrmPurchaseReturn"}

Dim formsToClose = From form In My.Application.OpenForms.Cast(Of Form)()
                   Join activeFormName In ActiveFroms
                   On form.Name Equals activeFormName
                   Select form

formsToClose.ToList().ForEach(Sub(form) form.Close())

You cannot use a For Each with the query above because Form.Close will modify the collection which is not allowed. Therefore i use List.ForEach.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I almost finished writing the same thing so I took the liberty of adding the initializer part to your answer. – vgru Jul 21 '14 at 12:27
  • @TimSchmelter does `formsToClose.ToList().ForEach(Sub(form) form.Close())` is correct?? am getting `Expression Expected` on [sub](http://127.0.0.1) –  Jul 22 '14 at 05:07
  • Which version of Vb.Net are you using? – Tim Schmelter Jul 22 '14 at 05:19
  • @TimSchmelter `Visual Studio 2008` `(VB 9.0)` –  Jul 22 '14 at 08:24
  • @keet: then [lambda-expressions](http://msdn.microsoft.com/en-us/library/bb531253(v=vs.90).aspx) are already supported. It works for me, i've tested it. Are you sure that you get the compiler error at `... ForEach(Sub ...`? However, as a workaround you can also use `Dim listOfForms=formsToClose.ToList()`. Then use a simple `For`-loop. – Tim Schmelter Jul 22 '14 at 08:58
  • @TimSchmelter Yes am sure ...anyways i'll try your suggestion –  Jul 22 '14 at 09:11
  • @keet: i cannot test it now, but i'll check later if it works also for me if i compile with .NET 3.5 (i've tested it yesterday with VS 2010). – Tim Schmelter Jul 22 '14 at 09:43