Results 1 to 8 of 8

Thread: [RESOLVED] Will Me.Close execute twice?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Resolved [RESOLVED] Will Me.Close execute twice?

    I'm cleaning up the code in some projects, doing things like moving Me.Close to Finally when both the Try & Catch end by closing the form.

    Theoretically, this project should probably run from a Sub Main but it's built with a user login form as the startup form-so everything runs from that form & when it closes the application ends. I'll try to summarize the code:

    Code:
    Try
       If Invalid_Password Then
          Me.Close
       End If
       Me.Hide
       NextForm.ShowDialog
    Catch
       Show_Error
    Finally
       Me.Close
    End Try
    Now, my question is if the password is invalid will it try to close the form twice & if so will it cause a problem? If not then what would be the best way to handle this? (Other than eliminating the Finally block & ending both Try & Catch with Me.Close)

    I could, I suppose, put Me.Hide & ShowDialog in an Else clause but there's considerable code in between that I left out in the summary. I don't know about you, but personally I hate IF/Else blocks with more than 5-10 lines of code.

    Thanks.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Will Me.Close execute twice?

    Yes, the call would be made twice. I would do this:
    Code:
    Try
        If Not Invalid_Password Then
           Me.Hide
           NextForm.ShowDialog
        End If
    Catch
        Show_Error
    Finally
        Me.Close
    End Try
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: Will Me.Close execute twice?

    As I mentioned, the two lines in the summary are 'shorthand' for a considerable body of code. I'd really prefer to find a clean way of ending the program in the If block. Or, I can go back to the original form:
    Code:
    Try
       If Invalid_Password Then
          Me.Close
       End If
       Me.Hide
       NextForm.ShowDialog
       Me.Close
    Catch
       Show_Error
       Me.Close
    End Try
    It looks wrong (like it should use a Finally block) but I could always add a comment explaining why it doesn't.

    Thanks.
    Last edited by calvin-c; Oct 16th, 2008 at 01:31 PM.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Will Me.Close execute twice?

    Code:
    Try
       If Invalid_Password Then
          Exit Try
       End If
       Me.Hide
       NextForm.ShowDialog
    Catch
       Show_Error
    End Try
    'what code is here?
    'if it is end sub then won't you get .Close here?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: Will Me.Close execute twice?

    Thank you. I think Exit Try is what I was looking for. IIRC the Finally block always executes, even following Exit Try, which is what I want.

    Yes, I could put .Close following End Try but I don't like having code outside Try/Catch blocks-no error checking.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Will Me.Close execute twice?

    I was asking what was after the end try statement?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: [RESOLVED] Will Me.Close execute twice?

    End Sub-but I don't see why that's relevant. You seem to suggest that End Sub will automatically close the form-but it doesn't. It would if this were a Sub Main, but it isn't. It's a startup form so End Sub simply returns to displaying the form.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Will Me.Close execute twice?

    i thought it was the other way around
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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