user3844416
user3844416

Reputation: 165

Open up a form only once by finding out if a class of form is already open

I have a menulist with some menuitems that opening a form that accepts a parameter. At the moment, when it opens the form it will create another form, rather than focusing on the form if its already open.

I've seen plenty of C# examples but have difficulty converting them.

I've tried this code, but I think its not working because allthough the menu form is a mdiContainer form, the form it opens isnt a child. I've shown this as its what I want to find, i.e. is a specific CLASS of form open.

    For Each child In Me.MdiChildren
        If TypeOf child Is frmCustomerPurchaseOrders Then
            child.WindowState = FormWindowState.Normal
            child.Focus()
            Exit Sub
        End If
    Next

    Dim myForm As New frmCustomerPurchaseOrders("NotFullyInvoiced")
    myForm.Show()

I have looked at My.Application.OpenForms which does pick up the form text, but as the form text changes when It opens, its hard to match by form name. Is there a way of checking if a particular Class of form is already open?

        For Each f As Form In My.Application.OpenForms
            MessageBox.Show(f.Text)
        Next

However, if I'm going along the wrong path in doing this please let me know! Many thanks

Upvotes: 0

Views: 169

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39142

You can do the exact same thing with OpenForms, that your example code is doing with MdiChildren; just check the type of each Form, f, in the loop:

For Each f As Form In My.Application.OpenForms
    If TypeOf f Is frmCustomerPurchaseOrders Then
        ' ... do something in here with "f" ...
    End If
Next

Upvotes: 2

Related Questions