FlyinBrian

This is giving me a headache. I'm trying to be able to switch between two forms constantly. So I click a button, main form closes, next one opens. Then I click button on second form, second one closes and first one opens again. Each time they get open they must be new instances.

So far for the second form's button I have

Form1 form = new Form1();
form.Show();
this.Close();

Which works fine between the two except the second form isnt really closing. (Should be disposing). When I try this.Dispose(), the whole program exits before the main form opens again.
Any ideas


Re: Windows Forms General Open Form, Close last form

Marco Minerva

Hi!

Try this way: add a constructor in the Form1 class that accepts as argument a reference to a Form class. Next, in the Load event of Form1, you call the Close and the Dipose method of the Form instance that was passed to the constructor. An example:

public partial class Form1: Form

{

Form1 frm = null;

public Form1(Form form)

{

frm = form;

}

public void Form_Load(object sender, EventArgs e)

{

//NOTE: you must register the Load event!

frm.Close();

frm.Dispose();

}

}

Bye.

Marco Minerva, marco.minerva@tiscali.it






Re: Windows Forms General Open Form, Close last form

FlyinBrian

same thing. for some reason the whole program exits if I call the Dispose method. Mind you I changed the form_load event a bit to

private void Form1_Load(object sender, EventArgs e)
{
if (frm != null)
{
frm.Close();
frm.Dispose();
}

}

somethins up with that dispose. When I'm in the second form I call the first form by
Form1 form = new Form1(this);
form.Show();


Form1 is the main form that starts up. Any other ideas




Re: Windows Forms General Open Form, Close last form

FlyinBrian

Bump, any help would be greatly appreciated




Re: Windows Forms General Open Form, Close last form

New-Bee

A couple of ideas- have you tried assigning null after disposing (worked for me with images issues i had once) Otherwise, what about adding the ref keyword





Re: Windows Forms General Open Form, Close last form

FlyinBrian

What do I assign Null I think the main form is open, just not showing. When I run the debug, I get into Form2, then I go back to form1. Form 2 disposes sucessfully, and i think form1 briefly flashes but doesnt stay open. And the debugger stays open making me think the form1 is active, just not showing. Im using
Form1 form = new Form(Form2.Activeform)
form.show();


this is run from the second form.




Re: Windows Forms General Open Form, Close last form

nobugz

Something is really wrong. When you close your 1st form after showing the 2nd form, your application should terminate. Please show us the code you use to switch from the 1st to the 2nd form.





Re: Windows Forms General Open Form, Close last form

FlyinBrian

The application is threaded. Heres a snippet

formClose = true;
DoEnableTextBox(true);

Properties.Settings.Default.save = true;
Properties.Settings.Default.Save();
Application.Run(new Main());
openConn.Abort();

I cannot dispose the form1 here as it gives me cross threaded exception, so i do it in the DoEnableTextBox method which invokes the object. It contains this

if (formClose == true)
{
Application.OpenForms["Form1"].Dispose();
}







Re: Windows Forms General Open Form, Close last form

RomanGuzi

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/M_System_Windows_Forms_Application_Run_1_5f393ee3.htm Application.Run Method (Form): This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application.

 This means that whenever you close form passed to Application.Run() method, all application would be terminated. You can use third form to manage other forms, and close it only when all other forms are closed. Something like this:

-------------------------------------------------------

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new FormMgr());

}

}

-----------------------------------------------------------

public partial class FormMgr : Form

{

private Form _currentForm;

private static FormMgr _formMgr;

public FormMgr()

{

_formMgr = this;

this.WindowState = FormWindowState.Minimized;

this.ShowInTaskbar = false;

InitializeComponent();

}

public static FormMgr Mgr

{

get { return _formMgr; }

}

public void ShowForm(Form form)

{

if (_currentForm != null)

{

Form currentForm = _currentForm;

_currentForm = null;

currentForm.Close();

}

_currentForm = form;

_currentForm.FormClosed += new FormClosedEventHandler(_currentForm_FormClosed);

_currentForm.Show();

}

void _currentForm_FormClosed(object sender, FormClosedEventArgs e)

{

if (sender == _currentForm)

{

this.Close();

}

}

protected override void OnLoad(EventArgs e)

{

base.OnLoad(e);

ShowForm(new Form1());

}

}

--------------------------------------------------------------

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

FormMgr.Mgr.ShowForm(new Form2());

}

}

-------------------------------------------------------------

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

FormMgr.Mgr.ShowForm(new Form1());

}

}