Hello
I am developping an application with five form (not MDI form), and I would like manupilate all this form from the main form (The main form),
my question is : how to close these forms from the main form
Best regard
Hello
I am developping an application with five form (not MDI form), and I would like manupilate all this form from the main form (The main form),
my question is : how to close these forms from the main form
Best regard
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kumar AGPosted Apr 29, 2009, 2:59 PM
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//You are getting the error because you have declared Form variables with in the method
//And trying to access them from other method
//Variable for form1 is is out of scope in second method and similarly others
//Declare the variables at class level to access it from all the methods
Form1 SR;
Form2 SCRI;
Form3 CF;
private void btn_Rechange_Click(object sender, EventArgs e)
{
//Check if the form1 already open/Exists, if exists get the form1 object else create a new form1 object
SR = (Form1) Application.OpenForms["Form1"];
if (SR == null)
SR = new Form1();
SR.Show();
//Check if form2 is already open/exists, if exists close
SCRI = (Form2) Application.OpenForms["Form2"];
if(SCRI != null)
SCRI.Close();
//You can also use Hide method
//Check if form3 is already open/exists, if exists close
CF = (Form3) Application.OpenForms["Form3"];
if (CF != null)
CF.Close();
}
//Similarly for other forms also
private void btn_CRI_Click_Click(object sender, EventArgs e)
{
SCRI = (Form2) Application.OpenForms["Form2"];
if (SCRI == null)
SCRI = new Form2();
SCRI.Show();
SR = (Form1) Application.OpenForms["Form1"];
if (SR != null)
SR.Close();
CF = (Form3) Application.OpenForms["Form3"];
if (CF != null)
CF.Close();
}
private void btn_Config_Click(object sender, EventArgs e)
{
CF = (Form3) Application.OpenForms["Form3"];
if (CF == null)
CF = new Form3();
CF.Show();
SR = (Form1) Application.OpenForms["Form1"];
if (SR != null)
SR.Close();
SCRI = (Form2) Application.OpenForms["Form2"];
if (SCRI != null)
SCRI.Close();
}
}
Posted Apr 29, 2009, 3:58 PM
Posted Apr 29, 2009, 1:26 PM
hello
thank you for your response, but, i would llike close Form1, when Form2 is open, and Close Form2 when Form3 is open....see exemple below;
private
void btn_Rechange_Click(object sender, EventArgs e){
Form1 SR = new Form1();SR.Show(
);CF.Close();
SCRI.Close();
}
private void btn_CRI_Click(object sender, EventArgs e)
{
Form2 SCRI = new Form2();
SCRI.Show();
CF.Close();
SR.Close();
}
private void btn_Config_Click(object sender, EventArgs e)
{
Form3 CF = new Form3();
CF.Show();
SR.Close();
SCRI.Close();
}
this error is displayed :
Erreur 3 Le nom 'CF' n'existe pas dans le contexte actuel
or
Error 3 the name CF is not exist;
that's my problem
if you have a response, I would be very grateful
StefanPosted Apr 29, 2009, 10:56 AM