简体   繁体   中英

How do I create a “line number strip” to a RichTextBox in C#?

Imagine I have a simple RichTextBox with 3 lines.

Each line says "line" and then the line number.

What I am really asking is to make a strip on the RichTextBox's left and each line I put in, a new label which shows the new line number pops up until it goes to the bottom.

Here is close to exactly what I am trying to achieve: https://i.ibb.co/3dQM2Pc/Ey-p.png . It is for a Roblox exploit I am making.

AND NO USER CONTROL

I figured out my problem by using FastColoredTextBox, as someone suggested to me.

Use FastColoredTextBox or ScintillaNET – Alexander Petrov

Here's what it turned out to be like: FastColoredTextBox 示例

To create such a RichTextBox, you can achieve it via custom User Control. Here are the steps you can refer to. First, you need to add a panel(to show the line number) and a RichTextBox into the User Control.

Then you can try the following code in "UserControl.cs".

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    showLineNo();
}

private void richTextBox1_VScroll(object sender, EventArgs e)
{
    showLineNo();
}

private void showLineNo()
{
    // get current location
    Point p = this.richTextBox1.Location;
    int crntFirstIndex = this.richTextBox1.GetCharIndexFromPosition(p);
    int crntFirstLine = this.richTextBox1.GetLineFromCharIndex(crntFirstIndex);
    Point crntFirstPos = this.richTextBox1.GetPositionFromCharIndex(crntFirstIndex);
    p.Y += this.richTextBox1.Height;
    int crntLastIndex = this.richTextBox1.GetCharIndexFromPosition(p);
    int crntLastLine = this.richTextBox1.GetLineFromCharIndex(crntLastIndex);
    Point crntLastPos = this.richTextBox1.GetPositionFromCharIndex(crntLastIndex);

    // Set brush
    Graphics g = this.panel1.CreateGraphics();
    Font font = new Font(this.richTextBox1.Font, this.richTextBox1.Font.Style);
    SolidBrush brush = new SolidBrush(Color.Green);
    Rectangle rect = this.panel1.ClientRectangle;
    brush.Color = this.panel1.BackColor;
    g.FillRectangle(brush, 0, 0, this.panel1.ClientRectangle.Width, this.panel1.ClientRectangle.Height);
    brush.Color = Color.Black;

    // Draw line number
    int lineSpace = 0;
    if (crntFirstLine != crntLastLine)
    {
        lineSpace = (crntLastPos.Y - crntFirstPos.Y) / (crntLastLine - crntFirstLine);
    }
    else
    {
        lineSpace = Convert.ToInt32(this.richTextBox1.Font.Size);
    }

    int brushX = this.panel1.ClientRectangle.Width - Convert.ToInt32(font.Size * 3);
    int brushY = crntLastPos.Y + Convert.ToInt32(font.Size * 0.21f);
    for (int i = crntLastLine; i >= crntFirstLine; i--)
    {
        g.DrawString((i + 1).ToString(), font, brush, brushX, brushY);
        brushY -= lineSpace;
    }
    g.Dispose();
    font.Dispose();
    brush.Dispose();
}

The test result,

在此处输入图像描述

Hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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