1
private void GetLine(TextBox txtCmdInput)//取控件里鼠標所在行的命令發(fā)送后提到最前
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
//處理剩下的字符串,注意把開頭結(jié)尾的"\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 - 1, 1);
29
}
30
}
31
if (strLeft != "")
32
{
33
while (strLeft[0] == '\r')
34
{
35
strLeft = strLeft.Remove(0, 2);
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 - 1, 1);
44
}
45
}
46
String strNew = curRowText + "\r\n" + strLeft;
47
//最后前面留一行空格且把鼠標定位到此
48
txtCmdInput.Text = "\r\n" + strNew;
49
}

2



3

4

5

6

7

8

9

10

11

12

13



14

15

16

17



18

19

20

21

22

23

24

25



26

27



28

29

30

31

32



33

34



35

36

37

38

39

40



41

42



43

44

45

46

47

48

49

接著引發(fā)textbox控件的KeyDown事件
private void txtCmdInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//發(fā)送光標所在行指令,且把它提到頭一行
GetLine(txtCmdInput);
e.SuppressKeyPress = true;//回車事件已經(jīng)處理完不再響應(yīng)了
}
}