在winform 中 我点一个按钮 给一个TextBox 赋值可能按钮事件中的处理需要很长时间,比如说10分钟,我需要向TextBox输出处理信息。现在问题是,按钮事件执行完毕后TextBox才会显示输出的信息,我想做到事件处理的时候就逐行输出到TextBox   
不知道我表达明白没?

解决方案 »

  1.   

    赋值完毕加一句
    Application.DoEvents();
      

  2.   

    如果用线程处理的话,那你更新控件的时候得用委托来做
    Google "invokerequired"
      

  3.   

    表达基本明白我随便给你发个例子,这是我当时遇到这个问题写的一个Demo,呵呵 private MyMethod my;
            static Form2 f2 = null;
            static Timer timer = null;
            static bool result = false;
            public Form1()
            {
                timer = new Timer();
                timer.Interval = 10;
                timer.Tick += new EventHandler(timer_tick);
                timer.Start();
                InitializeComponent();
                my = new MyMethod(method);
            }
            private int method()
            {
                System.Threading.Thread.Sleep(5000);
                return 100;
            }
            private void MethodComplete(IAsyncResult iar)
            {
                if (iar == null) return;
                if (tb5.InvokeRequired)
                {
                    write writeIntoTB = new write(RefreshTB);
                    tb5.BeginInvoke(writeIntoTB, (iar.AsyncState as MyMethod).EndInvoke(iar).ToString());
                }
                else
                {
                    RefreshTB((iar.AsyncState as MyMethod).EndInvoke(iar).ToString());
                }
                result = true;
            }
            private void RefreshTB(string s)
            {
                tb5.Clear();
                tb5.Text = s;
            }
            private void bt5_Click(object sender, EventArgs e)
            {
                result = false;
                IAsyncResult iar = my.BeginInvoke(MethodComplete, my);
                f2 = new Form2();
                f2.ShowDialog();
            }
            private void timer_tick(object sender, EventArgs e)
            {
                if (result)
                {
                    if (f2 != null)
                    {
                        f2.Close();
                    }
                }
            }