码迷,mamicode.com
首页 > 其他好文 > 详细

c/s委托练习

时间:2017-05-05 00:47:56      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:ima   cli   click   delegate   box   new t   lob   object   委托与事件   

今天玩了玩C/S开发,也随便练习了很久不用的委托

父窗体中写的代码

#region 委托与事件传递
    public delegate void TextChangedHandler(string s);

    public class CallObject {
        //用来存放子窗体返回的结果
        public string ResultValue = "";
        //定义事件对象 可以让子窗体修改父窗体的状态
        public event TextChangedHandler SelTextChanged;
        //以调用delegate的方式触发函数
        public void ChangeSelText(string s)
        {
            if (SelTextChanged != null)
            {
                //调用delegate
                SelTextChanged(s);
            }
        }
    }
    #endregion

   void co_SelTextChanged(string s)
        {
            textBox2.Text = s;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            CallObject co = new CallObject();
            co.SelTextChanged += new TextChangedHandler(co_SelTextChanged);
            Form3 f3 = new Form3(co);
            f3.ShowDialog();
            textBox2.Text = "测试结果" + co.ResultValue;
        }

子窗体中写的代码

public Form3(CallObject cov) : this() { this.co = cov; }
        private void button1_Click(object sender, EventArgs e)
        {
            co.ResultValue = textBox1.Text;
            Close();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            co.ChangeSelText("A");
        }
  

        private void radioButton2_CheckedChanged_1(object sender, EventArgs e)
        {
            co.ChangeSelText("B");
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            co.ChangeSelText("C");
        }

技术分享

结果

c/s委托练习

标签:ima   cli   click   delegate   box   new t   lob   object   委托与事件   

原文地址:http://www.cnblogs.com/zhaohongtian/p/6810377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!