Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Get current MDI document, and error if none.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [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!
    Last edited by NickThissen; Dec 4th, 2007 at 04:08 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [2005] Get current MDI document, and error if none.

    vb Code:
    1. If Me.ActiveMdiChild isnot nothing then
    2.     'etc

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [2005] Get current MDI document, and error if none.

    or use

    vb Code:
    1. try
    2.  
    3. catch ex as exception 'you'll have to narrow it down to the specific error you're getting
    4.  
    5. end try

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width