[RESOLVED] [2005] Get current MDI document, and error if none.
Hey,
I am using the following code to get the currently active Mdi Child (document) in my program. It works perfectly.
Code:
Public Function GetDoc() As frmDocument
Dim doc As frmDocument = CType(Me.ActiveMdiChild, frmDocument)
Return doc
End Function
However, when I try to use the result from "GetDoc()" if there are NO Mdi childs open at the time of calling "GetDoc()", then it errors.
How can I tell if there are no mdi childs open, so I can inform the user he/she is trying to do something impossible, without the program crashing?
I have tried this, but it didn't work: (Error: Object reference not set to an instance of an object.)
Code:
Public Function GetDoc() As frmDocument
Dim doc As frmDocument = CType(Me.ActiveMdiChild, frmDocument)
If Me.ActiveMdiChild.Name = "" Then
MessageBox.Show("Open a script first please!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return Nothing
Else
Return doc
End If
End Function
I know I could just make it give this message on every error that happens, but I don't want to do that if I don't have to... I can imagine a few other erros occuring, and I want to be able to give the user as much feedback as possible, instead of giving the same "Some unknown error happened" message at each error...
Thanks!
Re: [2005] Get current MDI document, and error if none.
vb Code:
If Me.ActiveMdiChild isnot nothing then
'etc
Re: [2005] Get current MDI document, and error if none.
or use
vb Code:
try
catch ex as exception 'you'll have to narrow it down to the specific error you're getting
end try
Re: [2005] Get current MDI document, and error if none.
Lol thanks... Your first suggestion did the trick... I was trying to do
If Me.ActiveMdiChild = Nothing Then...
and it kept having problems with the "=" sign... Now I used "Is" instead of "=" and it works... stupid :p