Dean
Dean

Reputation: 1236

Visual Basic form .close() method

I have the below snippet of code:

'Handle level specific properties
Select Case ScoreCard.CurrentDifficulty
    Case 1
        intImageCount = 2 'This is the number of images to show at any given time on screen +1

        'debug
        ScoreCard.CurrentDifficulty = 6
    Case 2
        intImageCount = 3 'This is the number of images to show at any given time on screen +1
    Case 3
        intImageCount = 5 'This is the number of images to show at any given time on screen +1
    Case 4
        intImageCount = 2  'This is the number of images to show at any given time on screen +1
    Case 5
        intImageCount = 5 'This is the number of images to show at any given time on screen +1
    Case 6
        frmLevel3_HouseOfMirrors.Show()
        Me.Close()
        Return
End Select

When case 6 is executed frm3_HouseOfMirrors.Show() executes and my new form opens. Me.close executes as well but my problem is that the script then gets to the return line. Isn't me.Close() suppose to stop all execution of code on the current form and unload its self from memory?

Upvotes: 1

Views: 11554

Answers (3)

Konrad Rudolph
Konrad Rudolph

Reputation: 546045

Isn't me.Close() suppose to stop all execution of code on the current form and unload its self from memory?

No. Close does exactly what it says: it closes the visual, interactive representation of the form.1 It doesn’t affect code execution directly. It does make sure that Form_Closing and then Form_Closed are called, however. But the rest of the code execution is unaffected; in particular, the current method runs through normally. After that, other methods on the form may or may not be called as necessary (and, as mentioned, Closing and Closed will be called).


1 And, yes, it releases the form’s resources unless the form was shown via ShowDialog rather than plain Show.

Upvotes: 0

SysDragon
SysDragon

Reputation: 9888

Just call frmLvl3_HouseOfMirrors.ShowDialog() instead of .Show(), this will stop the execution of code until the new form is closed.

Or if you want to cancel the execution of the rest of code try the Exit instruction. You have to detect you want to finish and add it outside this Sub, because .Close() didnt stop the execution of code.

Upvotes: 2

Ben
Ben

Reputation: 11

No, the "close" method just closes the form, but the program execution will continue. If you want to stop code execution until a form is closed, you could make it modal.

In VBA it would look like this: frmLevel3_HouseOfMirrors.Show vbModal

Upvotes: 1

Related Questions