[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1344
  • Last Modified:

Passing another form as a constructor's parameter

Hi, I have two forms: Form1 and Form2.  Please see the following description.
1. Form1 has one textbox and one combobox.
2. Form2 has also one textbox and one combobox.
3. When I click the button in Form1, it opens Form2.
4. I enter the values for the textbox and the combobox in Form2.
5. I click the save button in Form2.  Then, the values I entered are displayed in Form1's fields respectively.
I have following codes and it's "working".  However, I am not clearly understanding HOW.
The thing that I don't understand is passing Form1 into Form2's construcotor and assigning the parameter to f1.  I don't know why this step is required.  
I know when I click a button in Form1, it calls the constructor that has Form1 as a parameter in Form2.  and Form2's constructor is executing..and f1 can be used as an instance of Form1 so that it can access Form1's fields.  Please correct me if I am wrong.  I want a detailed explanation.  Thanks!
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);            
            f2.ShowDialog();           
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}
---------------------------------------------------------
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 f1 = null;
        public Form2(Form1 pParent)
        {
            InitializeComponent();         
            f1 = pParent;            
        }        
        private void button1_Click(object sender, EventArgs e)
        {
            f1.comboBox1.SelectedItem = comboBox2.Text;  
            f1.textBox1.Text = textBox2.Text;
        } 
        private void Form2_Load(object sender, EventArgs e)
        {            
        }             
    }
}

Open in new window

0
muffintwinkly
Asked:
muffintwinkly
  • 4
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
In my opinion you are doing it backwards...Form1 created Form2 so it should query Form2 when the save button is pressed.

It would look like this:
(the Modifiers Property for the TextBox and ComboBox on Form2 must be changed to Public or Friend)
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            if (f2.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = f2.textBox1.Text;
                this.comboBox1.Text = f2.comboBox1.SelectedItem.ToString();
            }
        }
 
    }
}
 
// ---------------------------------------------------------
 
namespace WindowsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
 
    }
}

Open in new window

0
 
muffintwinklyAuthor Commented:
There are more than one way doing this and I know that using constructor is not the best answer; however, I just want to understand the use of constructor that way...I want to understand the codes I provided.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"The thing that I don't understand is passing Form1 into Form2's construcotor and assigning the parameter to f1.  I don't know why this step is required."

Think of it this way.  Without passing the reference in, Form2 would have no idea that Form1 even existed.  We have to give to a "pathway" to Form1 and this is done by passing in the reference via the "this" keyword.

In the constructor of Form2, we are "saving" that reference in the f1 variable so that it can be used in other parts of the Class.  If we didn't save it in f1 then you would only be able to use that reference in the Constructor itself.  After the constructor completed running the reference would go out of scope and Form2 would no longer have a way of referring to Form1.

Does that help?

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jandromedaCommented:
Here how it is working. When you click the button1_Click on the Form1, in the click method you instantiate a new Form2 by calling the constructor of the Form2 and you pass a reference to the Form1 to that constructor as a parameter. Now the newly created Form2 object has a reference to the Form1 as you assign that reference to a identifier (f1) of the Form1 type. Now it is like you have a Form1 inside Form2 but this time Form1 is called f1. Now in the button1_Click method of the Form2 you directly set the values of the controls of the Form1. And that is how it works. If you don't pass a reference to the Form1 to the Form1 through the constructor you cannot use Form1 inside Form2.
0
 
muffintwinklyAuthor Commented:
are you saying when we declare Form1 f1 = new Form1() in class level; in Form2, Form2 doesn't know that Form1 exists unless we provide contructor with a Form1 parameter?  When I declare Form1 in class level inside Form2 and remove constructor, Form 2 doesn't display anything when it is open from Form1.  
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No...let me clarify.

Form2 would have no idea that the CURRENLTY DISPLAYED instance of Form1 exists unless we pass a reference in.

If you did this:

    public partial class Form2 : Form
    {

        private Form1 f1 = new Form();
 
    }

What you have done is created ANOTHER instance of Form1 that only Form2 can access.  At this point, the instance (f1) only exists in memory.  It isn't visible because the Show() method hasn't been called on it.

0
 
muffintwinklyAuthor Commented:
Thank you very much.
0
 
muffintwinklyAuthor Commented:
Thanks!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now