c# textbox中光标所在行命令及选中命令移动到最后一行且光标提前[转]
  1 private void MoveCmdToLast(TextBox txtCmdInput, String selCmd)//把所选中的命令移动到最下一行然后显示在文本框中
 private void MoveCmdToLast(TextBox txtCmdInput, String selCmd)//把所选中的命令移动到最下一行然后显示在文本框中
2 {
        {
3 String txtStr = txtCmdInput.Text;
            String txtStr = txtCmdInput.Text;
4 int start = txtStr.IndexOf(selCmd);
            int start = txtStr.IndexOf(selCmd);
5
6 //把选中行或光标所在行的命令送到最后一行且光标移到第一行
            //把选中行或光标所在行的命令送到最后一行且光标移到第一行
7 if (selCmd != null && selCmd.Length > 0 && selCmd != "\r\n")
            if (selCmd != null && selCmd.Length > 0 && selCmd != "\r\n")
8 {
            {
9 String strLeft = txtStr.Remove(start, selCmd.Length);
                String strLeft = txtStr.Remove(start, selCmd.Length);
10
11 //处理剩下的字符串,注意把开头结尾的"\r\n"找到删掉
                //处理剩下的字符串,注意把开头结尾的"\r\n"找到删掉
12
13 while (strLeft != null && strLeft.Length > 0 && (strLeft[strLeft.Length - 1] == '\r' || strLeft[strLeft.Length - 1] == '\n'))
                while (strLeft != null && strLeft.Length > 0 && (strLeft[strLeft.Length - 1] == '\r' || strLeft[strLeft.Length - 1] == '\n'))
14 {
                {
15 strLeft = strLeft.Remove(strLeft.Length - 1, 1);
                    strLeft = strLeft.Remove(strLeft.Length - 1, 1);
16 }
                }
17
18 while (strLeft != null && strLeft.Length > 0 && strLeft[0] == '\r')
                while (strLeft != null && strLeft.Length > 0 && strLeft[0] == '\r')
19 {
                {
20 strLeft = strLeft.Remove(0, 2);
                    strLeft = strLeft.Remove(0, 2);
21 }
                }
22
23 //处理你取出的当前行的字符串若有"\r\n"注意把它去掉
                //处理你取出的当前行的字符串若有"\r\n"注意把它去掉
24 while (selCmd != "" && selCmd.Length > 0 &&
                while (selCmd != "" && selCmd.Length > 0 &&
25 (selCmd[selCmd.Length - 1] == '\r'
                       (selCmd[selCmd.Length - 1] == '\r'
26 || selCmd[selCmd.Length - 1] == '\n'))
                       || selCmd[selCmd.Length - 1] == '\n'))
27 {
                {
28 selCmd = selCmd.Remove(selCmd.Length - 1, 1);
                    selCmd = selCmd.Remove(selCmd.Length - 1, 1);
29 }
                }
30
31 String strNew = strLeft + "\r\n" + selCmd;
                String strNew = strLeft + "\r\n" + selCmd;
32 //最后前面留一行空格且把鼠标定位到此
                //最后前面留一行空格且把鼠标定位到此
33 txtCmdInput.Text = "\r\n" + strNew;
                txtCmdInput.Text = "\r\n" + strNew;
34 }
            }
35 else
            else
36 {
            {
37 MessageBox.Show("请您不要发送空命令,谢谢合作!", "温馨提示:");
                MessageBox.Show("请您不要发送空命令,谢谢合作!", "温馨提示:");
38 }
            }
39 }
        }
40 private String GetCmd(TextBox txtCmdInput)//取控件里鼠标所在行或光标所选择的命令,发送的命令暂时写到文件中
        private String GetCmd(TextBox txtCmdInput)//取控件里鼠标所在行或光标所选择的命令,发送的命令暂时写到文件中
