close Form1 & open Form2

K

Karan

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
 
A

Armin Zingler

Karan said:
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
 
K

Karan

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
 
A

Armin Zingler

Karan said:
Hi Armin

f.show() is giving me error nullreferance


Sorry, I forgot this one here:

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


Armin
 

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