470,612 Members | 2,468 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,612 developers. It's quick & easy.

close Form1 & open Form2

I am calling finalize when form2 loads and deactivates form1 which closes
form1. However, same thing is not happening in form2 because finalize is
already called. Does anybody has solution to it. This code works well for
splash screen. I searched alot on net for codes but they don't work. for
example-
(1)

Public Sub CloseForm(formType As System.Type)
For Each oForm as System.Windows.Forms.Form in me.MdiParent.MdiChildren
If oForm.GetType() = formType Then
oForm.Dispose()
oForm.Close()
Next
End Sub

'how to use?
' CloseForm(TypeOf Form3)

(2)

--- code from FormSplash.cs ----

private FormMain formMain;
private ApplicationContext appContext;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//create splash and main forms to be used with the application context
FormSplash formSplash = new FormSplash();
formMain = new FormMain();

// set the main form of the application context object to be the splash
screen. When the splash screen is closing, it will switch the MainForm
property to be the instance of formMain.
appContext.MainForm = formSplash;

// run the application context (this shows the splash screen)
Application.Run(appContext);
}

protected override void OnClosing(CancelEventArgs e)
{
// the splash screen is about to close. Tell the application context to
now wait until the main form is closed before exiting the application. You
could put this code in many places but this seems to be just as easy as
anywhere else I could think of.
appContext.MainForm = formMain;

// hide this screen and show the main screen
this.Hide();
formMain.Show();

base.OnClosing (e);
}

--- end sample code ---

###My Own Code###

Form1 Code

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click

Dim frm As New Form2

frm.Show()

End Sub

Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate

finalize()

End Sub

Form2 Code

Private Sub Form2_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

Application.Exit()

End Sub

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click

Dim frm As Form1

frm = New Form1

End Sub

Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate

finalize()

End Sub
waiting for rerply
Thank you
Karan
Mar 9 '06 #1
3 5574
"Karan" <ka******@hotmail.com> schrieb
I am calling finalize when form2 loads and deactivates form1 which

You must never call Finalize. This is done by the GC immediatelly before
destroying the object. To close a Form, call it's close method only. If you
don't need the Form instance anymore, remove the reference. Later, whenever
the GC does it's work, the Form object is destroyed.

closes form1. However, same thing is not happening in form2 because
finalize is already called. Does anybody has solution to it. This
code works well for splash screen. I searched alot on net for codes
but they don't work. for example-
(1)

Public Sub CloseForm(formType As System.Type)
For Each oForm as System.Windows.Forms.Form in
me.MdiParent.MdiChildren If oForm.GetType() = formType Then
oForm.Dispose()
oForm.Close()
Next
End Sub

'how to use?
' CloseForm(TypeOf Form3)

Public Sub CloseForm(formType As System.Type)
For Each oForm as System.Windows.Forms.Form in me.MdiParent.MdiChildren
If oForm.GetType() Is formType Then '<----- "=" replaced by "Is"
oForm.Close()
Next
End Sub
Calling oForm.Dispose() is not necessary because it is called anyway
whenever the Form is closed.
'how to use?
' CloseForm(TypeOf Form3)

CloseForm(GetType(Form3))
(2)

--- code from FormSplash.cs ----

private FormMain formMain;
private ApplicationContext appContext;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//create splash and main forms to be used with the application
context FormSplash formSplash = new FormSplash();
formMain = new FormMain();

// set the main form of the application context object to be the
splash screen. When the splash screen is closing, it will switch the
MainForm property to be the instance of formMain.
appContext.MainForm = formSplash;

// run the application context (this shows the splash screen)
Application.Run(appContext);
}

protected override void OnClosing(CancelEventArgs e)
{
// the splash screen is about to close. Tell the application
context to now wait until the main form is closed before exiting the
application. You could put this code in many places but this seems
to be just as easy as anywhere else I could think of.
appContext.MainForm = formMain;

// hide this screen and show the main screen
this.Hide();
formMain.Show();

base.OnClosing (e);
}

--- end sample code ---

###My Own Code###

Form1 Code

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click

Dim frm As New Form2

frm.Show()

