8 Replies - 8262 Views - Last Post: 19 January 2012 - 02:06 PM Rate Topic: -----

#1 blogwhodle  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

How can you add Line Numbers to a RichTextBox or TextBox?

Posted 18 January 2012 - 06:05 PM

If you have any code or anything like that in visual studio, or even on here,
[...]
[...]
\\Stuff
\\\Stuff


it has line numbers. But in textboxes, the line numbers are tied to the original textbox and even if you scroll through the textbox, the line numbers move with it.
Any idea how do this to a textbox???
Thanks in advance.
:smile2:

Is This A Good Question/Topic? 0
  • +

Replies To: How can you add Line Numbers to a RichTextBox or TextBox?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,776
  • Joined: 12-June 08

Re: How can you add Line Numbers to a RichTextBox or TextBox?

Posted 18 January 2012 - 06:59 PM

You would manually prepend them as you add things to the textbox.

You can run far with this:

            TextBox foo = new TextBox(); //pretend this is a textbox on your form
            foo.Multiline = true;// make sure you can have multilines

            Int32 lLineNumber = 0;
            
            lLineNumber += 1;
            // append a specificially formatted line number, text, and new line to the textbox
            foo.Text += string.Format("{0}: {1}{2}", lLineNumber, "here", Environment.NewLine); // 0: here <newline>
            lLineNumber += 1;// increment the line number  for the next line.
            foo.Text += string.Format("{0}: {1}{2}", lLineNumber, "there", Environment.NewLine); // 1: there <newline>
            lLineNumber += 1;
            foo.Text += string.Format("{0}: {1}{2}", lLineNumber, "bar", Environment.NewLine); // 2: bar <newline>
            
            // text box should look like this:
            // 1: here <newline>
            // 2: there <newline>
            // 3: bar <newline>


Was This Post Helpful? 1
  • +
  • -

#3 blogwhodle  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

Re: How can you add Line Numbers to a RichTextBox or TextBox?

Posted 18 January 2012 - 07:22 PM

View Postmodi123_1, on 18 January 2012 - 06:59 PM, said:

You would manually prepend them as you add things to the textbox.

You can run far with this:

            TextBox foo = new TextBox(); //pretend this is a textbox on your form
            foo.Multiline = true;// make sure you can have multilines

            Int32 lLineNumber = 0;
            
            lLineNumber += 1;
            // append a specificially formatted line number, text, and new line to the textbox
            foo.Text += string.Format("{0}: {1}{2}", lLineNumber, "here", Environment.NewLine); // 0: here <newline>
            lLineNumber += 1;// increment the line number  for the next line.
            foo.Text += string.Format("{0}: {1}{2}", lLineNumber, "there", Environment.NewLine); // 1: there <newline>
            lLineNumber += 1;
            foo.Text += string.Format("{0}: {1}{2}", lLineNumber, "bar", Environment.NewLine); // 2: bar <newline>
            
            // text box should look like this:
            // 1: here <newline>
            // 2: there <newline>
            // 3: bar <newline>


But that would be the numbers physically in the text box.
If you edit it, you can delete the line numbers and stuff. I am trying to make a code editor that has line numbers similar to visual studio's ones, which you can't delete or anything.
Thanks for your help, however. :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,776
  • Joined: 12-June 08

Re: How can you add Line Numbers to a RichTextBox or TextBox?

Posted 18 January 2012 - 08:05 PM

Oh ho ho... the requirements change. Well that would mean a custom control. Yes-sir-ree.. How comfortable are you with tearing apart a control? Reshaping the bits and coming out a head? That is going to be some work.
Was This Post Helpful? 1
  • +
  • -

#5 blogwhodle  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

Re: How can you add Line Numbers to a RichTextBox or TextBox?

Posted 18 January 2012 - 08:31 PM

View Postmodi123_1, on 18 January 2012 - 08:05 PM, said:

Oh ho ho... the requirements change. Well that would mean a custom control. Yes-sir-ree.. How comfortable are you with tearing apart a control? Reshaping the bits and coming out a head? That is going to be some work.

