0

Suppose I have forms A,B and C. I open frmB from frmA by: frmB B = new frmB(); B.show(); And then opens frmC from frmB by: frmC C = new frmC(); C.show(); I do not close forms A and B just in case the user needs to go back to those forms and change something. But if the user is done with form C, I need to go back to form A and close form C and B. How can I do this in WM6.5? This is something I've done in Android and in iOS via Flag Clear Top and Pop to Root respectively, but Im confused on how to do this in WM6.5.

SleepNot
  • 2,982
  • 10
  • 43
  • 72

1 Answers1

1

This is not difficult, but it does require some design and planning.

Start by looking at a basic version of FormC that has a CustomMessage string and supports a couple of button clicks for OK and Cancel:

public class FormC : Form {

  public FormC() {
    this.DialogResult = DialogResult.None;
  }

  private void Cancel_Clicked(object sender, EventArgs e) {
    this.DialogResult = DialogResult.Cancel;
    Close();
  }

  private void OK_Clicked(object sender, EventArgs e) {
    this.DialogResult = DialogResult.OK;
    Close();
  }

  public string CustomMessage { get; set; }

}

You need to set the DialogResult properties of FormC!

Now, turn your attention to FormB. It is a little more complex than above, but it should still be basic and understandable.

In FormB, you need to wire up the Form's Closed event handler so that you can set a FormC_DialogResult property when that form is closed. Set the FormC_DialogResult property to public so that FormA can use it's value.

public class FormB : Form {

  private FormC formC;

  public FormB() {
    this.DialogResult = DialogResult.None;
  }

  private void Cancel_Clicked(object sender, EventArgs e) {
    this.DialogResult = DialogResult.Cancel;
    Close();
  }

  private void OK_Clicked(object sender, EventArgs e) {
    this.DialogResult = DialogResult.OK;
    Close();
  }

  public string CustomMessage { get; set; }

  public void FormC_Show() {
    FormC_DialogResult = DialogResult.None;
    formC = new FormC();
    formC.Closed += new EventHandler(FormC_Closed);
    formC.CustomMessage = "This is FormC. When it closes, " +
      "it will call FormB's FormC_Closed event handler, " +
      "which will set the FormC_DialogResult property.";
    formC.Show();
  }

  private void FormC_Closed(object sender, EventArgs e) {
    FormC_DialogResult = formC.DialogResult;
    if (FormC_DialogResult == DialogResult.OK) {
      this.DialogResult = DialogResult.OK;
      Close();
    }
  }

  public DialogResult FormC_DialogResult { get; set; }

}

Notice that when FormC's OK button is clicked, FormB gets the same OK Dialog Result and it is closed, too.

Now, from FormA, you can control both of your other forms.

public class FormA : Form {

  private FormB formB;

  public FormA() {
  }

  private void FormBShow_Clicked(object sender, EventArgs e) {
    FormB_Show();
  }

  private void FormB_Show() {
    FormB_DialogResult = DialogResult.None;
    formB = new FormB();
    formB.Closed += new EventHandler(FormB_Closed);
    formB.CustomMessage = "This is FormB. When it closes, " +
      "it will call FormA's FormB_Closed event handler, " +
      "which will set the FormB_DialogResult property.";
    formB.Show();
  }

  void FormB_Closed(object sender, EventArgs e) {
    FormB_DialogResult = formB.DialogResult;
    if (FormB_DialogResult == DialogResult.OK) {
      if (formB.FormC_DialogResult == DialogResult.OK) {
        // Update Form C information
      }
      // Update Form B information
    }
  }

  private DialogResult FormB_DialogResult { get; set; }

}

Oh, and welcome to delegates!