counting lines in a textbox

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
 
J

John Duval

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
 
G

Guest

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
 
M

Mark Rae

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...
 
P

pfc_sadr

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 -
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 
G

Guest

Hi!

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


/Hans
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top