Form Exit

G

Guest

Group,

i hope someone is able to help with this issue.

I'd like the form to exit after certain logic calculation... i tried the
me.dispose with in the Sub New() procedure, but it does not exit.

i passed the boolean true parameter to the Protected Overloads Overrides Sub
Dispose(ByVal disposing As Boolean), it does not do anything... i've included
a finalize procedure with no codes but still no go.

Please help.


Thanks in advance.


Ronin
 
H

Herfried K. Wagner [MVP]

Ronin said:
I'd like the form to exit after certain logic calculation... i tried the
me.dispose with in the Sub New() procedure, but it does not exit.

What do you mean with exit? To prevent the form from being initialized,
simply throw an exception.
 
H

Herfried K. Wagner [MVP]

Cerebrus said:
Try "Me.Close()"...

This won't have any effect inside the form's constructor because the
construction of a form's instance doesn't make the form visible at all.
 
G

Guest

Where would i put it? with in the Sub New()?
i had a try catch block with in the Sub New():
If intC > 0 Then
MsgBox("A survey was already taken with your information. If
you feel this is an error, please e-mail (e-mail address removed)")
Try
Me.Dispose(True)
Catch ex As Exception
Me.Close()
End Try
End If

and it still loads the form.


Further suggestions?



Thanks in advance.


Ronin
 
H

Herfried K. Wagner [MVP]

Ronin said:
Where would i put it? with in the Sub New()?
i had a try catch block with in the Sub New():
If intC > 0 Then
MsgBox("A survey was already taken with your information.
If
you feel this is an error, please e-mail (e-mail address removed)")
Try
Me.Dispose(True)
Catch ex As Exception
Me.Close()
End Try
End If

and it still loads the form.

\\\
Public Class Form1
Public Sub New()
If...Then
Throw New Exception(...)
Else
...
End If
End Sub
...
End Class
....
Try
Dim f As New Form1()
Catch...
...
End Try
///
 
G

Guest

Herfied,

The try/catch block statment seems to be out side the method body.
would i need to declare a separate class? if so, i'd like to be able to do
this without having to create a separate class in the form or class file.

Any help?
 
H

Herfried K. Wagner [MVP]

Ronin said:
The try/catch block statment seems to be out side the method body.
would i need to declare a separate class? if so, i'd like to be able to do
this without having to create a separate class in the form or class file.

You will have to place it where you are attempting to instantiate the form.
'Me.Close' and 'Me.Dispose' are rather useless because at the beginning of
the constructor the form is neither visible nor completely constructed.
 
C

Cerebrus

Hi Ronin,

I think someone would be able to help you better, if you gave some
details about How your form is launched ? From which form is it
launched ?

Without that information, I can just suggest that you should add the
code that Herfried suggested, at the place where you launch the form.
(which would have to be in another form class, wouldn't it)

Herfried wrote :

Thanks for the correction, Herfried, I didn't realize that Ronin wanted
to close his form in the Sub New itself. I thought he was looking for
the best place to close the form.

Regards,

Cerebrus.
 
G

Guest

The project/solution startup object is the actual form itself... i have no
class to launch/initialize anything else. The objective is to just create an
executable form file. The code within the form will then be use to connect to
a DB with DSN-less connection (string).

the form sub new code is as follows:
Public Sub New()
MyBase.New()
InitializeComponent()
With db
.Prop_User =
System.Security.Principal.WindowsIdentity.GetCurrent.Name
.cnn.Open()
.Prop_String = 1
.rst.Open(.Prop_String, .cnn)
intC = .rst.RecordCount
.Close_DB(.rst)
If intC > 0 Then
MsgBox("Test already taken")
MsgBox("Form will now exit.", MsgBoxStyle.Information +
MsgBoxStyle.OKOnly, "Exiting")
Me.Dispose(True)
Throw New Exception(MsgBox("Testing me",
MsgBoxStyle.OKOnly)) ' newly added code base on previous sugesstions
End If
End With
End Sub

But also tried adding the throw new exception within the Protected Overloads
Overrides Sub Dispose(ByVal disposing As Boolean) with the actual code
procedure below:

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
On Error GoTo ErrorHandler
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
ErrorHandler:
If Err.Number <> 0 Then
Throw New Exception("Testing me")
End If
End Sub

hope this helps. Please advice of further suggestions.

Thanks in advance.


Ronin
 
C

Cerebrus

Hi Ronin,

Now I never realized that you were using a single form application.
That makes it very different. Because when your Main (startup) form
closes, so does your application.

So, why don't you add all the code within the Load event instead of the
Sub New ? As in :

"Windows Form Designer generated code"

....

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

With db
.Prop_User =
System.Security.Principal.WindowsIdentity.GetCurrent.Name
.cnn.Open()
.Prop_String = 1
.rst.Open(.Prop_String, .cnn)
intC = .rst.RecordCount
.Close_DB(.rst)
If intC > 0 Then
MsgBox("Test already taken")
MsgBox("Form will now exit.", MsgBoxStyle.Information +

MsgBoxStyle.OKOnly, "Exiting")
Me.Close()
End if

End Sub

This should work...

Regards,

Cerebrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top