wookie85

my program got 3 form i face a problem when i jus close my form 2 or 3 as the program is still running anyway to solve the problem Something like if form 2 or 3 is close it will link back to form 1 or when form 2 or 3 is close the problem will stop running .

below is a example of the code for my form


This is the code form Form1:

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

this.Hide();

Form2 frm = new Form2(this);

frm.Show();

}

}

This is the code for Form2:

public partial class Form2 : Form

{

private Form1 m_frm;

public Form2()

{

InitializeComponent();

}

public Form2(Form1 frm) : this()

{

m_frm = frm;

}

private void button1_Click(object sender, EventArgs e)

{

if( m_frm != null )

m_frm.Show();

}

}


This is the code for Form3:

public partial class Form3 : Form

{

private Form1 m_frm;

public Form3()

{

InitializeComponent();

}

public Form3(Form1 frm) : this()

{

m_frm = frm;

}

private void button1_Click(object sender, EventArgs e)

{

if( m_frm != null )

m_frm.Show();

}

}






Re: Windows Forms General Form problem

wjousts

Not quite following what you are trying to achieve here. But you're hiding form1 before you open form2 or form3 so are you trying to make sure form1 reappears when form2 or 3 is closed If that's the case then override the OnClosed method on form2 and form3 and make them do what you have in your button1_click event on form2 and 3.





Re: Windows Forms General Form problem

nobugz

Your app will terminate when the user closes Form1. It should stay running when either Form2 or Form3 is closed. To keep it running when any form is still open, you'll need to manage your message loop. The easiest way to do this is shown in this thread.





Re: Windows Forms General Form problem

wookie85

what i mean is if i close form 2 or 3 the program is still running cause form 1 is running so i need a way to stop the program from running if i close form 2 or 3




Re: Windows Forms General Form problem

wjousts

Well that's easy, over ride OnClosed in form2 and form3 and add

m_frm.Close();




Re: Windows Forms General Form problem

wookie85

sorry quite new to this how should i add it in the program

This is the code for Form2:

public partial class Form2 : Form

{

private Form1 m_frm;

public Form2()

{

InitializeComponent();

}

public Form2(Form1 frm) : this()

{

m_frm = frm;

}

private void button1_Click(object sender, EventArgs e)

{

if( m_frm != null )

m_frm.Show();

}

}


This is the code for Form3:

public partial class Form3 : Form

{

private Form1 m_frm;

public Form3()

{

InitializeComponent();

}

public Form3(Form1 frm) : this()

{

m_frm = frm;

}

private void button1_Click(object sender, EventArgs e)

{

if( m_frm != null )

m_frm.Show();

}

}






Re: Windows Forms General Form problem

Timo S

In Form 2 and 3 FormClosing event you should type:

Code Snippet


Application.Exit(); // This will close the whole application




It's the easiest way to do what you want!





Re: Windows Forms General Form problem

wookie85

sorry for the newbie question

how do i do a form closing event




Re: Windows Forms General Form problem

Timo S

public partial class Form2 : Form

{

private Form1 m_frm;

public Form2()

{

InitializeComponent();

this.FormClosing += new FormClosingEventHandler(Form_Closing);

}

private void Form_Closing(object sender, FormClosingEventArgs e) {

Application.Exit();

}


public Form2(Form1 frm) : this()

{

m_frm = frm;

}

private void button1_Click(object sender, EventArgs e)

{

if( m_frm != null )

m_frm.Show();

}

}



Important points in this code are:



1) Creating the FormClosing event handler:

this.FormClosing += new FormClosingEventHandler(Form_Closing);


2) Creating the event method

private void Form_Closing(object sender, FormClosingEventArgs e) {

Application.Exit();
}



Every time the form is closing, it will raise FormClosing event, and then our Form_Closing event is going to run. Application.Exit method in it, will close the whole application.




Timo Salomaki







Re: Windows Forms General Form problem

Timo S

Wookie, do you still have some problems or is your question answered Remember to mark best replies as answers!





Re: Windows Forms General Form problem

wookie85

thanks alot for the help it working now Big Smile




Re: Windows Forms General Form problem

wjousts

It would be better to override the OnClosing event that to add an event handler. Something like this:

Code Snippet

partial class Form2 : Form

{

private Form1 m_frm;

public Form2()

{

InitializeComponent();

}

public Form2(Form1 frm) : this()

{

m_frm = frm;

}

private void button1_Click(object sender, EventArgs e)

{

if( m_frm != null )

m_frm.Show();

}


protected override void OnClosed(EventArgs e)

{


m_frm.Close();

base.OnClosed(e);

}

}


It is best pratice to override the base class method rather than add event handlers within a class to handle an event originating with the same class. I'd also prefer to use Close() on your first form rather than Application.Exit since it gives you the chance to handle the Close event in that form if you need to as well (again by overriding the OnClosed event).
If you need to check whether everything should close, use OnClosing instead. That will give you a chance to abort an exit if you need to.

Edited to add this from MSDN -

The OnClosed method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Implementers When overriding OnClosed in a derived class, be sure to call the base class's OnClosed method so that registered delegates receive the event.


Again, best practice.





Re: Windows Forms General Form problem

Timo S

Well, you're welcome Just remember to ask if you still have some problems!


Timo Salomaki