Search String in a TextArea or the use of IndexOf in standard API

When I out of home, I take my laptop. Recently, my Inter one had a Monitor crash (it does not works anymore).

So, when I have time, I check some code / write some code on a MacBook Pro M1. So I do yesterday.

In this case, I wanted to add a Search into a TextArea new feature. I took for an example in the “Introduction to Programming with Xojo.pdf” v4 (API2).

I found code that used IndexOf.

Back home, I tried to convert it to Standard API and failed. At last, I used InStr.

But I want to understand what happened / why I failed. Here’s the code and a screen shot at my demo function.

So, the line I failed to convert to Standard API is:
FoundAt = Source_String.IndexOf(Search_Loc, Search_String)

Code from the PB_Search PushButton:


Sub Action() Handles Action
  Var Found_Loc     As Integer
  Var Source_String As String
  Var Search_String As String
  Var FoundAt       As Integer
  
  // Get the Source String
  Source_String = TA.Text
  
  // Get the Search String
  Search_String = TF_Search.Text
  
  // Set the focus to the Search TextArea
  TA.SetFocus()
  
  // Search the String
  FoundAt = Source_String.IndexOf(Search_Loc, Search_String)
  
  // Report the location of the found text
  If FoundAt > 0 Then
    // Show the found occurence
    TA.VerticalScrollPosition = TA.LineNumber(FoundAt)
    
    // Highlight the found occurence
    TA.SelectionStart  = FoundAt
    TA.SelectionLength = Len(Search_String)
    
    // I found one
    L_Info.Text = Search_String + " found at: " + Str(FoundAt) + "."
    
    // Update the Search Position
    Search_Loc = FoundAt + 1
  Else
    // Not found
    L_Info.Text = Search_String + " was not found, sorry."
  End
End Sub

How Many returns the number of found “Search_String” (as you can read in the screen shot). I also use IndexOf there.

You define Found_Loc in this method, but use Search_Loc, which obviously is defined elsewhere if this code compiles. If you’re getting incorrect results, then that’s probably why.

Thank you Jay.

You define Found_Loc in this method, but use Search_Loc, which obviously is defined elsewhere
Yes, I moved the Static to a window Property whe I had the “How Many Times the Search String exists in the Source document.

I have to check, but the code (in the original code) problem was in the IndexOf line.

I expanded the above code a bit, but I saw a strange behavior in the count of fpund occurences (“Click„” and “How Many”):
in a check, I get more than 140 (I forgot the real unit #, so 141 or 143 or…) when pressing Return and 81 with a click in “How Many”. I do not searched yesterday (since I rewrote the whole: I forgot my hard disk at home / I was on the road).

Yes, Found_Loc is not used in two locations were it is defined. Funny (Was I tired ?)

At first look, Found_At seems a duplicate of Find_At.

I think Found_Loc was meant to get the Search String Position, when Search_Loc is … Start Next Search at…

Argh ! I found the error in the original code and it is related to what you wrote. I used a not declared variable as the Start Search Position value for IndexOf.

Now, why did I wanted to use IndexOf with 2015r1 (instead of InStr) ?