情景:有form1与form2两个窗体,启动form1 form1的button.click打开form2,在form1的textbox中输入文本在form2的textbox中同步显示。问题1:当我想通过创建线程+showDialog(代码注释部分)的方式时,在form1中输入文本在form2中不能同步显示。
     当使用 f2.show()方法时是可以显示的。
问题2:使用f2.shhow()时 当我把DST = f2.setContent这句代码放在showForm2_Click事件中 功能能实现,当放在textBox1_KeyUp事件中却不能实现高人求解释代码如下
   from1
        //在另一个线程中打开Form2
        //delegate void DelegateShowForm();
        //public void ShowForm()
        //{
        //    Form2 f2 = new Form2();
        //    f2.ShowDialog();
        //}
#region 同步委托声明
        public delegate void DelegateSetText(string T);
        Form2 f2 = new Form2();
        DelegateSetText DST;
#endregion        private void showForm2_Click(object sender, EventArgs e)
        {
            //DelegateShowForm DSF = new DelegateShowForm(ShowForm);
            //ThreadStart Ts = new ThreadStart(DSF);
            //Thread th = new Thread(Ts);
            //th.Start();

            Form2 f2 = new Form2();
            f2.Show();
            DST = f2.setContent;
        }
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            string text = textBox1.Text;
            DST(text);
        }form2
         public void setContent(string T)
        {
            this.textBox1.Text = T;
        }多线程WinForm委托

解决方案 »

  1.   

    写的好乱呀   请参考:http://blog.csdn.net/hellboy419/article/details/9096231
      

  2.   

    问题1:
    没有你这样做的,最好不要使用多线程操作UI,一般都只使用一个UI线程(也就是main方法中Application.Run()方法为入口进去的线程)问题2:
    如果把 DST = f2.setContent  放在 DST(text);之前
    也就是
            private void textBox1_KeyUp(object sender, KeyEventArgs e)
            {
                string text = textBox1.Text;
                DST = f2.setContent; //这里
                DST(text);
            }
    可以另外 Show() 和 ShowDialog()有很大的区别,详细请参考http://www.cnblogs.com/xiaozhi_5638/archive/2013/01/03/2843374.html