天書的博客
C++博客
首頁
新文章
新隨筆
聚合
管理
posts - 124, comments - 29, trackbacks - 0
c# 兩個TextBox 同步滾動 實現(xiàn)行號功能
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
//
根據(jù)行號確定光標(biāo)索引
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
//
定位光標(biāo)所在行號取其前面的子串
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
//
定位光標(biāo)所在行號取其前面的子串
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
//
定位光標(biāo)所在行號取其前面的子串
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
}
posted on 2008-09-12 16:52
天書
閱讀(2182)
評論(1)
編輯
收藏
引用
FeedBack:
#
re: c# 兩個TextBox 同步滾動 實現(xiàn)行號功能
2013-06-17 10:31 |
請輸入你的姓名
提交失敗后,可以通過“恢復(fù)上次提交”恢復(fù)剛剛提交的內(nèi)容
回復(fù)
更多評論
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
【推薦】100%開源!大型工業(yè)跨平臺軟件C++源碼提供,建模,組態(tài)!
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
<
2025年8月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
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
1
2
3
4
5
6
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(5)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2013年11月 (2)
2013年10月 (2)
2013年4月 (1)
2010年12月 (1)
2010年11月 (1)
2010年10月 (2)
2010年8月 (1)
2010年6月 (2)
2010年5月 (3)
2010年4月 (4)
2010年3月 (1)
2010年2月 (4)
2010年1月 (4)
2009年11月 (1)
2009年9月 (2)
2009年7月 (1)
2009年6月 (1)
2009年4月 (1)
2009年2月 (9)
2008年12月 (2)
2008年11月 (6)
2008年10月 (15)
2008年9月 (29)
2008年6月 (17)
2008年5月 (3)
2008年4月 (6)
2008年3月 (3)
文章分類
Direct3D(1)
文章檔案
2013年4月 (1)
2008年6月 (1)
2008年3月 (1)
好友的Bolg
韓全磊的技術(shù)Blog
搜索
最新評論
1.?re: 插件化開發(fā)——接口和反射[未登錄]
評論內(nèi)容較長,點擊標(biāo)題查看
--x
2.?re: 觸發(fā)器作用 一種特殊的存儲過程,它在插入,刪除或修改特定表中的數(shù)據(jù)時觸發(fā)執(zhí)行
thank you
--enen
3.?re: C#屬性解析——綜合“公有字段的簡單而直接的表達(dá)式"和get和set函數(shù)提供的控制權(quán)"而產(chǎn)生的
很詳細(xì)
--www
4.?re: C# winform DevExpress GridControl GridView大批量數(shù)據(jù)(20萬條)導(dǎo)出Excel[未登錄]
導(dǎo)出的數(shù)據(jù)打不開啊,不是EXCEL啊
--李
5.?re: C# textbox 屬性 TabStop 控制輸入焦點
sdfs
--78
閱讀排行榜
1.?DateTimePicker 控件的格式設(shè)置 CustomFormat yyyy-MM-dd HH:mm:ss 月大寫M,分鐘小寫m,小時H代表24小時計算,h代表12小時計算(20854)
2.?DevExpress.XtraCharts 使用心得(9964)
3.?c# 中treeview 樹節(jié)點圖標(biāo)的動態(tài)加載,及選中時圖標(biāo)改變(7872)
4.?C# winform DevExpress GridControl GridView大批量數(shù)據(jù)(20萬條)導(dǎo)出Excel(7623)
5.?DevExpress TreeList 調(diào)優(yōu)_綁定數(shù)據(jù)源方式, 放棄原來的AppendNode加載數(shù)據(jù)的方式(6790)
評論排行榜
1.?D3D中鏡面反射效果實現(xiàn)(3)
2.?C# 多行textbox 按回車鍵提取光標(biāo)所在行字符串且在最上面一行顯示,光標(biāo)回歸第一行(3)
3.?c# 圓形按鈕制作——Region屬性(3)
4.?C# textbox 屬性 TabStop 控制輸入焦點(2)
5.?c# 兩個TextBox 同步滾動 實現(xiàn)行號功能 (1)
Copyright ©2025 天書 Powered By
博客園
模板提供:
滬江博客
久久久精品久久久久特色影视
|
久久人妻少妇嫩草AV蜜桃
|
品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美
|
99久久无码一区人妻a黑
|
国产精品成人无码久久久久久
|
最新久久免费视频
|
精品久久久久久久无码
|
蜜臀久久99精品久久久久久
|
嫩草伊人久久精品少妇AV
|
国产午夜福利精品久久
|
久久香蕉超碰97国产精品
|
亚洲精品乱码久久久久久不卡
|
久久A级毛片免费观看
|
亚洲国产日韩欧美久久
|
夜夜亚洲天天久久
|
亚洲中文字幕久久精品无码喷水
|
久久午夜夜伦鲁鲁片免费无码影视
|
丰满少妇人妻久久久久久
|
久久久噜噜噜久久中文字幕色伊伊
|
一级a性色生活片久久无
|
国产精品久久久久久
|
四虎国产精品成人免费久久
|
香蕉久久一区二区不卡无毒影院
|
日韩乱码人妻无码中文字幕久久
|
亚洲欧美一级久久精品
|
国内精品久久久久久麻豆
|
2021久久精品国产99国产精品
|
久久久久波多野结衣高潮
|
日韩影院久久
|
亚洲国产成人久久综合野外
|
久久本道久久综合伊人
|
国产精品美女久久久久av爽
|
色偷偷888欧美精品久久久
|
99久久综合狠狠综合久久止
|
精品国际久久久久999波多野
|
久久综合给合久久狠狠狠97色69
|
国产成人精品综合久久久久
|
久久精品国产亚洲AV影院
|
国内高清久久久久久
|
久久精品免费一区二区
|
狠狠色婷婷久久一区二区
|