how to communicate data between forms?

后端 未结 4 707
后悔当初
后悔当初 2021-01-23 18:51

I want to pass some values to different forms at there button click event. plz guide me.I am using c sharp.net 2005,win forms. I want to access the value in a sql query in form

相关标签:
4条回答
  • 2021-01-23 19:15

    You can pass it through the constructor of the child form if the data is mandatory, or through a property if it is optional.

    0 讨论(0)
  • 2021-01-23 19:20

    Try this code, you have to do something like following code : inside this event you have to pass data to other

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 secondForm = new Form2();
            secondForm.YourProperty = "This is your data";
            secondForm.Show();
        }
    

    In the other form you have to declare a property :

        public string YourProperty { get; set; }
    

    hope this help.

    0 讨论(0)
  • 2021-01-23 19:28

    Use delegate. Thats the best way you could talk. Or as suggested, if form2 is a child of form1, then ctor argument based. If its 2 independent, then delegates.

    0 讨论(0)
  • 2021-01-23 19:36

    You have several options:

    • pass the data to the constructor of the child form
    • expose a instance property in the parent form, then pass that form as an argument to the child form
    • expose a static property in the parent form
    0 讨论(0)
提交回复
热议问题