Fork me on GitHub

跨窗体传值

1、使用委托解决跨窗体传值

传值的过程中使用的是同一个线程,当这两个窗体分别想做一些其他处理又有数据沟通的时候,采用多线程实现跨窗体传值

           Thread thread = new Thread(() => {
                FormChild formChild = new FormChild();
                formChild.delSendToOtherForm = new DelSetToOtherForm(SetToOtherForm);
                formChild.ShowDialog();
            });

当使用多线程时,需要注意的是FormChild窗体是另一个线程创建的,设置FormParent窗体的属性,使其不捕获其他线程调用引发的异常,但是这样做不安全,接下来采用判断调用控件的线程来自哪里 

C# 模态窗体详细介绍

           //校验该控件创建的线程是否是当前线程,如果是则为false,反之true
            if (this.txtReceive.InvokeRequired)
            {
                //调用委托
                this.Invoke(delSetToOtherForm, txt);
            }
            else
            {
                this.txtReceive.Text = txt;
            }

这样就不用去设置是否去捕获异常

1.属性实现

        public string txtTest { get; set; }
        private void Form2_Load( object sender, EventArgs e )
        {
            textBox1.Text = txtTest;
        }

            Form2 form = new Form2();
            form.txtTest = textBox1.Text;
            form.Show();

2.委托实现

 public partial class FormChild : Form
    {
        public DelSetToOtherForm delSendToOtherForm;
        public FormChild()
        {
            InitializeComponent();
            //显示当前窗体所在线程的标识符
            this.Text +="当前线程所在标识符是:"+ Thread.CurrentThread.ManagedThreadId;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtSend.Text == string.Empty)
            {
                return;
            }
            else
            {
                delSendToOtherForm(txtSend.Text);
            }
        }
    }


    public delegate void DelSetToOtherForm(string txt);
    public partial class FormParent : Form
    {
        public FormParent()
        {
            InitializeComponent();
        }

        private void FormParent_Load(object sender, EventArgs e)
        {
            //设置txtReceive的ReadOnly属性
            this.txtReceive.ReadOnly =true;
            //显示当前窗体所在的线程
            this.Text += "当前线程所在标识符是:" + Thread.CurrentThread.ManagedThreadId;
            //不捕获其他线程调用引发的异常-这样写是不安全的
            Control.CheckForIllegalCrossThreadCalls = false; ;
        }
        public void SetToOtherForm(string txt)
        {
            this.txtReceive.Text = txt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(() => {
                FormChild formChild = new FormChild();
                formChild.delSendToOtherForm = new DelSetToOtherForm(SetToOtherForm);
                formChild.ShowDialog();
            });
            thread.Start();
        }
        ///哪个线程创建了FormChild窗体,FormChild窗体归其控制
        ///最简单的方式就是允许其他线程来访问当前线程创建的控件(指的是继承自Control基类的实例)-FormChild窗体窗体

3.使用invoke

namespace 跨窗体传值_多线程实现Invoke
{
    public delegate void DelSetToOtherForm(string txt);
    public partial class FormParent : Form
    {
        //声明一个委托
        private DelSetToOtherForm delSetToOtherForm;
        public FormParent()
        {
            InitializeComponent();
        }

        private void FormParent_Load(object sender, EventArgs e)
        {
            //显示当前窗体所在的标识符
            this.Text += "当前窗体的标识符是:" + Thread.CurrentThread.ManagedThreadId;
            //实例化该委托
            delSetToOtherForm = new DelSetToOtherForm(SetToOtherThread);
            //不捕获其他线程调用控件引发的异常
            //Control.CheckForIllegalCrossThreadCalls = false;
        }
        public void SetToOtherForm(string txt)
        {
            //校验该控件创建的线程是否是当前线程,如果是则为false,反之true
            if (this.txtReceive.InvokeRequired)
            {
                //调用委托
                this.Invoke(delSetToOtherForm, txt);
            }
            else
            {
                this.txtReceive.Text = txt;
            }
        }
        public void SetToOtherThread(string txt)
        {
            this.txtReceive.Text = txt;
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            //FormChild formChild = new FormChild();
            //formChild.delSendToOtherForm = new DelSetToOtherForm(SetToOtherForm);
            //formChild.Show();

            Thread thread = new Thread(() =>
            {
                FormChild formChild = new FormChild();
                formChild.delSendToOtherForm = new DelSetToOtherForm(SetToOtherForm);
                formChild.ShowDialog();
            });
            thread.Start();
        }
    }
}
 public partial class FormChild : Form
    {
        public DelSetToOtherForm delSendToOtherForm;
        public FormChild()
        {
            InitializeComponent();
        }

        private void FormChild_Load(object sender, EventArgs e)
        {
            //显示当前窗体所在的标识符
            this.Text += "当前窗体的标识符是:" + Thread.CurrentThread.ManagedThreadId;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            delSendToOtherForm(txtSend.Text);
        }
    }
}

 

posted @ 2013-09-17 20:17  种花生的读书人  阅读(389)  评论(0编辑  收藏  举报

该博客仅作为记录笔记,转载随意