Home>

visual studio 2017
When both Form1 (parent screen) and Form2 (child screen) are open in the windows form, clicking the button1 of Form1 passes the string prepared by the Form1 click method to Form2 and displays it on Label1 of Form2 ( I want to implement the updated).

Code written in Form1

private void Form1_Load (object sender, EventArgs e)
        {
        }
        private void button3_Click (object sender, EventArgs e)
        {
            Form2 f = new Form2
            {
                // Set the value to the property of the sub screen
                strParam = "Value set from the parent screen"
            };
            f.ShowDialog ();
        }

Code written in Form2

// Create a property that can be accessed from the parent screen ----
        private String _strParam;
        public String strParam
        {
            get
            {
                return _strParam;
            }
            set
            {
                _strParam = value;
            }
        }
        public Form2 ()
        {
            InitializeComponent ();
        }
        private void Form2_Load (object sender, EventArgs e)
        {
            this.label1.Text = strParam;
        }
Problems i am experiencing

In this description, when the button of Form1 was clicked, Form2 was reopened and was in the state described in Label1 of Form2. I want to update and display Label1 of Form2 that was originally open.

Tried

Form2

private form1 fm1;
        // Overload the constructor
        public Form2 (Form1 fm)
        {
            //
            // Required for Windows Forms Designer support.
            //
            InitializeComponent ();
            // main form
            fm1 = fm;
        }
        private void Form2_Load (object sender, EventArgs e)
        {
            this.label1.Text = fm1. ~? ?

        }


I thought I could get information on Form1, but it doesn't work well even if I define it in Form1.
Even when trying to call the button method of Form1 from fm1, the variable of the result string value could not be written (red line). I am referring to the site, but I was confused. Can you give me some advice?

Other things I tried

public void Form1_Load (object sender, EventArgs e)
        {
            Form1 form1 = new Form1 ();
            form1.aaa2 = "bbb";
        }
Public string aaa2 {get;private set;}
Supplemental information (FW/tool version etc.)

win10
visual studio 2017 Community

What you did with advice

Form2

public string Y {get;internal set;} // ← Add
        public string X {get;internal set;} // ← Add
        public Form2 ()
        {
            InitializeComponent ();
        }

Form1

private void button2_Click (object sender, EventArgs e)
        {
            // Form2 form = new Form2 ();
            //form.Show ();
            // Instantiate class and set properties (traditional writing)
            Form2 form = new Form2 ();
            form.X = "1";
            form.Y = "2";
        }
        private void button3_Click (object sender, EventArgs e)
        {
            Form2.X = "3";// ← Object reference is required error
Definition of form.X = "3";// ← X is not defined
        }
What I did with advice 2

Form1
C #

public void Form1_Load (object sender, EventArgs e)
        {
            // Create Form2 when Form1 is loaded
            form2 = new Form2 ();
            form2.X = "test";
            form2.Show ();
        }
       private void button3_Click (object sender, EventArgs e)
        {
            // Label1 is public
            form2.X = "....";
            form2.Refresh ();
        }

Form2

public string Y {get;internal set;}
        public string X {get;internal set;}

        public Form2 ()
        {
            InitializeComponent ();
        }

From1 and Form2 appeared at the same time when debugging was executed. However, the label of Form2 is still displayed as Label1, test and initial are not displayed, and even if Button3 is pressed, there was no change.

Try ③

As a result, based on the value set on the Form2 side, a button was created that was assigned to label1.Text and reflected. Thank you very much.

  • Answer # 1

    Let's set the properties of the already displayed instance
    New instance will create Form2

    Click on button1 in Form1 (button3_Click, but button1?)

      

    button3_Click (
          Form2 f = new Form2

    Here, Form2 is newly created. Therefore,

      

    When you click the button of Form1, Form2 is reopened

    □ I think it is easy to create Form2 when opening Form1.

    // Class member has form2
       Form2 form2;
    // Create Form2 when Form1 is loaded
       private void Form1_Load (object sender, EventArgs e)
       {
          // ....
          form2 = new Form2 ();
          // ....
       }
       private void button3_Click (object sender, EventArgs e)
       {
          form2.Label1.Text = "....";// Label1 is public
          // or
          form2.SetLabel1 ("....");// Method to set a string in Lable1
       }

    Is this easy? Although I wrote directly above, it is easier to use later by preparing and calling the method (ex. SetLabel1 ()) that sets a string in Label1.

    In addition, although it is a description of the question text, I did not understand the distinction between Form1 and Form2, so I interpreted it appropriately here.