如何在RichTextBox C#中显示一行的编号

我正在制作一个带代码突出显示的简单文本和脚本编辑器。 为此,我使用RichTextBox。 但我不知道如何让它在左侧显示行数,就像在VS或Notepad ++中一样。 有什么解决方案吗?

我尝试重新使用其他地方引用的codeproject文章中的代码,但是我看到的每个选项看起来都有些过于愚蠢。

所以我构建了另一个显示行号的RichTextBoxEx。

行号可以打开或关闭。 它很快。 它滚动干净。 您可以选择数字的颜色,渐变的背景颜色,边框粗细,字体,是否使用前导零。 您可以选择“显示”编号行或根据RTB中的硬换行编号。

例子:

alt text http://sofzh.miximages.com/c%23/13zcoz6.png

替代文字http://sofzh.miximages.com/c%23/wml2z9.png

替代文字http://sofzh.miximages.com/c%23/25i4x3o.png

它有局限性:它仅在左侧显示数字。 如果你关心的话,你可以毫不费力地改变它。

代码设计为C#项目。 虽然它是更大的“解决方案”(XPath可视化工具)的一部分,但自定义RichTextBox被打包为可分离的程序集,并且可以在新项目中使用。 在Visual Studio中,只需添加对DLL的引用,然后将其拖放到设计图面上即可。 您可以从较大的解决方案中丢弃其他代码。

看代码

我会将每行存储在一个具有发布到richtextbox的方法的类中。 在该方法中,您可以根据其在类中的位置来添加行号。

例如(非常粗略):

class myText { public List Lines; public string GetList() { StringBuilder sb = new StringBuilder(); int i = 0; foreach (string s in Lines) { sb.AppendFormat("{0}: {1}", i, s).AppendLine(); i++; } return sb.ToString(); } } 

Scintilla.Net http://scintillanet.codeplex.com/可能是最符合您需求的解决方案。 但是对于我的项目,我使用了Cheeso建议的解决方案(来自XPath visualizer的RichTextBoxEx)。 对于不是很大的文档来说,这很简单,也很快。 来自互联网的所有其他.net组件都非常慢。

简单的方法:

 Dim myArray = RichTextBox1.Text.Split() Dim cnt As Integer = 0 RichTextBox1.Clear() Do While cnt < myArray.Count RichTextBox1.AppendText(cnt & ":" & myArray(cnt) & vbNewLine) cnt = cnt + 1 Loop 
  public int getWidth() { int w = 25; // get total lines of richTextBox1 int line = richTextBox1.Lines.Length; if (line <= 99) { w = 20 + (int)richTextBox1.Font.Size; } else if (line <= 999) { w = 30 + (int)richTextBox1.Font.Size; } else { w = 50 + (int)richTextBox1.Font.Size; } return w; } public void AddLineNumbers() { // create & set Point pt to (0,0) Point pt = new Point(0, 0); // get First Index & First Line from richTextBox1 int First_Index = richTextBox1.GetCharIndexFromPosition(pt); int First_Line = richTextBox1.GetLineFromCharIndex(First_Index); // set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively pt.X = ClientRectangle.Width; pt.Y = ClientRectangle.Height; // get Last Index & Last Line from richTextBox1 int Last_Index = richTextBox1.GetCharIndexFromPosition(pt); int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index); // set Center alignment to LineNumberTextBox LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center; // set LineNumberTextBox text to null & width to getWidth() function value LineNumberTextBox.Text = ""; LineNumberTextBox.Width = getWidth(); // now add each line number to LineNumberTextBox upto last line for (int i = First_Line; i <= Last_Line + 2; i++) { LineNumberTextBox.Text += i + 1 + "\n"; } } private void Form1_Load(object sender, EventArgs e) { LineNumberTextBox.Font = richTextBox1.Font; richTextBox1.Select(); AddLineNumbers(); } private void richTextBox1_SelectionChanged(object sender, EventArgs e) { Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart); if (pt.X == 1) { AddLineNumbers(); } } private void richTextBox1_VScroll(object sender, EventArgs e) { LineNumberTextBox.Text = ""; AddLineNumbers(); LineNumberTextBox.Invalidate(); } private void richTextBox1_TextChanged(object sender, EventArgs e) { if (richTextBox1.Text == "") { AddLineNumbers(); } } private void richTextBox1_FontChanged(object sender, EventArgs e) { LineNumberTextBox.Font = richTextBox1.Font; richTextBox1.Select(); AddLineNumbers(); } private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e) { richTextBox1.Select(); LineNumberTextBox.DeselectAll(); } private void Form1_Resize(object sender, EventArgs e) { AddLineNumbers(); } 

你可以通过绘制自己的控件来实现。 这是一个如何绘制自己链接的示例