开发者

how to communicate data between forms?

开发者 https://www.devze.com 2023-03-28 04:32 出处:网络
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 2 receiv

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 2 received from form 1 variable.


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


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消