Question Find If Form Is Loaded

w.kalo

Member
Joined
Mar 7, 2012
Messages
6
Programming Experience
1-3
hi
i am new vb.net and i use build my software using access and vba
in the above there was a function called isloaded and return boolean value
it checks if a specified form is loaded or not

how this function can be done in vb

many thanks
 
The My.Application.OpenForms property is a collection of all the open forms in your application. You can use it in a variety of ways. This will tell you whether a specific form object is open, e.g. the default instance of Form2:
If My.Application.OpenForms.Cast(Of Form)().Contains(Form2) Then
This will tell if any instance of a particular form type, e.g. Form2, is open:
If My.Application.OpenForms.OfType(Of Form2)().Any() Then
That said, I can't think of any situation where I've needed to do that. Whatever you're trying to achieve, I'll wager that there's a better way. If you would like to explain further then we can let you know if and what it is.
 
hi there
many thanks for your reply
it works fine



why i needed the code?
because if the user has more than one form opened and forgot that a specific form is open, automatically the user will try to open it from menu so i want to prevent that.

that's all
regards
 
As I said, there is a better way. What you should do is activate the existing form if there is one or else open one if there isn't. If you're using default instances then that is as simple as this:
Form2.Show()
Form2.Activate()
If there is no form currently open then the first line will open a new one and the second line will have no effect. If there is a form already open then the first line will have no effect and the second line will shift focus to it.

If you're not using default instances then the code is only slightly more complex:
Private form2Instance As Form2

Private Sub OpenForm2()
    If form2Instance Is Nothing OrElse form2Instance.IsDisposed Then
        form2Instance = New Form2
    End If

    form2Instance.Show()
    form2Instance.Activate()
End Sub
 
Back
Top