Results 1 to 19 of 19

Thread: [RESOLVED] [02/03] close all forms except one [VB 2003]

  1. #1

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Resolved [RESOLVED] [02/03] close all forms except one [VB 2003]

    Hi,

    How I can close all the forms opened of my application except a form that I do not want to close?

    regards
    Last edited by anna7; Mar 19th, 2007 at 11:19 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] close all forms except one [VB 2003]

    How are you creating/opening them? Since there is no forms collection in 2003 you will have to either close them manually, when tracking with public object variables, or when you close the parent form it will close the child form(s) automatically.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [02/03] close all forms except one [VB 2003]

    U can use an arraylist containning all your opened forms and then when needed search the array to close all the unwanted forms.
    U can create a module and make your array public in there for all the forms

  4. #4
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [02/03] close all forms except one [VB 2003]

    I'm using visual basic 2005

    here is an example of what I said earlier
    Attached Files Attached Files

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] close all forms except one [VB 2003]

    Yes, public object variables like I posted earlier will help you track what forms are open and give you the ability to close which one(s) you want.

    Note, 2005 projects are not openable by 2003. You will have to use notepad or other text editor to just view the code and not able to run it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] close all forms except one [VB 2003]

    Note also that closing your startup form will exit the application, so unless this one form you don't want to close is the startup form you're going to have to tap dance a bit to get it to work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: [02/03] close all forms except one [VB 2003]

    Thanks to all for answering.

    Note, 2005 projects are not openable by 2003. You will have to use notepad or other text editor to just view the code and not able to run it.
    There is no problem, also I have vb 2005.

    here is an example of what I said earlier
    Talkro, thank you for your contribution. I will check your test project and I try it.

    regards

  8. #8

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: [02/03] close all forms except one [VB 2003]

    Now I try it and it runs good.

    I am very glad for your contribution. I have implemented it in my application, modifying some aspect, and it works perfectly.

    Thank you very much to all.

    Regards

  9. #9

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: [02/03] close all forms except one [VB 2003]

    Sorry, I have a problem.

    When I can remove form from arrayList?

    Because Now I remove form from Array when form is disposed:

    For example:

    Code:
    Public Class frmEmpresas 
    
    #Region " Windows Form Designer generated code "
    
        Private Shared frmEmpEj As frmEmpresas = Nothing
    
        Public Shared Function Instance() As frmEmpresas
            If frmEmpEj Is Nothing Then
                frmEmpEj = New frmEmpresas
                addForm(frmEmpEj)
            End If
            frmEmpEj.Focus()
            Return frmEmpEj
        End Function 'Instance
    
        Public Sub New()
            MyBase.New()
         
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        removeForm(frmEmpEj)
            frmEmpEj = Nothing
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
    .
    .
    .
    End Class

    But when my Main Form of the app (MDI parent form) executes this code:

    Code:
     Dim i As Integer
                For i = 0 To formArray.Count - 1
                    Dim f As Form = CType(formArray(i), Form)
                    f.Close()
                Next
    
                formArray.Clear()
    Error: "Index out of the interval"

    regards,

  10. #10

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: [02/03] close all forms except one [VB 2003]

    please, any idea?

    Regards

  11. #11
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [02/03] close all forms except one [VB 2003]

    Have you tried using break points to see what the content of your formArray is?

    At waht value of i does it gives you the error?

  12. #12

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: [02/03] close all forms except one [VB 2003]

    Yes, because if I remove form from Array, this have less items.

    For example, If I add two forms in ArrayList, after when I executes this code:


    Code:
     Dim i As Integer
    'array have two items
      For i = 0 To formArray.Count - 1                
    Dim f As Form = CType(formArray(i), Form)
    f.Close() 'but if close the form, this remove from ArrayList. Now ArrayList only have one item. For this it fails.      
       Next
       formArray.Clear()
    When the form is closed:

    Code:
      removeForm(myForm)
    I hope that you have understood me, because my English is not very good.

    Regards

  13. #13
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [02/03] close all forms except one [VB 2003]

    be careful with a for loop on a dynamic size array, when the array is decreasing and i increasing, at some point i is greater than the size of the array and trying to access a value at that position in the array.

    try this

    vb Code:
    1. while formArray.count >= 0
    2.        dim f as form = ctype(formArray(i), form)
    3.        f.close()
    4. end while
    5.  
    6. formArray.Clear()

  14. #14

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: [02/03] close all forms except one [VB 2003]

    Hi talkro,

    I have tried this code but the same error continues displaying.

    regards

  15. #15
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: [02/03] close all forms except one [VB 2003]

    Are you using this array to store MDI Child forms? (You mentioned above that the only form remaining open is the MDI parent) If so you don't need to use an array, you should use the "MdiChildren" collection of the MDI Parent form.

    VB Code:
    1. For Each frmCloseure As Form In Me.MdiChildren
    2.     frmCloseure.Close()
    3.     frmCloseure.Dispose()
    4. Next

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  16. #16

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Exclamation Re: [02/03] close all forms except one [VB 2003]

    I have seen where the failure originates.

    I explain it.

    How I want to open different instances of the same child form from other child form:

    formChild1 (This one is a child form who opens other child forms)

    Code:
     Sub openFrm(ByVal nombre As String, ByVal accion As String, Optional ByVal id As Integer = -1)
            Try
                 For Each forma As Form In Me.ParentForm.MdiChildren
                    If forma.Text = nombre Then
                        forma.Focus()
                        Exit Sub
                    End If
                Next
                fNouEmpl = New frmNouEmpl
                addForm(fNouEmpl)          
      fNouEmpl.Tag = Guid.NewGuid().ToString
                mColFrm2s.Add(fNouEmpl, fNouEmpl.Tag)
                AddHandler fNouEmpl.Closed, AddressOf fNouEmpl_Closed
                fNouEmpl.MdiParent = Me.MdiParent
                fNouEmpl.Text = nombre
                If id <> -1 Then
                    fNouEmpl.idEmplM = id
                End If
                fNouEmpl.accEmpl = accion
                fNouEmpl.Show()
            Catch ex As Exception
                cl.shErr(ex.Message, Me.Text)
                cl.controlError(Me.Text, ex.ToString)
            End Try
        End Sub

    In the case that fNouEmpl is closed:

    Code:
       Private Sub fNouEmpl_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
            fNouEmpl.Dispose()
            Me.connGrid(condicio, condicio2)
            removeForm(fNouEmpl)
        End Sub
    If I close formChild1 before that fNouEmpl :

    Code:
    Private Sub frmEmpleados_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            For Each fNouEmpl In mColFrm2s
                'Use this if you want to leave the frm2's open but don't want them to fire the event any more 
                RemoveHandler fNouEmpl.Closed, AddressOf fNouEmpl_Closed
            Next
               End Sub
    until here runs ok.


    The problem is if I close formChild1 before fNouEmpl instances, because when I close the instances of fNouEmpl, it will not call "removeForm".

    I don't know how to solve this. Any idea?


    Regards

  17. #17

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: [02/03] close all forms except one [VB 2003]

    Quote Originally Posted by ProphetBeal
    Are you using this array to store MDI Child forms? (You mentioned above that the only form remaining open is the MDI parent) If so you don't need to use an array, you should use the "MdiChildren" collection of the MDI Parent form.

    VB Code:
    1. For Each frmCloseure As Form In Me.MdiChildren
    2.     frmCloseure.Close()
    3.     frmCloseure.Dispose()
    4. Next
    Yes! it runs perfectly

    Thanks a lot.

    Regards

  18. #18
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: [02/03] close all forms except one [VB 2003]

    I'm glad its working now

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  19. #19

    Thread Starter
    Hyperactive Member anna7's Avatar
    Join Date
    Sep 2005
    Location
    Catalonia
    Posts
    481

    Re: [RESOLVED] [02/03] close all forms except one [VB 2003]

    Hi,


    I have simplified the code to put it here.

    When I close fNouEmpl and formChil1d is not been closed , this one must execute a few functions.

    In the case that formChild1 was closed before fNouEmpl, I must delete the event, And I did not have this code it would display error.

    Sorry if I do not explain very well and because I cannot put here the all code.

    regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width