Hello,
In opening form 1, I open form 2 in the constructor before I initialize componentss().
I created a public variable to set a int value in form2 so that if the correct value is returned, form 1 continues on its way, otherwise the project closes. I recieve this error when the retuned value is not set to 1, when i want the project to close. Is there another way that works? Is there a way to create a int value return for form 1?
HelPPP! THanks.
"Make sure you have not released a resource before attempting to use it. "
public Form 1()
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
if (frm2.returned_value == 1)
initializeComponents();
else{this.close();}
}
j_sen21Posted Nov 21, 2007, 10:19 AM
AlanPosted Nov 21, 2007, 10:12 AM
Well, it's a bit drastic but you can avoid placing Application.Run() in a try block by replacing this.Close() with Environment.Exit(1).
This will kill the process stone dead. It's not normally recommended for Windows forms applications but, as Application.Exit() doesn't work, it's probably all you can use here.
j_sen21Posted Nov 21, 2007, 8:24 AM
THanks for the reply Alan,
I actually threw that in there, but now I wont know when any other errors occur in form 1.
j_sen21Posted Nov 21, 2007, 8:22 AM
j_sen21Posted Nov 21, 2007, 7:55 AM
I should have included that. I did that at first. But when you close down form 1, it leaves the program open when you check run programs. Unless there is a way of killing the whole app from form 1???
THanks
AlanPosted Nov 21, 2007, 5:53 AM
Although I prefer Jan's and Ryan's suggestions, it is actually possible to do what you want by having a Returned_Value field in Form1 which is set from Form2. When Form2 is constructed, you can pass a reference to Form1 as a parameter to enable the return value to be set.
If the main form is closed in the constructor, then the Application.Run() method in Main() which is responsible for displaying the main form will throw an exception because the main form instance has been disposed of. You can deal with this by calling Application.Run() within a try statement, catching the exception and letting it fall through.
This short (and heavily simplified) program will show you what I mean:
using System;
using System.Windows.Forms;
class Test
{
static void Main()
{
try
{
Application.Run(new Form1());
}
catch
{
}
}
}
class Form1 : Form
{
public int Returned_value = 0;
public Form1()
{
Form2 frm2 = new Form2(this);
frm2.ShowDialog();
if (Returned_value == 1)
this.Text = "Form1";
else
this.Close();
}
}
class Form2 : Form
{
public Form2(Form1 frm1)
{
this.Text = "Form2";
// in practice you'd want to cache frm1 in a field
// so it can be called from any method in Form2
frm1.Returned_value = 2; // change to '1' to load Form1
}
}
Ryan AlfordPosted Nov 20, 2007, 10:54 PM
if (form2.showdialog() == dialogresult.OK) // depending on what the "1" means
{
// do code here
}
Jan MontanoPosted Nov 20, 2007, 10:38 PM
Maybe it would be better if you set Form2 as your startup form instead. Then from Form2, depending if the condition is successful, you'll call Form1.
Cheers,
Jan