C# winfrom 通过委托插入文本

 delegate void ShowMessageDelegate(RichTextBox tb, string msg);
        /// <summary>
        /// 委托RichTextBox 添加文本
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="msg"></param>
        private void ShowMessage(RichTextBox tb, string msg)
        {
            if (tb.InvokeRequired)
            {
                //ShowMessageDelegate myDelegate = ShowMessage; //方法1
                ShowMessageDelegate myDelegate = new ShowMessageDelegate(ShowMessage); //方法2
                tb.Invoke(myDelegate, new object[] { tb, msg });
            }
            else
            {

                //tb.Text = tb.Text.Insert(0, msg + "\r\n");
                tb.Text += msg + "\r\n";
            }
        }

 

posted @ 2022-03-02 11:09  学竹  阅读(102)  评论(0编辑  收藏  举报