Click here to Skip to main content
15,941,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In VB.Net I want to check whether the current form is opened or not for access the function with the form

i give the coding
Invoice.Showgrip()

Somtimes if the form is not active the object reference not set to an instance of an object error will occured so how to check whether the form is visible or active.
help me please
Posted
Updated 9-Nov-22 4:37am
v2
Comments
Sergey Alexandrovich Kryukov 13-Jan-12 4:59am    
Why? why?!
--SA

You need to cycle through all forms and check if the one you want is indeed there
VB
For Each form In My.Application.OpenForms
        If (form.name = yourForm.name) Then
            'form is loaded so can do work 
            'if you need to check whether it is actually visible
            If form.Visible Then
                'do work when visible
            End If
        End If
    Next
 
Share this answer
 
'probably need to check if your form is instantiated

If Invoice isnot nothing then
invoice.Showgrip
End If
 
Share this answer
 
Null object reference has nothing to do with form status. It can be closed, activated — the instance of its class remains valid. I have no idea why would you need to check up if the form is closed. I'm pretty much sure you never need it. Some advice and example:

If you want to show a form again, never close it but hide it instead. Here is how:

C#
public class MyForm : Form {

    public override void OnFormClosing(FormClosingEventArgs eventArgs) {
        base.OnFormClosing(eventArgs);
        if (eventArgs.ClosegReason == System.Windows.Forms.CloseReason.UserClosing) {
            eventArgs.Cancel = true;
            this.Hide();
        }
    }

    //...

} //class MyForm


Also, if you do it right, form operations should not depend on its status. For example, Form.Show shows the form no matter if it was hidden or shown before the call.

See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^].

If you still want to know of the form was closed, you can add some Boolean flag as a field of the form class, override OnFormClosed and set this flag to true, which would indicate the form was closed. Again, I think you won't need it and won't find a good use of it; I added this advice only to formally answer your question.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900