现在的位置: 首页 > 综合 > 正文

可在多线程下TextBox中显示信息,和控制滚动的一个函数

2011年12月15日 ⁄ 综合 ⁄ 共 998字 ⁄ 字号 评论关闭
一、可在多线程下调用显示信息。
二、可以滚动到添加的最后一行。
三、不添加新行,仅在最后一行动态显示动容。
变量messageBox的类型是TextBox

 1         private void showMsg(string msg, bool needScroll)
 2         {
 3             if (this.InvokeRequired == true)
 4             {
 5                 ShowMsgDelegate smd = new ShowMsgDelegate(showMsg);
 6                 this.Invoke(smd, msg, needScroll);
 7             }
 8             else
 9             {
10                 if (needScroll == true)
11                 {
12                     messageBox.AppendText(msg + "\r\n");
13                     messageBox.SelectionStart = messageBox.Text.Length;
14                 }
15                 else
16                 {
17                     int li = messageBox.Text.LastIndexOf("\r\n"+ "\r\n".Length;
18                     messageBox.SelectionStart = li;
19                     messageBox.SelectionLength = messageBox.Text.Length - li;
20                     messageBox.SelectedText = msg;
21                 }
22             }
23             Application.DoEvents();
24         }

抱歉!评论已关闭.