如何做一个带有行数的RichTextBox

本文介绍了一个简单的源码生成器的设计思路,通过使用richTextBox控件实现代码编辑功能,并详细展示了如何实时更新代码行号及滚动条的最大值。

先看下效果:

只所以想到妖做这个,是因为目前打算写个源码生成器,为了以后编写代码方便,懒嘛!

用用到的控件richTextBox1,richTextBox2,vScrollBar1,hScrollBar1
不过目前下方的滚动条还未起作用,找不到方法了。再努力做吧!

相应的代码如下;

//========================获取行号
  int _currentLine = 0;
  public int CurrentLine
  {
   get{return _currentLine;}
   set{_currentLine = value;}
  }
  private void richTextBox1_TextChanged(object sender, System.EventArgs e)
  {
   updateNumber_Line();
  }
  private void updateNumber_Line()
  {
   Point pos = new Point(0, 0);
   int firstIndex = richTextBox1.GetCharIndexFromPosition(pos);
   int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex);//=======得到行号
   pos.X = this.richTextBox1.Width;
   pos.Y = this.richTextBox1.Height;
   int lastIndex = richTextBox1.GetCharIndexFromPosition(pos);
   int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex);
   int myStart = this.richTextBox1.SelectionStart;
   int myLine = this.richTextBox1.GetLineFromCharIndex(myStart) + 1;

   //============垂直滚动条
   this.vScrollBar1.Maximum = myLine;
   this.vScrollBar1.Minimum = 1;

   //============垂直滚动条
   int ViewedColumns = this.richTextBox1.Width;//===========可视列号
   this.hScrollBar1.Maximum = ViewedColumns;
   this.hScrollBar1.Minimum = 1;

   pos = richTextBox1.GetPositionFromCharIndex(lastIndex);
   if (lastIndex > _currentLine||lastIndex<_currentLine)
   {
    this.richTextBox2.Text = "";
    for (int i = firstLine; i <= lastLine + 1; i++)
    {
     this.richTextBox2.Text += i + 1 + "\n";
    }
   }
   _currentLine = lastIndex;
  }

转载于:https://www.cnblogs.com/kingeric/archive/2006/12/18/596196.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值