代码之家  ›  专栏  ›  技术社区  ›  fonix232

如何在RichTextBox C中显示行号#

  •  8
  • fonix232  · 技术社区  · 16 年前

    6 回复  |  直到 16 年前
        1
  •  21
  •   Bhargav Rao rlgjr    7 年前

    我尝试重新使用其他地方引用的codeproject文章中的代码,但是我看到的每一个选项似乎都有点太模糊了。

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

    enter image description here

    enter image description here

    enter image description here

    它有局限性:它只在左边显示数字。如果你愿意的话,你可以不费吹灰之力改变这一切。

    See the code

        2
  •  1
  •   JYelton Melchior Blausand    16 年前

    我会将每一行存储在一个类中,该类有方法发布到richtextbox。在该方法中,可以根据行号在类中的位置预先设置行号。

    例如(非常粗略):

    class myText
    {
        public List<string> 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();
        }
    }
    
        3
  •  1
  •   Puterdo Borato Johannes Passing    15 年前

    闪烁.Net http://scintillanet.codeplex.com/ 但对于我的项目,我使用了Cheeso建议的解决方案(来自XPath可视化工具的RichTextBoxEx)。对于不太大的文档来说,它足够简单和快速。 互联网上所有其他的.net组件都非常慢。

        4
  •  1
  •   Konrad Viltersten    13 年前

    简单的方法是:

    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
    
        5
  •  1
  •   Pritam Zope    10 年前
        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();
        }
    
        6
  •  0
  •   RvdK    16 年前

    下面是一个如何画自己的例子 link