End Sub

Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate

finalize()

End Sub

Form2 Code

Private Sub Form2_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

Application.Exit()

End Sub

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click

Dim frm As Form1

frm = New Form1

End Sub

Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate

finalize()

End Sub

I do not completely understand what's your intention:

The code in Form1.btnShow_Click looks ok. Delete subs Form1_Deactivate and
Form2_Deactivate. In Form2.btnShow_Click, there seems to be a "frm.Show"
missing.

Looking at btnShow_Click: Do you want to close Form1 if you open Form2, and
close Form2 if you open Form1? Then you should add Me.Close in btnShow_Click
after showing the other Form. If there is no Form that will live from the
start till the end of the application, use this Sub Main:

Sub Main 'In a Module. In a Class, use Shared Sub Main
Dim f as Form1
f.Show
application.run()
end sub
Armin

Mar 9 '06 #2
Hi Armin

f.show() is giving me error nullreferance
Sub Main 'In a Module. In a Class, use Shared Sub Main
Dim f as Form1
f.Show
application.run()
end sub
There is a button on form1 show, on click of that button form1 should close
and form2 should open and vice versa.

Thanks for the reply
Karan

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... "Karan" <ka******@hotmail.com> schrieb
I am calling finalize when form2 loads and deactivates form1 which

You must never call Finalize. This is done by the GC immediatelly before
destroying the object. To close a Form, call it's close method only. If
you don't need the Form instance anymore, remove the reference. Later,
whenever the GC does it's work, the Form object is destroyed.

closes form1. However, same thing is not happening in form2 because
finalize is already called. Does anybody has solution to it. This
code works well for splash screen. I searched alot on net for codes
but they don't work. for example-
(1)

Public Sub CloseForm(formType As System.Type)
For Each oForm as System.Windows.Forms.Form in
me.MdiParent.MdiChildren If oForm.GetType() = formType Then
oForm.Dispose()
oForm.Close()
Next
End Sub

'how to use?
' CloseForm(TypeOf Form3)

Public Sub CloseForm(formType As System.Type)
For Each oForm as System.Windows.Forms.Form in me.MdiParent.MdiChildren
If oForm.GetType() Is formType Then '<----- "=" replaced by "Is"
oForm.Close()
Next
End Sub
Calling oForm.Dispose() is not necessary because it is called anyway
whenever the Form is closed.
'how to use?
' CloseForm(TypeOf Form3)

CloseForm(GetType(Form3))

###My Own Code###

Form1 Code

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click

Dim frm As New Form2

frm.Show()

End Sub

Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate

finalize()

End Sub

Form2 Code

Private Sub Form2_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

Application.Exit()

End Sub

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShow.Click

Dim frm As Form1

frm = New Form1

End Sub

Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate

finalize()

End Sub

I do not completely understand what's your intention:

The code in Form1.btnShow_Click looks ok. Delete subs Form1_Deactivate and
Form2_Deactivate. In Form2.btnShow_Click, there seems to be a "frm.Show"
missing.

Looking at btnShow_Click: Do you want to close Form1 if you open Form2,
and close Form2 if you open Form1? Then you should add Me.Close in
btnShow_Click after showing the other Form. If there is no Form that will
live from the start till the end of the application, use this Sub Main:

Sub Main 'In a Module. In a Class, use Shared Sub Main
Dim f as Form1
f.Show
application.run()
end sub
Armin

Mar 12 '06 #3
"Karan" <ka******@hotmail.com> schrieb
Hi Armin

f.show() is giving me error nullreferance
Sub Main 'In a Module. In a Class, use Shared Sub Main Dim f as
Form1

Sorry, I forgot this one here:

f = New Form1
f.Show
application.run()
end sub


There is a button on form1 show, on click of that button form1 should
close and form2 should open and vice versa.

Armin

Mar 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ashish | last post: by
7 posts views Thread by ematrix | last post: by
3 posts views Thread by rdemyan via AccessMonster.com | last post: by
5 posts views Thread by Sudha Pune | last post: by
4 posts views Thread by Jason Huang | last post: by
3 posts views Thread by Ronald S. Cook | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.