1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
9
namespace VSGround
10

{
11
public partial class Form1 : Form
12
{
13
public Form1()
14
{
15
InitializeComponent();
16
}
17
18
private void Form1_Load(object sender, EventArgs e)
19
{
20
txtInput.Focus();
21
//txt2.TabIndex = 0;
22
}
23
private int GetRNCount(String str)
24
{
25
int RNnum = 0;
26
for(int i = 0; i<str.Length; i++)
27
{
28
if (str[i] == '\r')
29
{
30
RNnum++;
31
}
32
}
33
return RNnum;
34
}
35
//根據行號確定光標索引
36
private int GetCurIndex(int curRow)
37
{
38
int curIndex = 0;
39
if (curRow < 10)
40
{
41
curIndex = 3 * (curRow - 1);
42
}
43
else if (curRow < 100)
44
{
45
curIndex = (10 - 1) * 3 + (curRow - 10) * 4;
46
}
47
else if (curRow < 1000)
48
{
49
curIndex = (10 - 1) * 3 + (100-1) * 4 + (curRow - 100)*5;
50
}
51
else if (curRow < 10000)
52
{
53
curIndex = (10 - 1) * 3 + (100 - 1) * 4 + (1000 - 1) * 5 + (curRow - 1000) * 6;
54
}
55
else
56
{
57
curIndex = (10 - 1) * 3 + (100 - 1) * 4 + (1000 - 1) * 5 + (10000 - 1) * 6 + (curRow - 10000) * 7;
58
}
59
return curIndex;
60
}
61
private void txtInput_TextChanged(object sender, EventArgs e)
62
{
63
txtLeft.Clear();
64
//兩個textbox中的滾動條保持同步
65
txtInput.ScrollToCaret();
66
67
//定位總行號
68
String str = txtInput.Text;
69
if (str != null && str.Length > 0)
70
{
71
int countRN = GetRNCount(str);
72
for(int j = 0; j < countRN+1; j++)
73
{
74
txtLeft.Text += (j + 1).ToString();
75
txtLeft.Text += "\r\n";
76
}
77
txtLeft.Text = txtLeft.Text.Substring(0, txtLeft.Text.Length - 2);
78
79
}
80
//定位光標所在行號取其前面的子串
81
int careIndex = txtInput.SelectionStart;
82
String careFrontStr = txtInput.Text.Substring(0, careIndex);
83
//分析子串有幾個回車換行符
84
int countSubRN = GetRNCount(careFrontStr);
85
int CurRow = countSubRN + 1;
86
txtLeft.SelectionStart = GetCurIndex(CurRow);
87
txtLeft.ScrollToCaret();
88
}
89
90
private void txtInput_KeyDown(object sender, KeyEventArgs e)
91
{
92
txtInput.ScrollToCaret();
93
if(e.KeyCode == Keys.Up)
94
{
95
//定位光標所在行號取其前面的子串
96
int careIndex = txtInput.SelectionStart;
97
String careFrontStr = txtInput.Text.Substring(0, careIndex);
98
//分析子串有幾個回車換行符
99
int countSubRN = GetRNCount(careFrontStr);
100
int CurRow = countSubRN ;
101
if(CurRow != 0)
102
{
103
txtLeft.SelectionStart = GetCurIndex(CurRow);
104
}
105
else
106
{
107
txtLeft.SelectionStart = GetCurIndex(CurRow+1);
108
}
109
110
txtLeft.ScrollToCaret();
111
}
112
else if(e.KeyCode == Keys.Down)
113
{
114
//定位光標所在行號取其前面的子串
115
int careIndex = txtInput.SelectionStart;
116
String careFrontStr = txtInput.Text.Substring(0, careIndex);
117
//分析子串有幾個回車換行符
118
int countSubRN = GetRNCount(careFrontStr);
119
int CurRow = countSubRN ;
120
txtLeft.SelectionStart = GetCurIndex(CurRow + 2);
121
txtLeft.ScrollToCaret();
122
}
123
}
124
}
125
}
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8

9
namespace VSGround10


