Pass value from parent form to child form?

后端 未结 3 640
梦如初夏
梦如初夏 2021-01-27 18:36

I\'m trying to pass a value set in my parent form to a second form. I created a property with a get part in the parent form.

I don\'t want to do something like:

3条回答
  •  遥遥无期
    2021-01-27 19:06

    I'm not sure about how you are going to use the Form2 in the parent(let it be frmParent). anyway you can follow any of the steps below:

    Define the property in the child form as static so that you can access that by using Form2.frmProperty.

    Or define the property as public and then access through the class instance, so that you can access the variable through that instance as long as the instance existed. something like the following:

    Form2 secondFormInstance = new Form2();
    secondFormInstance.frmProperty = 10;
    
    // at some later points
    
    int childValue = secondFormInstance.frmProperty; // take value from that variable
    secondFormInstance.frmProperty++; // update the value
    

    Or you can use like what you specified in the question.

提交回复
热议问题