C# 委托 页面间传值

来源:互联网 发布:insert 添加多条数据 编辑:程序博客网 时间:2024/05/15 02:28

 ------------------------------------------------------------------------------------------------------------

form1
定义委托
定义事件
定义事件方法
在能得到值的地方写上你的事件方法,并把值做为参数

form2
定义form1的事件引用
在+=后面的方法里就可以取到form1的值了

 

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //定义委托
        public delegate void MyDelegate(object sender, MyEventArgs e);
        //定义事件
        public event MyDelegate MyEvent;    

        private void button1_Click(object sender, EventArgs e)     
        {           
            Form2 f2 = new Form2();     
            //实例化一个Form2;          
            MyEventArgs me=new MyEventArgs();    
            //自定义的事件数据类实例;       
            me.MyValue="this.textBox1.Text2222222222";
            me.V2 = "V2";
            //Form1中textBox的值传给事件数据类实例;    
            this.MyEvent += new MyDelegate(f2.SetTextValue);
            //事件订阅自己建的委托;       
            MyEvent(this,me); 
            //执行事件;       
            f2.Show();    
            //Form2显示    
        }   
    } 
    //
    public class MyEventArgs:EventArgs  
    {      
        public string MyValue="";

        public string V2
        {
            get;
            set;
        }
    }

 

    public partial class Form2 : Form
    {
        Form1 f1;
        public Form2()
        {
            InitializeComponent();
        }
        public void SetTextValue(object sender, MyEventArgs e)
        {
            MessageBox.Show(e.MyValue+e.V2);
        }

}

 

------------------------------------------------------------------------------------------
//========B画面=========   
public delegate void ValueChange(string drugCode, string drugName);
private ValueChange CallBack;

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public DrugSearchForm(ValueChange callBack)
        {
            InitializeComponent();
            // イベント取得
            this.CallBack = callBack;
        }

//A画面に値を渡す
                string externalDrugCode = this.DrugDataGridView.Rows[ht.RowIndex].Cells["EXTERNAL_DRUG_CODE"].Value.ToString();
                string drugName = this.DrugDataGridView.Rows[ht.RowIndex].Cells["DRUG_NAME"].Value.ToString();
this.CallBack(externalDrugCode, drugName);


//========A画面=========

        /// <summary>
        /// 一覧で値セット時
        /// </summary>
        /// <param name="drugCode">コード</param>
        /// <param name="drugName">名</param>
        private void CallBack(string drugCode, string drugName)
        {
            this.DrugCodeTextBox.Text = drugCode;
            this.DrugNameTextBox.Text = drugName;

        }


                // B画面呼出
                DrugSearchForm drugSearchForm = new DrugSearchForm(this.CallBack);

 

 

 

C# Windows Form 刷新父窗体

 

第一种方法:
用委托,
Form2和Form3是同一组    
Form2

C#代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace TestMouseMove{    public delegate void SetVisiableHandler();    public partial class Form2 : Form    {        public Form2()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            Form3 frm = new Form3(new SetVisiableHandler(SetVisiable));            frm.Show();        }        private void SetVisiable()        {            SetVisiable(this.label1, !this.label1.Visible);        }        private void SetVisiable(Control control, bool visiable)        {            if (this.Controls.Contains(control))            {                control.Visible = visiable;            }        }    }}


Form3

C#代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace TestMouseMove{    public partial class Form3 : Form    {        private SetVisiableHandler m_setVisible;        public Form3(SetVisiableHandler setvisible)        {            InitializeComponent();            this.m_setVisible = setvisible;        }        private void btnVisible_Click(object sender, EventArgs e)        {            if (this.m_setVisible != null)            {                this.m_setVisible();            }        }    }}


第二种方法:  
用变量,
Form4和Form5是同一组
Form4

C#代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace TestMouseMove{    public partial class Form4 : Form    {        public Form4()        {            InitializeComponent();        }        #region 子窗口刷新父窗口的值        private string strLabel1 = "";        public string StrLabel1        {            get            {                return strLabel1;            }            set            {                strLabel1 = value;                this.label1.Text = strLabel1;            }        }        #endregion        private void button1_Click(object sender, EventArgs e)        {            Form5 form5 = new Form5(this);//这里注意传个this            form5.Show();        }    }}


Form5

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  

namespace TestMouseMove  
{  
public partial class Form5 : Form  
    {  
        Form4 form4 = new Form4();  

public Form5(Form4 formFrm)//这个构造方法里有参数  
        {  
            form4 = formFrm; //这个必须要有  
            InitializeComponent();  
        }  

private void button1_Click(object sender, EventArgs e)  
        {  
            form4.StrLabel1 = this.textBox1.Text;  
        }  
    }  
}

第3种方法

遍历窗体查找

foreach (Form frm in Application.OpenForms)   
            {   
                if (frm.Name == "要找的状体")   
                {   
                    frm.Refresh();   
                }   
            }

 

 

-----------------------------------------------------------------------------------

 public class MockHostSettingEventArgs : EventArgs
    {
        public MockHostSettingEventArgs(MockHostController.CommandSetting commandSet)
        {
            this.CommandSet = commandSet;
        }

        public MockHostController.CommandSetting CommandSet
        {
            get;
            set;
        }
    }

——————————————————子页面传给父页面值———————————————————————————

父页面

        private void SetHostSettingButton_Click(object sender, EventArgs e)
        {

           //跳转子页面
            SetHostSettingForm ssf = new SetHostSettingForm(this);
            ssf.ShowDialog();
        }

   // 事件方法 接收数据

        public void OnGetHostSet(object sender, MockHostSettingEventArgs e)
        {
            lock (comSet)
            {
                this.comSet = e.CommandSet;
            }
        }

———————————————————————————————————————————————————————————————

子页面

//定义委托时间

        public event EventHandler<MockHostSettingEventArgs> MockHostSettingEvent;

        public SetHostSettingForm(MockHostForm Mockhostfrom)
        {
            InitializeComponent();

//订阅事件
            this.MockHostSettingEvent += new EventHandler<MockHostSettingEventArgs>(Mockhostfrom.OnGetHostSet);
        }

 

//传值

   if (this.MockHostSettingEvent != null)
                    {
                        this.MockHostSettingEvent(this, new MockHostSettingEventArgs(this.CmdSetting));
                    }

 

 

原创粉丝点击