Luka Pop
Luka Pop

Reputation: 55

Textbox line counter (more complex) VB.NET

im deep into (for fun and learning) programming and i have problem, i know how to count lines in textbox with this simple code

Label1.Text = Textbox1.Lines.Count

But this code doesnt count lines when i paste some multiline text it count it as one line. And yes, one more question is there way to make line counter live, i tried

    Private Sub Textbox1_TextChanged(sender As Object, e As EventArgs) Handles Page_Names.TextChanged
Label1.Text = Textbox1.Lines.Count
End Sub

but this reproduce much lag.

Example of "Multiline" text that i want to paste

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Sorry for stupid question im new, and sorry for bad English :)

Thanks in advance!

Upvotes: 0

Views: 750

Answers (2)

BanForFun
BanForFun

Reputation: 752

You handle a different TextBox's TextChanged Event that the one you want to count the lines of.

Try this:

Private Sub Textbox1_TextChanged(sender As Object, e As EventArgs) Handles Textbox1.TextChanged
Label1.Text = Textbox1.Lines.Count
End Sub

Upvotes: 0

Metzner88
Metzner88

Reputation: 3

Due to lower reputation I was unabled to answer it in the add comment section, thus posted it here. The solution for your problem is to play with the textbox properties. Change the AcceptReturn Property to true of the textbox properties.

Multiline=true AcceptReturn=true then you can do anything to count the lines of the textbox. just like adding a button, a label and then adding codes like to button clicking event.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = TextBox1.Lines.Count
End Sub

Upvotes: 0

Related Questions