I can make custom controls but I am no professional at that kind of stuff.
What I think I could do is add 2 text boxes, one skinny readonly one LineNumbers Readonly, and another richtextbox Text. Then add a timer set to a around 100 milliseconds UpdateTimer. A Scroll Bar ScrlBar. Every time UpdateTimer ticks, the number of lines in the textbox is counted and placed in the first one. (Text.Lines.Length) All I would need to do then is to connect both textboxes to the scrollbar, add methods to change the textboxes, and add an event to Text.TextChanged so that it does the syntax highlighting. All of that I know how to do except bind both textboxes to one scrollbar, since I never have used a scroll bar in my lifetime since the controls that needed them had built in ones.
I will need to look up the ScrollBar Class and the RichTextBox and TextBox class on MSDN to find out how to do that.
This way I can create a control with a bunch of child controls instead of disassembling richtextbox which I do not feel like doing because that is out of my ability. But I first need to find out the scrollbar stuff.
Thanks for the Idea!
:)
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: How can you add Line Numbers to a RichTextBox or TextBox?

Posted 18 January 2012 - 08:56 PM

Why do you need the timer? Think about when the line count changes.
Was This Post Helpful? 1
  • +
  • -

#7 NiceAlexanderAS  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 7
  • Joined: 16-January 12

Re: How can you add Line Numbers to a RichTextBox or TextBox?

Posted 18 January 2012 - 09:48 PM

What you can do is you can add a textbox and a listbox... and look for the events list of the textbox and add the TextChange... and now inside you have to write code that for each line on the textbox is going to add a new item on the listbox with the line number.... You need to clear the listbox for each change of lines so you don't get something like (1 2 1 2 3 1 2 3 4)

string Code = TextBox1.Text;
ListBox1.Clear();
int Line = 1;
for(int i = 0; i < Code.Lenght; i++)
{
// Read all the text and see when the new line is going to happen
if(Code[i] == EndOfLine)
{
ListBox1.Items.Add(Line);
Line++;
}
}



Was This Post Helpful? 1
  • +
  • -

#8 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: How can you add Line Numbers to a RichTextBox or TextBox?

Posted 19 January 2012 - 06:08 AM

If you just googled you would've found many great articles:

Line Numbers for RichText Control in C#
LineNumbers for the RichTextBox (Not C# but easily translatable)
Numbering lines of RichTextBox in .NET 2.0
displaying line number in rich text box c#

The google search phrase I used was: "richtextbox line numbers c#"
Was This Post Helpful? 1
  • +
  • -

#9 blogwhodle  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

Re: How can you add Line Numbers to a RichTextBox or TextBox?

Posted 19 January 2012 - 02:06 PM

View PostAdamSpeight2008, on 18 January 2012 - 08:56 PM, said:

Why do you need the timer? Think about when the line count changes.



View PostNiceAlexanderAS, on 18 January 2012 - 09:48 PM, said:

What you can do is you can add a textbox and a listbox... and look for the events list of the textbox and add the TextChange... and now inside you have to write code that for each line on the textbox is going to add a new item on the listbox with the line number.... You need to clear the listbox for each change of lines so you don't get something like (1 2 1 2 3 1 2 3 4)

string Code = TextBox1.Text;
ListBox1.Clear();
int Line = 1;
for(int i = 0; i < Code.Lenght; i++)
{
// Read all the text and see when the new line is going to happen
if(Code[i] == EndOfLine)
{
ListBox1.Items.Add(Line);
Line++;
}
}





View PostRexGrammer, on 19 January 2012 - 06:08 AM, said:

If you just googled you would've found many great articles:

Line Numbers for RichText Control in C#
LineNumbers for the RichTextBox (Not C# but easily translatable)
Numbering lines of RichTextBox in .NET 2.0
displaying line number in rich text box c#

The google search phrase I used was: "richtextbox line numbers c#"

Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Now I figured it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1