Windows Develop Bookmark and Share   
 index > Windows Forms General > what's the status of a form after form1.show() and form1.hide()
 

what's the status of a form after form1.show() and form1.hide()

I have 2 buttons on form1 to open 2 other forms: form2.show() and form3. show().
after working on form3 for a while I want to click a button on form3 to do soemthing on form2, in which case I need to check if form2 is still present (form2 might be closed or hidden). How could I achieve this?
thanks in advance!

Achievement provides the ultimate pleasure in life
ssfftt  Saturday, September 26, 2009 5:43 PM
  • Marked As Answer byssfftt Saturday, September 26, 2009 6:01 PM
  •  
David M Morton  Saturday, September 26, 2009 5:55 PM
  • Marked As Answer byssfftt Saturday, September 26, 2009 6:01 PM
  •  
David M Morton  Saturday, September 26, 2009 5:55 PM
Your main form should be the "switchboard", keeping track of form lifetimes. First Form3, it should declare an event:

public partial class Form3 : Form {
public EventHandler RequestForm2;
public Form3() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
RequestForm2(this, EventArgs.Empty);
}
}

Now the main form:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}

Form2 mForm2;

private void showForm2() {
if (mForm2 == null) {
mForm2 = new Form2();
mForm2.FormClosed += mForm2_FormClosed;
mForm2.Show();
}
else {
mForm2.WindowState = FormWindowState.Normal;
mForm2.Focus();
}
}

void mForm2_FormClosed(object sender, FormClosedEventArgs e) {
mForm2 = null;
}

private void button1_Click(object sender, EventArgs e) {
showForm2();
}

private void button2_Click(object sender, EventArgs e) {
Form3 frm = new Form3();
frm.RequestForm2 += Form3_RequestForm2;
frm.Show();
}

private void Form3_RequestForm2(object sender, EventArgs e) {
showForm2();
}
}


Hans Passant.
nobugz  Saturday, September 26, 2009 6:03 PM

You can use google to search for other answers

Custom Search

More Threads

• Scrollable charting application
• DoDragDrop on TreeView Problem
• .NET Remoting and unmanaged client code
• OTP: Install Vista Ultimate over Home Premium
• event handling in modal form
• Problem with animation class for show form
• Combo Box - Adding User Enter Text
• Disabling screen captures to the clipboard
• Displaying Pictures
• selected index of a record in datagrid when a selection is made in dropdown