如何做一个带有行数的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;
  }

你可能感兴趣的:(text)