Lance
Lance

Reputation: 33

Close Windows Form while it is initializing

I have a winforms app that checks user credentials as it starts. If autharization fails I want to close the app. So I have been using the following

Public Sub New()
_CurrentUser.GetuserDetails
If _CurrentUser.IsAuthorized then
    'Let the app start
Else
   'Shut it down
    Me.Close()
End IF

End Sub

However this produces a "Cannot access a disposed object" error Any Ideas?

Upvotes: 2

Views: 1325

Answers (2)

Lance
Lance

Reputation: 33

Thanks for the quick reply Jay Using Application.Exit certainly stopped the error occuring. Unfortunatly it didnt close the app either.:)

It did let me see that the next executed section was the Form_Load event so I moved all my authorization code to there and then Application.Exit worked a treat. Me.Close also worked fine in the Load block. Thanks again

Upvotes: 1

Jay Riggs
Jay Riggs

Reputation: 53595

Instead of calling Me.Close(), call Application.Exit()

Upvotes: 2

Related Questions