0

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

  • 1
    there is no way to do this out-of-the-box, you are better of just getting a component that is built to do this – TheGeneral Nov 17 '19 at 04:17
  • You would have to make a custom control, which would probably be easier in WPF. However, how to do that is too broad of a topic for StackOverflow, plus there are already custom controls available that do that; just use one of those. – Herohtar Nov 17 '19 at 04:42
  • 1
    Here is a quick one: Add a Label to the side of the RTB. Make it non-autosize, clear the text and add this to the Paint event: `private void st_lines_Paint(object sender, PaintEventArgs e) { RichTextBox rtb = richTextBox1; for( int l = 0; l < rtb.Lines.Length; l++) { int chInd = rtb.GetFirstCharIndexFromLine(l); Point lPt = rtb.GetPositionFromCharIndex(chInd); TextRenderer.DrawText(e.Graphics, "line " + l, Font, new Point( 2, lPt.Y + 3), Color.Black); } }`. In the RTB.TextChanges event write `st_lines.Invalidate();` - Optimize as needed. Done.. – TaW Nov 17 '19 at 12:46
  • You should also add an `st_lines.Invalidate();` to the `richTextBox1_VScroll` event! - This will work with freely formatted text. So there is no need for an outside control at all! - If you get it to work feel free to add the result as an answer! – TaW Nov 17 '19 at 14:01
  • Use [FastColoredTextBox](https://github.com/PavelTorgashov/FastColoredTextBox) or [ScintillaNET](https://github.com/jacobslusser/ScintillaNET) – Alexander Petrov Nov 18 '19 at 05:41
  • @AlexanderPetrov You're right. FastColoredTextBox is really helpful. –  Dec 15 '19 at 19:16
  • 1
    @TaW - Just wanted to say that I found your solution extremely useful. You provided a simple method to visualize line numbers of the corresponding RTB along with the functionality required to align scrolling. This should be an actual answer. If you have the time to do so, make it so. Even though the accepted answer has already been selected, yours is the one that actually provides a solution that doesn't require hacky 3rd party controls. – BJS3D Nov 13 '22 at 22:44
  • No, I won't write it up as an answer, but as I wrote in the comment: Feel free to write an answer yourself..! – TaW Nov 14 '22 at 07:08
  • @TaW - Understood. After implementing it as so in my Dreamweaver clone, I kind of realized why you may not want it as a full-on answer: beyond a several hundred lines of text in the adjacent box, it really starts to slow things down. Trying to load a .json file of over 16,000 lines pretty much hangs things up. Still, for average html, php, css and js markup, this solution is sufficient. :) – BJS3D Nov 19 '22 at 15:04
  • No, that is not the reason and imo presenting the user with 16k lines is not really reasonable either. I have stopped writing answers here altogether for 'political' reasons. - The performance issue may or may not be resolved by making the scrolling controls [double-buffered](https://stackoverflow.com/questions/41893708/how-to-prevent-datagridview-from-flickering-when-scrolling-horizontally/41894210#41894210).. – TaW Nov 19 '22 at 19:23

1 Answers1

2

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: Example of FastColoredTextBox