0

I am confused about how to navigate between Forms in c#

I want to do the following:

  1. from Form1 opening the Form2 and make Form2 on the top of the original one Form1 and then get back to Form1 and user are not allowed to use Form1 untill they close the child Form2 to get back to Form1

  2. with the same scenario but I may pass parameter from Form2 to Form1

I searched but think are not clear in my mind I found that there is something called MDI Parent And Child like the answer here but

I do not want the child from to be inside the original one

I do not the original from style to be change and be in that gray one

that way I think what I need to use is NOT MDI Parent And Child

please help I appreciate long explanation description with example

sam
  • 2,493
  • 6
  • 38
  • 73
  • What you want to do is open your Form2 as a `Modal` to form1. Refer this https://stackoverflow.com/questions/2503079/how-do-i-make-a-form-modal-in-windows-forms – adityap Sep 17 '18 at 20:44
  • For the second scenario, there are many ways you can pass back the data from form2 to form1. One of them is using property accessors on the form2 object. Refer - https://stackoverflow.com/questions/4587952/passing-data-between-forms – adityap Sep 17 '18 at 20:46
  • Possible duplicate of [Show a form from another form](https://stackoverflow.com/questions/2618830/show-a-form-from-another-form) – Paul McDowell Sep 17 '18 at 20:47

1 Answers1

0

This assumes that the data you are interested in on Form2 can be accessed by read/write properties. In my example, I show that as a simple string property called UsefulData. Here's the normal way to do what you are asking:

  private void button1_Click(object sender, EventArgs e)
  {
      var dialog = new Form2();                       //create an instance of Form2
      dialog.UsefulData = "Some Useful Data";         //set one or more properties of that form
      string result = "No Result Yet";                //a place to store the eventual data from Form2
      if (dialog.ShowDialog(this) == DialogResult.OK) //passing "this" properly parents the dialog to your Form1
      {                                               //note that I only get the data if the user pressed OK
          result = dialog.UsefulData;                 //get the data
      }
      //do something with that result
  }

There you go!

Flydog57
  • 6,851
  • 2
  • 17
  • 18