Question How To Dispose Existing Open Form & Open The Main Menu Form ?

mansoor.live

New member
Joined
Dec 11, 2013
Messages
4
Programming Experience
5-10
hello experts i am new in vb.net and don't have too much experience .

i have a scenario that :

form 1 = main menu
form 2 = daily transactions

i am using mdi-application

i am on daily transaction form and i have home button, when i press this home button it automatically close current (daily transaction) form and open main menu form ..

how can i achieve this.

i tried this code but it throw exception.

VB.NET:
Function checkForm()

        Dim fc As FormCollection = Application.OpenForms
        Dim FormFound As Boolean = False
        For Each frm As Form In fc
            If frm.Name = "main_Menu" Then
                frm.Focus()
                FormFound = True
            End If
            If frm.Name = "DailyTransactions" Then                
                FormFound = True
                frm.Dispose()
            End If
        Next
        If FormFound = False Then
            Dim f As New main_Menu()
            f.MdiParent = Me
            f.Show()
        End If

    End Function

Help Would Be Appriciated.
 
So, this main menu for is also a an MDI child, correct? Presumably you only want one instance of it, so it makes sense to use the default instance. In that case, the code to open the main menu or activate it if it's already open and then close the current form would be:
main_Menu.MdiParent = Me.MdiParent
main_Menu.Show()
main_Menu.Activate()

Me.Close()
By the way, 'main_Menu' is a pretty dodgy name. What's wrong with MainMenu or MainMenuForm? Be consistent with naming, e.g. don't start some class names with upper case and some lower and use underscores in some and not others. Underscores are only useful if you are using a single case, e.g. main_menu or MAIN_MENU. If you're using Pascal casing then the upper-case characters indicate the start of the words so the underscores serve no useful purpose.
 
dear one, i tried the above code as if you see the below given code after next i am calling main_menu. but it throw exception. which is the main problem.
i wanted to close the "DailyTransaction" and open main_menu at the same time.


So, this main menu for is also a an MDI child, correct? Presumably you only want one instance of it, so it makes sense to use the default instance. In that case, the code to open the main menu or activate it if it's already open and then close the current form would be:
main_Menu.MdiParent = Me.MdiParent
main_Menu.Show()
main_Menu.Activate()

Me.Close()
By the way, 'main_Menu' is a pretty dodgy name. What's wrong with MainMenu or MainMenuForm? Be consistent with naming, e.g. don't start some class names with upper case and some lower and use underscores in some and not others. Underscores are only useful if you are using a single case, e.g. main_menu or MAIN_MENU. If you're using Pascal casing then the upper-case characters indicate the start of the words so the underscores serve no useful purpose.
 
Last edited:
strange i can not able to attach an image in this forum ..

VB.NET:
Unhandlred exception has occurred in your application. if you click Continue, the application will ignore this error and attempt to continue. 
If you click Quit, the application will close immediately.

collection was modifie; enumeration operation may not execute.

This is the Message in Exception |

If only there was a way for us to know what that exception is.
 
strange i can not able to attach an image in this forum ..

VB.NET:
Unhandlred exception has occurred in your application. if you click Continue, the application will ignore this error and attempt to continue. 
If you click Quit, the application will close immediately.

collection was modifie; enumeration operation may not execute.

This is the Message in Exception |

That means that you are adding to or removing from a collection that you are enumerating with a For Each loop. You are enumerating the OpenForms collection and closing a form inside the loop. Closing a form removes it from the OpenForms collection, hence the issue. If you must do it that way then you should copy the OpenForms collection to an array first and then enumerate that. You can then close a form without affecting the list you're enumerating because, while closing the form will remove it from the OpenForms collection, it will not remove it from the array.
 
Can You Please Please Help Me Make An Example Of Any thing PLease. Please . As I New So I Can't .. Please....
That means that you are adding to or removing from a collection that you are enumerating with a For Each loop. You are enumerating the OpenForms collection and closing a form inside the loop. Closing a form removes it from the OpenForms collection, hence the issue. If you must do it that way then you should copy the OpenForms collection to an array first and then enumerate that. You can then close a form without affecting the list you're enumerating because, while closing the form will remove it from the OpenForms collection, it will not remove it from the array.
 
This will return a Form array:
My.Application.OpenForms.Cast(Of Form)().ToArray()
 
Back
Top