41 {
        {
42 string txtStr = txtCmdInput.Text;
            string txtStr = txtCmdInput.Text;
43 string selStr = txtCmdInput.SelectedText;
            string selStr = txtCmdInput.SelectedText;
44 string selCmd = null;
            string selCmd = null;
45 int start = 0;
            int start = 0;
46 if (selStr != null && selStr.Length > 0)
            if (selStr != null && selStr.Length > 0)
47 {
            {
48
49 int selBegin = txtStr.IndexOf(selStr);
                int selBegin = txtStr.IndexOf(selStr);
50 int selEnd = selBegin + selStr.Length - 1;
                int selEnd = selBegin + selStr.Length - 1;
51 string subPreStr = txtStr.Substring(0, selBegin);
                string subPreStr = txtStr.Substring(0, selBegin);
52 string subLastStr = txtStr.Substring(selEnd + 1);
                string subLastStr = txtStr.Substring(selEnd + 1);
53 string preleft = null;
                string preleft = null;
54 string lastleft = null;
                string lastleft = null;
55 if (subPreStr.Length > 0 && subPreStr[subPreStr.Length - 1] != '\n')
                if (subPreStr.Length > 0 && subPreStr[subPreStr.Length - 1] != '\n')
56 {
                {
57 int nindex = subPreStr.LastIndexOf("\n");
                    int nindex = subPreStr.LastIndexOf("\n");
58 preleft = subPreStr.Substring(nindex + 1);
                    preleft = subPreStr.Substring(nindex + 1); 
59 }
                }
60 if (subLastStr.Length > 0 && subLastStr[0] != '\r')
                if (subLastStr.Length > 0 && subLastStr[0] != '\r')
61 {
                {
62 int rindex = subLastStr.IndexOf("\r");
                    int rindex = subLastStr.IndexOf("\r");
63 if (rindex != -1)
                    if (rindex != -1)
64 {
                    {
65 lastleft = subLastStr.Substring(0, rindex + 2);
                        lastleft = subLastStr.Substring(0, rindex + 2);
66 }
                    }
67 else lastleft = null;
                    else lastleft = null;
68 }
                }
69 else if (subLastStr != null && subLastStr.Length > 0 && subLastStr[0] == '\r')
                else if (subLastStr != null && subLastStr.Length > 0 && subLastStr[0] == '\r')
70 {
                {
71 lastleft = "\r\n";
                    lastleft = "\r\n";
72 }
                }
73 selStr = preleft + selStr + lastleft;
                selStr = preleft + selStr + lastleft;
74 start = txtStr.IndexOf(selStr);
                start = txtStr.IndexOf(selStr);
75 selCmd = selStr;
                selCmd = selStr;
76 }
            }
77 else
            else
78 {
            {
79 //取光标所在行的字符串包括末尾的换行回车符"\r\n"
                //取光标所在行的字符串包括末尾的换行回车符"\r\n"
80 //string strCmdText = txtCmdInput.Text;
                //string strCmdText = txtCmdInput.Text;
81 int curInx = txtCmdInput.SelectionStart;       //光标所在位置索引
                int curInx = txtCmdInput.SelectionStart;       //光标所在位置索引
82 string tmp = txtStr.Substring(0, curInx);  //开始到光标处的子串
                string tmp = txtStr.Substring(0, curInx);  //开始到光标处的子串
83 int n = tmp.LastIndexOf('\n');             //找光标所在行的开头索引start + 1
                int n = tmp.LastIndexOf('\n');             //找光标所在行的开头索引start + 1
84 start = n + 1;
                start = n + 1;
85 tmp = txtStr.Substring(curInx);//当前光标所在位置到最后的子串
                tmp = txtStr.Substring(curInx);//当前光标所在位置到最后的子串
86 int end = tmp.IndexOf('\n'); //找该行的末尾索引包括"\r\n"
                int end = tmp.IndexOf('\n'); //找该行的末尾索引包括"\r\n"
87 string curRowText = null;
                string curRowText = null;
88 if (end > 0)
                if (end > 0)
89 {
                {
90 curRowText = txtStr.Substring(start, curInx - start + end + 1);
                    curRowText = txtStr.Substring(start, curInx - start + end + 1);
91 }
                }
92 else
                else
93 {
                {
94 curRowText = txtStr.Substring(start);
                    curRowText = txtStr.Substring(start);
95 }
                }
96 selCmd = curRowText;
                selCmd = curRowText;
97 }
            }
98 //MoveCmdToLast(txtStr,selCmd);
            //MoveCmdToLast(txtStr,selCmd);
99 //把光标所在行的命令写入文件中
            //把光标所在行的命令写入文件中
100 FileStream fs = new FileStream("D:\\file.txt",FileMode.Append,FileAccess.Write);
            FileStream fs = new FileStream("D:\\file.txt",FileMode.Append,FileAccess.Write);
101 StreamWriter sw = new StreamWriter(fs);
            StreamWriter sw = new StreamWriter(fs);
102 sw.Flush();
            sw.Flush();
103 sw.Write(selCmd);
            sw.Write(selCmd);
104 sw.Flush();
            sw.Flush();
105 sw.Close();
            sw.Close();
106 return selCmd;
            return selCmd;
107 }
        }  
 private void MoveCmdToLast(TextBox txtCmdInput, String selCmd)//把所选中的命令移动到最下一行然后显示在文本框中
 private void MoveCmdToLast(TextBox txtCmdInput, String selCmd)//把所选中的命令移动到最下一行然后显示在文本框中2
 {
        {3
 String txtStr = txtCmdInput.Text;
            String txtStr = txtCmdInput.Text;4
 int start = txtStr.IndexOf(selCmd);
            int start = txtStr.IndexOf(selCmd);5

6
 //把选中行或光标所在行的命令送到最后一行且光标移到第一行
            //把选中行或光标所在行的命令送到最后一行且光标移到第一行7
 if (selCmd != null && selCmd.Length > 0 && selCmd != "\r\n")
            if (selCmd != null && selCmd.Length > 0 && selCmd != "\r\n")8
 {
            {9
 String strLeft = txtStr.Remove(start, selCmd.Length);
                String strLeft = txtStr.Remove(start, selCmd.Length);10

11
 //处理剩下的字符串,注意把开头结尾的"\r\n"找到删掉
                //处理剩下的字符串,注意把开头结尾的"\r\n"找到删掉12

13
 while (strLeft != null && strLeft.Length > 0 && (strLeft[strLeft.Length - 1] == '\r' || strLeft[strLeft.Length - 1] == '\n'))
                while (strLeft != null && strLeft.Length > 0 && (strLeft[strLeft.Length - 1] == '\r' || strLeft[strLeft.Length - 1] == '\n'))14
 {
                {15
 strLeft = strLeft.Remove(strLeft.Length - 1, 1);
                    strLeft = strLeft.Remove(strLeft.Length - 1, 1);16
 }
                }17

18
 while (strLeft != null && strLeft.Length > 0 && strLeft[0] == '\r')
                while (strLeft != null && strLeft.Length > 0 && strLeft[0] == '\r')19
 {
                {20
 strLeft = strLeft.Remove(0, 2);
                    strLeft = strLeft.Remove(0, 2);21
 }
                }22

23
 //处理你取出的当前行的字符串若有"\r\n"注意把它去掉
                //处理你取出的当前行的字符串若有"\r\n"注意把它去掉24
 while (selCmd != "" && selCmd.Length > 0 &&
                while (selCmd != "" && selCmd.Length > 0 &&25
 (selCmd[selCmd.Length - 1] == '\r'
                       (selCmd[selCmd.Length - 1] == '\r'26
 || selCmd[selCmd.Length - 1] == '\n'))
                       || selCmd[selCmd.Length - 1] == '\n'))27
 {
                {28
 selCmd = selCmd.Remove(selCmd.Length - 1, 1);
                    selCmd = selCmd.Remove(selCmd.Length - 1, 1);29
 }
                }30

31
 String strNew = strLeft + "\r\n" + selCmd;
                String strNew = strLeft + "\r\n" + selCmd;32
 //最后前面留一行空格且把鼠标定位到此
                //最后前面留一行空格且把鼠标定位到此33
 txtCmdInput.Text = "\r\n" + strNew;
                txtCmdInput.Text = "\r\n" + strNew;34
 }
            }35
 else
            else36
 {
            {37
 MessageBox.Show("请您不要发送空命令,谢谢合作!", "温馨提示:");
                MessageBox.Show("请您不要发送空命令,谢谢合作!", "温馨提示:");38
 }
            }39
 }
        }40
 private String GetCmd(TextBox txtCmdInput)//取控件里鼠标所在行或光标所选择的命令,发送的命令暂时写到文件中
        private String GetCmd(TextBox txtCmdInput)//取控件里鼠标所在行或光标所选择的命令,发送的命令暂时写到文件中41
 {
        {42
 string txtStr = txtCmdInput.Text;
            string txtStr = txtCmdInput.Text;43
 string selStr = txtCmdInput.SelectedText;
            string selStr = txtCmdInput.SelectedText;44
 string selCmd = null;
            string selCmd = null;45
 int start = 0;
            int start = 0;46
 if (selStr != null && selStr.Length > 0)
            if (selStr != null && selStr.Length > 0)47
 {
            {48

49
 int selBegin = txtStr.IndexOf(selStr);
                int selBegin = txtStr.IndexOf(selStr);50
 int selEnd = selBegin + selStr.Length - 1;
                int selEnd = selBegin + selStr.Length - 1;51
 string subPreStr = txtStr.Substring(0, selBegin);
                string subPreStr = txtStr.Substring(0, selBegin);52
 string subLastStr = txtStr.Substring(selEnd + 1);
                string subLastStr = txtStr.Substring(selEnd + 1);53
 string preleft = null;
                string preleft = null;54
 string lastleft = null;
                string lastleft = null;55
 if (subPreStr.Length > 0 && subPreStr[subPreStr.Length - 1] != '\n')
                if (subPreStr.Length > 0 && subPreStr[subPreStr.Length - 1] != '\n')56
 {
                {57
 int nindex = subPreStr.LastIndexOf("\n");
                    int nindex = subPreStr.LastIndexOf("\n");58
 preleft = subPreStr.Substring(nindex + 1);
                    preleft = subPreStr.Substring(nindex + 1); 59
 }
                }60
 if (subLastStr.Length > 0 && subLastStr[0] != '\r')
                if (subLastStr.Length > 0 && subLastStr[0] != '\r')61
 {
                {62
 int rindex = subLastStr.IndexOf("\r");
                    int rindex = subLastStr.IndexOf("\r");63
 if (rindex != -1)
                    if (rindex != -1)64
 {
                    {65
 lastleft = subLastStr.Substring(0, rindex + 2);
                        lastleft = subLastStr.Substring(0, rindex + 2);66
 }
                    }67
 else lastleft = null;
                    else lastleft = null;68
 }
                }69
 else if (subLastStr != null && subLastStr.Length > 0 && subLastStr[0] == '\r')
                else if (subLastStr != null && subLastStr.Length > 0 && subLastStr[0] == '\r')70
 {
                {71
 lastleft = "\r\n";
                    lastleft = "\r\n";72
 }
                }73
 selStr = preleft + selStr + lastleft;
                selStr = preleft + selStr + lastleft;74
 start = txtStr.IndexOf(selStr);
                start = txtStr.IndexOf(selStr);75
 selCmd = selStr;
                selCmd = selStr;76
 }
            }77
 else
            else78
 {
            {79
 //取光标所在行的字符串包括末尾的换行回车符"\r\n"
                //取光标所在行的字符串包括末尾的换行回车符"\r\n"80
 //string strCmdText = txtCmdInput.Text;
                //string strCmdText = txtCmdInput.Text;81
 int curInx = txtCmdInput.SelectionStart;       //光标所在位置索引
                int curInx = txtCmdInput.SelectionStart;       //光标所在位置索引82
 string tmp = txtStr.Substring(0, curInx);  //开始到光标处的子串
                string tmp = txtStr.Substring(0, curInx);  //开始到光标处的子串83
 int n = tmp.LastIndexOf('\n');             //找光标所在行的开头索引start + 1
                int n = tmp.LastIndexOf('\n');             //找光标所在行的开头索引start + 184
 start = n + 1;
                start = n + 1;85
 tmp = txtStr.Substring(curInx);//当前光标所在位置到最后的子串
                tmp = txtStr.Substring(curInx);//当前光标所在位置到最后的子串86
 int end = tmp.IndexOf('\n'); //找该行的末尾索引包括"\r\n"
                int end = tmp.IndexOf('\n'); //找该行的末尾索引包括"\r\n"87
 string curRowText = null;
                string curRowText = null;88
 if (end > 0)
                if (end > 0)89
 {
                {90
 curRowText = txtStr.Substring(start, curInx - start + end + 1);
                    curRowText = txtStr.Substring(start, curInx - start + end + 1);91
 }
                }92
 else
                else93
 {
                {94
 curRowText = txtStr.Substring(start);
                    curRowText = txtStr.Substring(start);95
 }
                }96
 selCmd = curRowText;
                selCmd = curRowText;97
 }
            }98
 //MoveCmdToLast(txtStr,selCmd);
            //MoveCmdToLast(txtStr,selCmd);99
 //把光标所在行的命令写入文件中
            //把光标所在行的命令写入文件中100
 FileStream fs = new FileStream("D:\\file.txt",FileMode.Append,FileAccess.Write);
            FileStream fs = new FileStream("D:\\file.txt",FileMode.Append,FileAccess.Write);101
 StreamWriter sw = new StreamWriter(fs);
            StreamWriter sw = new StreamWriter(fs);102
 sw.Flush();
            sw.Flush();103
 sw.Write(selCmd);
            sw.Write(selCmd);104
 sw.Flush();
            sw.Flush();105
 sw.Close();
            sw.Close();106
 return selCmd;
            return selCmd;107
 }
        }  函数调用如下:
        private void txtCmdInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;    / /回车事件已经处理完不再响应了
                string str = GetCmd(txtCmdInput);
                MoveCmdToLast(txtCmdInput, str);
            }
        }
 
                    
                     
                    
                 
                    
                

 
             
                
            
         
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号