[C#]richtextbox实现行号

简介:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
editorControl是一个userControl,其包含两个控件:左侧是一个用来显示行号的RichTextBox(使用label等均可),右侧是一个继承自RichTextBox的componenteditorGrid1。
 
/*实现行号 begin*/
 
(1) 添加事件
         private  void  richTextBoxMain_TextChanged( object  sender, EventArgs e)
         {
             updateLabelRowIndex();
         }
 
         private  void  richTextBoxMain_FontChanged( object  sender, EventArgs e)
         {
             updateLabelRowIndex();
             richTextBoxMain_VScroll( null null );
         }
 
         private  void  richTextBoxMain_Resize( object  sender, EventArgs e)
         {
             richTextBoxMain_VScroll( null null );
         }
 
         private  void  richTextBoxMain_VScroll( object  sender, EventArgs e)
         {
             //move location of numberLabel for amount of pixels caused by scrollbar
             int  p = richTextBoxMain.GetPositionFromCharIndex(0).Y % (richTextBoxMain.Font.Height + 1);
             labelRowIndex.Location =  new  Point(0,p);
             updateLabelRowIndex();
         }
 
(2)更新行号的函数
 
         private  void  updateLabelRowIndex()
         {
             //we get index of first visible char and number of first visible line
             Point pos =  new  Point(0,0);
             int  firstIndex =  this .richTextBoxMain.GetCharIndexFromPosition(pos);
             int  firstLine =  this .richTextBoxMain.GetLineFromCharIndex(firstIndex);
 
             //now we get index of last visible char and number of last visible line
             pos.X +=  this .richTextBoxMain.ClientRectangle.Width;
             pos.Y +=  this .richTextBoxMain.ClientRectangle.Height;
             int  lastIndex =  this .richTextBoxMain.GetCharIndexFromPosition(pos);
             int  lastLine =  this .richTextBoxMain.GetLineFromCharIndex(lastIndex);
 
             //this is point position of last visible char,
             //we'll use its Y value for calculating numberLabel size
             pos =  this .richTextBoxMain.GetPositionFromCharIndex(lastIndex);
 
             labelRowIndex.Text =  "" ;
             for  ( int  i = firstLine; i <= lastLine +1 ; i++)
             {
                 labelRowIndex.Text += i + 1 +  "\r\n" ;
             }
         }    
         /*end*/
本文转自静默虚空博客园博客,原文链接:http://www.cnblogs.com/jingmoxukong/articles/2118109.html,如需转载请自行联系原作者

相关文章
|
C# 数据库
C# 让RichTextBox支持GIF
我只是做了一些简单的测试...有疑问给我发消息把. 使用方法 //获取选择的图形 并且保存出来 private void button2_Click(object sender, EventArgs e)    ...
974 0
|
C# 数据库 内存技术
C# 将RichTextBox中内容的文档以二进制形式存
private void button1_Click(object sender, EventArgs e)        {              System.IO.MemoryStream mstream = new System.
898 0
|
C#
C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色
在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示 和错误等类别。为了更好地区分不同类型的日志,我们需要使用不同的颜色来输出对应的日志,比如:一般消息为绿色,警告提示的用橙色,错误的用红色字体。
1940 0
|
C# Windows
如何在C#下利用RichTextBox打开一个有文字格式和图片的Word文档
转自博客: http://blog.csdn.net/michellehsiao/article/details/7684309 小知识点:.Net Framework 4.0 和.Net Framework 4.0 Client Profile区别:       .NET Framework Client Profile是.NET Framework的裁剪版本。
1279 0
|
C# Windows
c# richTextBox显示一个txt文档出现中文乱码
1、参考解决方案 怎么读的呢? 如果是从文本中读的,考虑一下编码问题 FileStream fs = new FileStream(@"c:\你的文本.txt", FileMode.
2865 0
|
C#
[C#]richtextbox实现拖放
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); richTextBox1.
742 0
|
C# 移动开发
[C#]richtextbox实现行号
editorControl是一个userControl,其包含两个控件:左侧是一个用来显示行号的RichTextBox(使用label等均可),右侧是一个继承自RichTextBox的componenteditorGrid1。
914 0
|
9天前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。