Hello,

I have an application where (and this is probably not how its done but something I'd like to do) the user clicks a button which brings up another form with 2 text boxes. The user enters text, and then clicks ok. The user entered text is then passed to a constructor to an object instantiated in form1. I noticed that control flow during form1's click event falls to the next line after form 2 is shown. Can I "suspend" control flow on form1, passing focus to form2 until the ok click event occurs on form2 so I can use its data to instantiate my constructor? In short, what I'm trying to do is this:

public class myClass
{
   public myClass(int a, int b);
//...constructor, and class methods, members, accessors etc.
}

public struct constructordata
{
   public int int_a;
   public int int_b;
}

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

   private void Its_Ok_button(object sender, EventArgs e)
   {
      constructordata c_data = new constructordata();
      //Passing in constructordata to form 2.
      Form2 form2 = new Form2(ref c_data);
      form2.Show();
      //Here I want to wait until the data has been assigned in form 2.
      myClass class = new myClass(c_data.int_a, c_data.int_b);
   }
   
   . . .

   public partial class Form2 : Form
   {
      public Form2(ref constructordata c_data)
      {
         InitializeComponent;
      }

   private void data_ok_Click(object sender, EventArgs e)
   {
      c_data.int_a = textBox1.Text.ToInt16();
      c_data.int_b = textBox2.Text.ToInt16();
// So here is where I want focus to return back to form1. 
     }

I tried writing a delegate for this, but I don't declare an instance of form1 on form2 so I don't have access to its methods. I could just put the text fields on form1 but that would clutter up other functionality and overall display of the form. No matter what I pass to form 2, control immediately falls back to the next line in form1. I created a boolean value in the struct, set it to false in form1 and true in form 2, placed the constructor after the while loop once the value fell to true, but that seems inelegant and it crashes the debuger with all of the context switching (this leads me to infer that form2 is not a thread but a forked process which therefore has its own process space. Which is rather daunting if so.)

I realize there are other ways to have written this, perhaps passing in a reference to the struct is a bad idea, but the problem still remains. I need data from form2 to be instantiated on form1 during form2's click event.

I vaguely remember from a VB6 class years ago that you can swap focus for forms and events but I am now more curious as to the hows and whys of form application behavior so I don't write a ticking time bomb just trying to instantiate a constructor.

Thanks a lot in advance!

whoaaa, just use a dialog

and i would use that ref, just use, properties in the second form

Form2 form2 = new Form2();
form2.ShowDialog();

if(form2.DialogResult == DialogResult.OK)
{
myClass class = new myClass(form2.IntA, form2.IntB);
}

i guess just to be thorough, he is what form2 could be

public class Form2 : Form
{
//all the other code
private int int_a;
public int IntA
{
get { return int_a; }
}
}

Worked! Thank you. The good ol' Modal and non-modal dialogs. It's slowly creeping back. For any other readers out there having this problem, also, in the Form2 code, make sure you actually assign Form2.DialogResult = Dialog.OK That hung me up for a bit, I'm embarrassed to admit.

This question has already been answered. Start a new discussion instead.