Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Me.Close does not work

Tags:

forms

vb.net

I'm working with VB.net. I’m having problems while I connect my application to my database today so I wanted to add an error handling to close the form.

The problem is that when I put Me.close in a form, this form still open. I used the Form.Closing event handler to verify that it was called, and then ran my application in step by step which showed that the event handler was called, but the application continues and the errors appears to the user.

Does anyone knows how to close a form properly without closing the application?

like image 811
Ben Avatar asked Sep 22 '11 09:09

Ben


3 Answers

Close will close a form, but only if it has no more code to run.

That is, there are two conditions that need to be fulfilled for a form to close:

  • Close must be called
  • Any method still running must be left

I suspect that another method is still running code, for instance a loop or other code that causes the form to remain open.

Furthermore, the form will get re-opened automatically once you start accessing its members form elsewhere, due to an infuriating property of VB to auto-instantiate forms.

like image 136
Konrad Rudolph Avatar answered Nov 14 '22 22:11

Konrad Rudolph


You can check for what reason the form don't get closed.

Private Sub Form1_Closing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing

   MsgBox(e.CloseReason.ToString)

End Sub

You can add to the Form_Closing event the following The e.Cancel will close the open operation. But first check the reason.

Private Sub Form1_Closing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing

  e.Cancel = True

End Sub
like image 22
Sven Avatar answered Nov 14 '22 23:11

Sven


Technically the form is closed but not disposed, which means you can still reach the object but all controls in it are no longer reachable.

So you will have to call dispose from somewhere to get rid of it completely.

like image 1
chrissie1 Avatar answered Nov 14 '22 23:11

chrissie1