Creating child forms?

  • Thread starter Thread starter jay
  • Start date Start date
J

jay

I'm trying to open up an existing form from my main form. In that new form,
I want to pass a value to a starting form from a button click.

I'm using:

Form2 f = new Form2()
f.Show()

but how do I reference the Form1 from the Form2?
I tried using ParentForm property, but unsuccessfully.

I would also like to know how to open this new form (Form2) modally.

thank you,
jay
 
I had the same problem, and the way I got around it was passing a variable
into the Form2 constructor. You can also declare a static variable on Form1
and set its value before calling Form2. I've asked around what the 'best'
or 'preferred' method is, but haven't received a response yet. But those
two options do work. The ShowDialog() method opens your form as a modal
dialog box, and returns a DialogResult you can use to determine which dialog
Button was pressed; or you can set its value manually in the modal dialog if
something went wrong.

Thanks,
Michael C.
 
You could pass a reference in the Form2 constructor.

Use ShowDialog() to get it modal, you can use the DialogResult enumeration
that is returned to determine appropriate actions.
You can associate a DialogResult with buttons, thay way you don't have to
implement a click event that sets the property.

Form2 f = new Form2(this)

if(f.ShowDialog() == DialogResult.OK)
{
// do something
}

Chris
 
Thanks for both of your replies. I'd like to add a complete solution I found
for my problem in case someone needs it:

In form1 I have a static variable that I can access from form2 when form2 is
open.
In form1 I have an event that gets fired when form2 is closed. This is where
I handle whatever I want handled when form2 is closed. It looks like this:

// This is form1 code:

private void button1_Click(object sender, System.EventArgs e)
{
Form2 f = new Form2();
f.Closed += new EventHandler(f_Closed);
f.ShowDialog();
}
private void f_Closed(object sender, EventArgs e)
{
_Act();
}
private void _Act()
{
MessageBox.Show ("Do whatever...");
}

hope this helps someone and thanks again to you two guys,
jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top