C# 利用委托完成窗体间传数据

主窗体:form1

参数设置窗体:form4

首先在form4中设置委托和事件

public delegate void TransfDelegate(string value,double dv, int naction,string addr, string id);

public event TransfDelegate TransfEvent;

按钮触发事件

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxName.Text.Length == 0)
            {
                MessageBox.Show("姓名不能为空");
                return;
            }

            if (textBoxWeight.Text.Length == 0)
            {
                MessageBox.Show("体重不能为空");
                return;
            }

            name = textBoxName.Text;
            weight = Convert.ToDouble(textBoxWeight.Text);
            action = comboBoxAction.SelectedIndex;
            address = comboBoxAddress.SelectedItem.ToString();
            deviceid = comboBoxDevice.SelectedItem.ToString();

            //触发事件,传递样本名字,体重,动作,地址,设备号
            TransfEvent(name, weight, action, address, deviceid);
            this.Close();
        }

 

主窗体中按钮事件打开参数设置窗体form4,在事件中处理传递过来的参数

        private void buttonSave_Click(object sender, EventArgs e)
        {
            fillPatientInfo();

            //save data to database

           ......

  }

 

        private void fillPatientInfo()
        {
            Form4 logForm = new Form4();
            logForm.TransfEvent += frm_TransfEvent;
            logForm.ShowDialog();
        }

        //事件处理方法
        void frm_TransfEvent(string value, double dv, int naction, string addr, string id)
        {
            name = value;
            weight = dv;
            action = naction;
            address = addr;
            deviceid = id;
        }

 

posted on 2017-09-13 21:08  不忘初心,知耻后勇  阅读(202)  评论(0编辑  收藏  举报

导航