none
Prevent multiple form instances RRS feed

  • Question

  • Hi

    Not been here for a while or done any coding for about 12 months, just picking up an old project i wanted to get finished. What is the best method to prevent multiple forms being instantiated when the button to load the form is clicked? I am using the following

    in AddinGlobal Class

    Friend Shared FormOpen As Boolean = False

    in the Form Load Event

    AddinGlobal.FormOpen = True

    in the Form Close event

    AddinGlobal.FormOpen = False

    in the method to open the form

    If Not AddinGlobal.FormOpen Then 
       dim f as New frmMain
       f.Show(New InventorWin())
    End If

    Im sure there must a more official way of managing this, i would like to do this properly instead of it just working. One thing that might get commented on is the Friend Shared access level. It seems that if i dont share it, i cant access it from anywhere..

    Anyways, any help / advice is always appreciated :)

    Thanks

    Nacho


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!




    • Edited by NachoShaw Wednesday, August 14, 2019 2:51 PM
    Wednesday, August 14, 2019 2:50 PM

All replies

  • Hello,

    Create a code module, place the following code in.

    Namespace My
        Partial Friend Class MyApplication
            ''' <summary>
            ''' Determines if the a form is open
            ''' </summary>
            ''' <param name="sender">Name of form</param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function IsFormOpen(ByVal sender As String) As Boolean
                Return ((From f In My.Application.OpenForms.Cast(Of Form)() Where f.Name.Equals(sender) Select f.Name).ToList.Count > 0)
            End Function
        End Class
    End Namespace
    

    Basic usage

    If Not My.Application.IsFormOpen("ChildForm") Then

    Try it out from a sample I put on Microsoft OneDrive

    https://1drv.ms/u/s!AtGAgKKpqdWjiCMVgSj58ltBc3Di?e=LVCp2q


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, August 14, 2019 5:36 PM
    Moderator
  • Try this method of opening the form:

     

    If Not frmMain.Visible Then
        frmMain.Show(New InventoryWin)
    Else
        If frmMain.WindowState = FormWindowState.Minimized Then frmMain.WindowState = FormWindowState.Normal
        frmMain.Activate()
    End If

     

    The shared variable is not needed.


    • Edited by Viorel_MVP Wednesday, August 14, 2019 5:53 PM
    Wednesday, August 14, 2019 5:52 PM
  • Thank Karen, i will try this tonight

    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Wednesday, August 14, 2019 10:32 PM
  • Hi

    Thanks for the reply. In reference to the Shared Variable not being needed, this is what happens:

    Shared

    in AddinGlobal

    In form - FormOpen is exposed

    Not Shared

    In AddinGlobal

    In Form - FormOpen is NOT exposed

    Thanks


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Wednesday, August 14, 2019 10:36 PM
  • I would probably do it a different way. I would create one instance of the form and then show or hide it. Do you want specifics of that?

    Your code shows creation of the form (dim f as New frmMain) then showing the form but not deletion of the form. And I am not sure but I think that creating the form the way you do won't work. Viorel_'s code might help, I am not sure what it is doing.

    Karen's code might help determine if the form is open but I think you need more than that.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, August 17, 2019 7:25 PM