Closing Form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am showing a MDI Child Form.
That form has a validation by logged user.
So that if an invalid user wants to open the forma message is displayed and
the form is closed.
But the problem that i cannot use the close() method neither on the Load nor
the Activate Event.

How can I solve it?

Thanks
 
Fernando Hunth said:
I am showing a MDI Child Form.
That form has a validation by logged user.
So that if an invalid user wants to open the forma message is displayed
and
the form is closed.
But the problem that i cannot use the close() method neither on the Load
nor
the Activate Event.

Add a shared factory method to your form class that will construct and
return the form only if the conditions are true:

\\\
Public Class Form1
Inherits Form
 
Looks like you already got one solution for this issue. Here is some sample
code for another solution to the same issue. I use code like this to close
the form if an error occurs during the form load. In the sample code I am
explicitly causing an error during the form load so that it can be tested.

Public Class frmTest
Inherits System.Windows.Forms.Form

< Windows Form Designer generated code >

Private mblnCloseFormDueToError As Boolean = False

Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Enclose program code with Try ... Catch ... Finally for error
handling
Try

Throw New System.Exception("Test")

Catch objEx As Exception ' Catch errors and define the name of the
exception object.
' The "objEx" varaible is the exception object.
' Below my general error handling code to log the error,
' but I am commenting out for this post.
'gobjErrorHandler.LogError(objEx)
' Below is some error handling code that is specific to this
situation.
' When there is an error loading a form,
' close it and display a message to the user.
MsgBox("There was an error bringing up the " & _
Me.Text & " screen.", _
MsgBoxStyle.Critical, Application.ProductName)
' The Me.Close() method does not seem to work during
' the Form.Load() event, so we will close the form
' when the Form.Paint() event fires.
mblnCloseFormDueToError = True
Finally ' Clean up code.
End Try

End Sub ' frmTest_Load

Private Sub frmTest_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' The Me.Close() method does not seem to work during
' the Form.Load() event. Closing the form during the
' Form.Paint() event allows us to close the form right
' away if there was an error during the Form.Load() event.
If mblnCloseFormDueToError = True Then
Me.Close()
End If
End Sub ' frmTest_Paint

End Class ' frmTest
 
Back
Top