counting lines in a textbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

sorry for a dumb question. I have a textbox where multiline=true and I want
to set the scrolbar from none to vertical when the textbox have more then
f.ex. 5 lines,
or reverse.

How can I count the amount of lines in a textbox?

Best regards
Hans
 
Hi Hans,
One approach would simply be to split the textbox text at the newline
character and then count how many strings you get. For example:

string [] lines = this.textBox1.Text.Split('\n');
MessageBox.Show(lines.Length.ToString());

If you care whether or not a line is empty, you'll actually need to
look at the content of lines[].
Hope that helps.
John
 
Hi Jon
Thank you for your help.

After som brainstorm this worked for me.

if (this.textbox1.Line.Length > 5)
this.textbox1.Scrolbar = Scrolbar.Vertical
else
this.textbox1.Scrobar = Scrolbar.None

Note: this does'nt work if Wordwrap=true and you aren't pressing enter for
new line.
Do you have nay idea for solution.

Best regards
Hans

Do
John Duval said:
Hi Hans,
One approach would simply be to split the textbox text at the newline
character and then count how many strings you get. For example:

string [] lines = this.textBox1.Text.Split('\n');
MessageBox.Show(lines.Length.ToString());

If you care whether or not a line is empty, you'll actually need to
look at the content of lines[].
Hope that helps.
John

Hi!

sorry for a dumb question. I have a textbox where multiline=true and I want
to set the scrolbar from none to vertical when the textbox have more then
f.ex. 5 lines,
or reverse.

How can I count the amount of lines in a textbox?

Best regards
Hans
 
One approach would simply be to split the textbox text at the newline
character and then count how many strings you get.

If you have set WordWrap to true, chances are you will have no newline
characters at all...
 
except that some lines _WRAP_ also right?






Hi Hans,
One approach would simply be to split the textbox text at the newline
character and then count how many strings you get. For example:

string [] lines = this.textBox1.Text.Split('\n');
MessageBox.Show(lines.Length.ToString());

If you care whether or not a line is empty, you'll actually need to
look at the content of lines[].
Hope that helps.
John

sorry for a dumb question. I have a textbox where multiline=true and I want
to set the scrolbar from none to vertical when the textbox have more then
f.ex. 5 lines,
or reverse.
How can I count the amount of lines in a textbox?
Best regards
Hans- Hide quoted text -

- Show quoted text -
 
Hi,

x
Hans - DiaGraphIT - said:
Hi Jon
Thank you for your help.

After som brainstorm this worked for me.

if (this.textbox1.Line.Length > 5)
this.textbox1.Scrolbar = Scrolbar.Vertical
else
this.textbox1.Scrobar = Scrolbar.None

Note: this does'nt work if Wordwrap=true and you aren't pressing enter
for
new line.
Do you have nay idea for solution.

You could always set Wordwrap= false :)

Othen than that I have no idea how to solve this. Why don't you post (or
search) in a javascript forum.
 
Hi!

Thank you all for your replies. I've found out that a RichTextBox is in my
case a better friend :-)


/Hans
 
Back
Top