[C#]richtextbox实现行号

简介: editorControl是一个userControl,其包含两个控件:左侧是一个用来显示行号的RichTextBox(使用label等均可),右侧是一个继承自RichTextBox的componenteditorGrid1。
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*/
目录
相关文章
|
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.
897 0
|
C#
C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色
在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示 和错误等类别。为了更好地区分不同类型的日志,我们需要使用不同的颜色来输出对应的日志,比如:一般消息为绿色,警告提示的用橙色,错误的用红色字体。
1937 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的裁剪版本。
1276 0
|
C# Windows
c# richTextBox显示一个txt文档出现中文乱码
1、参考解决方案 怎么读的呢? 如果是从文本中读的,考虑一下编码问题 FileStream fs = new FileStream(@"c:\你的文本.txt", FileMode.
2860 0
|
C#
[C#]richtextbox实现拖放
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); richTextBox1.
741 0
|
1月前
|
C#
24. C# 编程:用户设定敌人初始血值的实现
24. C# 编程:用户设定敌人初始血值的实现
18 0