-2

I have declared a form as

Private _fUpdate As frmUpdate

There are all kinds of solutions to check if a form is open and visible.

However, if a form is Minimized and not shown in the taskbar, it doesn't show up in Application.OpenForms.

Also Form.IsHandleCreated returns false with the above window state.

If Not uForm Is Nothing returns True even if the form has not yet been instanciated, so it isn't usuable as well.

Is there another way to check whether a form is loaded then a variable storing the window existance and hidden/shown state?

IvanH
  • 5,039
  • 14
  • 60
  • 81
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • `Application.OpenForms` is unreliable at times. Seems like just checking if the object var `IsNot Nothing` should work fine. – Ňɏssa Pøngjǣrdenlarp Nov 10 '17 at 00:26
  • Null checking is not a good idea for this problem. When you close the form, the variable will not be null, instead the `Disposed` property of the form will be true. – Reza Aghaei Nov 10 '17 at 04:04
  • About the minimized form which doesn't show in taskbar, if instead of setting `ShowInTaskBar` property you hide the form, you can trust to `Visible` property. – Reza Aghaei Nov 10 '17 at 04:10
  • If I use Visible, it won't appear in OpenForms. :-( – tmighty Nov 10 '17 at 09:47
  • @Plutonix When is Application.OpenForms unreliable? tmighty: you have to post code that duplicates this issue with a minimized form not appearing in the OpenForms collection. I can't duplicate that problem. – LarsTech Nov 10 '17 at 19:22
  • 1
    @LarsTech https://stackoverflow.com/a/3751748/1070452 I think it happens in other cases than those listed....I had it flake out a time or two and started not using it on principle – Ňɏssa Pøngjǣrdenlarp Nov 10 '17 at 19:26

1 Answers1

0

What I usually do if I need to get the instance of a form from somewhere else within my application, is create a Shared property in your form called Instance or something similar and set it in the form's Load event.

With this instance property, you can call it from anywhere else in your application, and use the WindowState property of your form to check its state.


Example of the form:

Public Class frmUpdate
    Public Shared Property Instance As frmUpdate

    Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
        Instance = Me
    End Sub
End Class


Example usage in some other method:

Private Sub DoSomething()
    If (frmUpdate.Instance.WindowState = FormWindowState.Minimized)
        'Do something
    End If
End Sub
Raxdiam
  • 73
  • 2
  • 7