posts - 124,  comments - 29,  trackbacks - 0

 

 1 private void GetLine(TextBox txtCmdInput)//取控件里鼠标所在行的命令发送后提到最前
 2        {
 3            //取光标所在行的字符串包括末尾的换行回车符"\r\n"
 4            string strCmdText = txtCmdInput.Text;
 5            int curInx = txtCmdInput.SelectionStart;       //光标所在位置索引
 6            string tmp = strCmdText.Substring(0, curInx);  //开始到光标处的子串
 7            int start = tmp.LastIndexOf('\n');             //找光标所在行的开头索引start + 1
 8
 9            tmp = strCmdText.Substring(curInx);//当前光标所在位置到最后的子串
10            int end = tmp.IndexOf('\n'); //找该行的末尾索引包括"\r\n"
11            string curRowText = null;
12            if (end > 0)
13            {
14                curRowText = strCmdText.Substring(start + 1, curInx - start + end);
15            }

16            else
17            {
18                curRowText = strCmdText.Substring(start + 1);
19            }

20            //把光标所在行的命令提到第一行的下一行
21            String strLeft = strCmdText.Remove(start + 1, curRowText.Length);
22
23            //处理剩下的字符串,注意把开头结尾的"\r\n"找到删掉
24            if (strLeft != "")
25            {
26                while (strLeft[strLeft.Length - 1== '\r' || strLeft[strLeft.Length - 1== '\n')
27                {
28                    strLeft = strLeft.Remove(strLeft.Length - 11);
29                }

30            }

31            if (strLeft != "")
32            {
33                while (strLeft[0== '\r')
34                {
35                    strLeft = strLeft.Remove(02);
36                }

37            }

38            //处理你取出的当前行的字符串若有"\r\n"注意把它去掉
39            if (curRowText != "" && curRowText.Length > 0)
40            {
41                while (curRowText[curRowText.Length - 1== '\r' || curRowText[curRowText.Length - 1== '\n')
42                {
43                    curRowText = curRowText.Remove(curRowText.Length - 11);
44                }

45            }

46            String strNew = curRowText + "\r\n" + strLeft;
47            //最后前面留一行空格且把鼠标定位到此
48            txtCmdInput.Text = "\r\n" + strNew;
49        }
  

接着引发textbox控件的KeyDown事件
        private void txtCmdInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //发送光标所在行指令,且把它提到头一行
                GetLine(txtCmdInput);
                e.SuppressKeyPress = true;//回车事件已经处理完不再响应了
            }
        }
posted on 2008-09-03 15:09 天书 阅读(4793) 评论(3)  编辑 收藏 引用

FeedBack:
# re: C# 多行textbox 按回车键提取光标所在行字符串且在最上面一行显示,光标回归第一行
2009-02-27 13:59 | 斯比克
学习~~  回复  更多评论
  
# re: C# 多行textbox 按回车键提取光标所在行字符串且在最上面一行显示,光标回归第一行
2009-02-27 14:01 | 斯比克
呵呵,虽然不是我需要的效果..但是给我提供了思路...3Q...  回复  更多评论
  
# re: C# 多行textbox 按回车键提取光标所在行字符串且在最上面一行显示,光标回归第一行
2009-10-09 18:02 | 我们学习网
按回车光标转向下一控件。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