简体   繁体   English

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

[英]displaying line number in rich text box c#

I have a Multiline richtextbox control into which i want to integrate the feature of adding a line number.我有一个多行 Richtextbox 控件,我想将添加行号的功能集成到该控件中。 i have considered many approaches我考虑了很多方法

  1. Add a label and updating the line numbers as the line count changes添加 label 并随着行数的变化更新行号
  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.添加列表框并在其中显示行号。

I got two doubts.我有两个疑问。

  1. The richtextbox which i'm using is a custom made control and derieves from RichTextBox class.我正在使用的 Richtextbox 是一个定制的控件,它来自 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#在 c# 中显示多行文本的行号的最佳方法是什么

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 是我的富文本框,而 tB 是 rTB 旁边的文本框

JT jr小JT

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

WORKS 100%,,.工作 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.但是您需要为行号添加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();
            }
        }    
    } 
    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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM