displaying line number in rich text box c#

2019-01-24 06:09发布

问题:

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.

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:

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

J.T. jr



回答2:

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

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


回答3:

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.

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


回答4:

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