Simple closing form question

desperado

Well-known member
Joined
Oct 7, 2006
Messages
68
Programming Experience
Beginner
Hi everyone,

Was hoping someone out there could help me out please.

I have a parent form and a menu at the top, I use the menu to open new forms. The problem I'm facing is when I click on 'Quit' in the menu it should close the form I have opened but it keeps throwing an error.

On the quit button I have the following coding.
VB.NET:
frm2.close
frm3.close
frm4.close

As it should close the form that is opened, however it breaks on the form that is not opened. Any help or suggestions please?
 
You should be able to check if the form has been disposed before calling the close member function.

Something like:

VB.NET:
        If Not Form2 Is Nothing OrElse Not Form2.IsDisposed() Then
            Form2.Close()
        End If
 
I thought it was:
VB.NET:
If Not Form2 Is Nothing AndAlso Not Form2.IsDisposed() Then Form2.Close()
 
I think JuggaloBrotha is right and it should be AndAlso. This is something that a coworker sent to me that helps me remember the difference between the two:

A AndAlso B : evaluates A first. If A is true, it goes on to test B. If A is false, it doesn't bother testing B. In a big loop this makes a difference. Also, B could do other stuff -- like set of a chain of function calls and database hits or access stdin or console.readline() that you might not want it to when there's no point.

A OrElse B : evaluates A first. If A is true, it doesn't bother testing B. If A is false it goes on to evaluate B.
 
Thanks for the correction Juggalo. :D

Good thing I prefaced my comment with "Something like:"

No problem, sometimes knowing when to use 'AndAlso' vs 'OrElse' can get a little tricky when you're not used them or just aren't paying attention or a combination of both.
 
Back
Top