简体   繁体   中英

Reading lines in textbox individually

I am trying read each line in a textbox individually and check if it contains a certain string. This will be used in the textchanged event of the textbox to check for a certain string and it will execute it's corresponding code if found.

I cant get it working right though. Here is my code.

    Dim txt As FastColoredTextBox = TryCast(page.Controls(0), FastColoredTextBox)
        For Each line As Line In txt.Lines
            If CBool(InStr(line.ToString(), "<vb>")) Then
                txt.Language = Language.VB
            End If

The FastColoredTextBox.Lines is a List(Of String) so you could simply loop on the Lines in this way

Dim txt As FastColoredTextBox = TryCast(page.Controls(0), FastColoredTextBox) 
For Each line As String In txt.Lines 
   If line.IndexOf("<vb>", StringComparison.OrdinalIgnoreCase) > 0 Then 
      txt.Language = Language.VB 
      Exit For ' If this is all you have to do exit immediatly
   End If 
Next

EDIT: The Exit For allows to break the loop without searching on subsequent lines that are of no interest. Of course, if you have other if then the Exit For should be removed. Also note that in my answer you don't have to create an unnecessary array of all the strings already in the control. And as last note, why use the old style Instr (VB6) when now we have a rich bag of string tools

So I am guessing you're using the FastColoredTextBox from the CodeProject.com site, right?

Try splitting the Text of the text box into its corresponding lines first:

Dim txt As FastColoredTextBox = TryCast(page.Controls(0), FastColoredTextBox)
Dim Lines as string() = txt.Text.Split(VbCrLf)
For Each line As String In txt.Lines
  CBool(InStr(line, "<vb>")) Then
  txt.Language = Language.VB
End If

Why you're going in that long way, I did it now with the following short easy code:

For i = 0 To Val(TextBox1.Lines.Count)-1
    If TextBox1.Lines(i).ToString.StartsWith("OS Version:") Then
        Label9.Text = TextBox1.Lines(i).ToString
        Exit For
      Exit Sub
    End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM