Reputation: 136
I have a multiline text box and I want to get line number that specific word in it.
I tried this:
For Each line As String In TextBox1.Lines
If line = "50" Then
Label2.Text = 'Number Of line
End If
Next
But I don't know how to get line number that "50" in it and show it in label2. how can i do that?
Upvotes: 0
Views: 160
Reputation: 905
Or try this:
Dim lines = TextBox1.Lines
Label2.Text = Array.IndexOf(lines, "50").ToString()
That will show the (zero based) index of first line to contain "50". Or -1 if no matching lines found.
Upvotes: 0
Reputation: 905
Try using a counter:
Dim iLineCount As Integer = 0
For Each line As String In TextBox1.Lines
iLineCount += 1
If line = "50" Then
Label2.Text = iLineCount.ToString()
End If
Next
Upvotes: 1
Reputation: 460048
Use a For
-loop instead of a For Each
:
Dim lines = TextBox1.Lines
For i As Int32 = 0 To lines.Length - 1
If lines(i) = "50" Then Label2.Text = (i + 1).ToString()
Next
I'm storing the TextBox.Lines
String()
in a variable because there's some overhead if you use this property often.
Upvotes: 1