{11
public partial class Form1 : Form12

{13
public Form1()14

{15
InitializeComponent();16
}17

18
private void Form1_Load(object sender, EventArgs e)19

{20
txtInput.Focus();21
//txt2.TabIndex = 0;22
}23
private int GetRNCount(String str)24

{25
int RNnum = 0;26
for(int i = 0; i<str.Length; i++)27

{28
if (str[i] == '\r')29

{30
RNnum++;31
}32
}33
return RNnum;34
}35
//根據行號確定光標索引36
private int GetCurIndex(int curRow)37

{38
int curIndex = 0;39
if (curRow < 10)40

{41
curIndex = 3 * (curRow - 1);42
}43
else if (curRow < 100)44

{45
curIndex = (10 - 1) * 3 + (curRow - 10) * 4;46
}47
else if (curRow < 1000)48

{49
curIndex = (10 - 1) * 3 + (100-1) * 4 + (curRow - 100)*5;50
}51
else if (curRow < 10000)52

{53
curIndex = (10 - 1) * 3 + (100 - 1) * 4 + (1000 - 1) * 5 + (curRow - 1000) * 6;54
}55
else56

{57
curIndex = (10 - 1) * 3 + (100 - 1) * 4 + (1000 - 1) * 5 + (10000 - 1) * 6 + (curRow - 10000) * 7;58
}59
return curIndex;60
}61
private void txtInput_TextChanged(object sender, EventArgs e)62

{63
txtLeft.Clear();64
//兩個textbox中的滾動條保持同步65
txtInput.ScrollToCaret();66

67
//定位總行號68
String str = txtInput.Text;69
if (str != null && str.Length > 0)70

{71
int countRN = GetRNCount(str);72
for(int j = 0; j < countRN+1; j++)73

{74
txtLeft.Text += (j + 1).ToString();75
txtLeft.Text += "\r\n";76
}77
txtLeft.Text = txtLeft.Text.Substring(0, txtLeft.Text.Length - 2);78

79
}80
//定位光標所在行號取其前面的子串81
int careIndex = txtInput.SelectionStart;82
String careFrontStr = txtInput.Text.Substring(0, careIndex);83
//分析子串有幾個回車換行符84
int countSubRN = GetRNCount(careFrontStr);85
int CurRow = countSubRN + 1;86
txtLeft.SelectionStart = GetCurIndex(CurRow);87
txtLeft.ScrollToCaret();88
}89

90
private void txtInput_KeyDown(object sender, KeyEventArgs e)91

{92
txtInput.ScrollToCaret();93
if(e.KeyCode == Keys.Up)94

{95
//定位光標所在行號取其前面的子串96
int careIndex = txtInput.SelectionStart;97
String careFrontStr = txtInput.Text.Substring(0, careIndex);98
//分析子串有幾個回車換行符99
int countSubRN = GetRNCount(careFrontStr);100
int CurRow = countSubRN ;101
if(CurRow != 0)102

{103
txtLeft.SelectionStart = GetCurIndex(CurRow);104
}105
else106

{107
txtLeft.SelectionStart = GetCurIndex(CurRow+1);108
}109
110
txtLeft.ScrollToCaret();111
}112
else if(e.KeyCode == Keys.Down)113

{114
//定位光標所在行號取其前面的子串115
int careIndex = txtInput.SelectionStart;116
String careFrontStr = txtInput.Text.Substring(0, careIndex);117
//分析子串有幾個回車換行符118
int countSubRN = GetRNCount(careFrontStr);119
int CurRow = countSubRN ;120
txtLeft.SelectionStart = GetCurIndex(CurRow + 2);121
txtLeft.ScrollToCaret();122
}123
}124
}125
} 1
using System;
2
using System.Collections.Generic;
3
using System.Windows.Forms;
4
using System.Text;
5
6
namespace VSGround
7

{
8
class MyTextBox:System.Windows.Forms.TextBox
9
{
10
public MyTextBox():base()
11
{
12
13
}
14
protected override bool IsInputKey(System.Windows.Forms.Keys KeyData)
15
{
16
if (KeyData == System.Windows.Forms.Keys.Up ||
17
KeyData == System.Windows.Forms.Keys.Down)
18
return true;
19
return base.IsInputKey(KeyData);
20
}
21
}
22
}
using System;2
using System.Collections.Generic;3
using System.Windows.Forms;4
using System.Text;5

6
namespace VSGround7


{8
class MyTextBox:System.Windows.Forms.TextBox9

{10
public MyTextBox():base()11

{12

13
}14
protected override bool IsInputKey(System.Windows.Forms.Keys KeyData)15

{16
if (KeyData == System.Windows.Forms.Keys.Up ||17
KeyData == System.Windows.Forms.Keys.Down)18
return true;19
return base.IsInputKey(KeyData);20
}21
}22
}

