question

Bri-6611 avatar image
0 Votes"
Bri-6611 asked Bri-6611 answered

Counting lines in a textbox

When counting lines in a textbox, the count is off.
If I click below the text the curser appears two rows beneath the text.
the count is off by 2.
the count seems to be counting to where the curser appears (if I click below the text).

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

     Dim ln As Integer = TextBox20.Text.Length
     TextBox20.SelectionStart = ln 'This should place the curser at -
     'the end of the text.
     'TextBox20.ScrollToCaret()
     Dim lines() As String = TextBox20.Lines
     TextBox77.Text = TextBox20.Lines.Length

 End Sub
dotnet-visual-basic
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Does it work differently if you specify False for WordWrap property of TextBox20?

0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

If I click below the text the curser appears two
rows beneath the text.
the count is off by 2.
.the count seems to be counting to where the curser
appears (if I click below the text).

You could only click beneath the text if there are blank lines
in the textbox. The line count includes any blank lines you
may have at the end of the string array.

  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bri-6611 avatar image
0 Votes"
Bri-6611 answered

Same result, it seem to be counting to where the curser would appear as opposed to any text. I suppose I could add code to see if there is any text on the bottom two lines and deduct if there is none. But I have no idea why the curser is appearing two rows beneath the text.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bri-6611 avatar image
0 Votes"
Bri-6611 answered

Wow is there an (is blank) like an (is numeric) way of identifying blank lines.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

is there an (is blank) like an (is numeric) way of
identifying blank lines.

You could simply iterate over the lines array and
check each line for "".

 Dim num As Integer = TextBox20.Lines.Length
 For Each s As String In TextBox20.Lines
     If s = "" Then num = num - 1
 Next
 TextBox77.Text = num.ToString

This code will subtract one from the line count for
every blank line, not just trailing lines. To
subtract only trailing blank lines from the count
you will need to amend the logic accordingly.

  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bri-6611 avatar image
0 Votes"
Bri-6611 answered

Thanks much

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.