在富文本框中显示行号c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/657635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:08:27  来源:igfitidea点击:

displaying line number in rich text box c#

c#textboxmultilineline-numbers

提问by Anirudh Goel

I have a Multiline richtextbox control into which i want to integrate the feature of adding a line number. i have considered many approaches

我有一个多行富文本框控件,我想在其中集成添加行号的功能。我考虑了很多方法

  1. Add a label and updating the line numbers as the line count changes
  2. Add a picturebox along with to draw string on it.
  3. Add another textbox along with and show line numbers on it
  4. Add listbox along and display line numbers in it.
  1. 添加标签并随着行数的变化更新行号
  2. 添加一个图片框并在其上绘制字符串。
  3. 添加另一个文本框并在其上显示行号
  4. 添加列表框并在其中显示行号。

I got two doubts.

我有两个疑问。

  1. The richtextbox which i'm using is a custom made control and derieves from RichTextBox class. How can i add multiple controls to it.
  2. What is the best approach to show line numbers for the multiline text in c#
  1. 我使用的 Richtextbox 是一个定制的控件,并且源自 RichTextBox 类。我如何向它添加多个控件。
  2. 在 c# 中显示多行文本的行号的最佳方法是什么

回答by J.T.

My own example. All is fine, but wordwrap must be disabled :(

我自己的例子。一切都很好,但必须禁用自动换行:(

    int maxLC = 1; //maxLineCount - should be public
    private void rTB_KeyUp(object sender, KeyEventArgs e)
    {
        int linecount = rTB.GetLineFromCharIndex( rTB.TextLength ) + 1;
        if (linecount != maxLC)
        {
            tB_line.Clear();
            for (int i = 1; i < linecount+1; i++)
            {
                tB_line.AppendText(Convert.ToString(i) + "\n");
            }
            maxLC = linecount;
        }
    }

where rTB is my richtextbox and tB is textBox next to rTB

其中 rTB 是我的 Richtextbox,tB 是 rTB 旁边的 textBox

J.T. jr

JT Jr

回答by namenotrecognized

this code helped me thank you, needed to convert visual basic but could:

这段代码帮助我谢谢你,需要转换visual basic但可以:

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    Dim maxlc As Integer = 1
    Dim linecount As Integer = TextBox1.GetLineFromCharIndex(TextBox1.Height) + 1

    If linecount <> maxlc Then
        TextBox2.Clear()
        For i = 0 To linecount - 1 Step 1
            TextBox2.AppendText(Convert.ToString(i) + vbNewLine)
        Next i
        maxlc = linecount
    End If
End Sub

回答by Gocha

WORKS 100%!!! But you need to add richTextBox2 for line numbers, if you want change it to other form like listbox, anyway it served me well.

100% 有效!!!但是您需要为行号添加richTextBox2,如果您想将其更改为列表框等其他形式,无论如何它对我来说都很好。

    private void richTextBox1_keyDown(object sender, KeyEventArgs e)
    {

        for (int i = 0; i <= richTextBox1.Lines.Count(); i++)
        {
            if (!(e.KeyCode == Keys.Back))
            {
                if (!richTextBox2.Text.Contains(i.ToString()))
                {
                    richTextBox2.Text += i.ToString() + "\n";
                }
            }
            else
            {
                richTextBox2.Clear();
            }
        }    
    } 

回答by Pritam Zope

    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();
    }