Implement "Find Next"

I’m reading a text file into a TextArea control and have a TextField where I can enter search text. It works with this bit of code.

Var start As Integer = 0
Var len As Integer = txtFind.Text.Length

start = InStr(txtLogData.Text, txtFind.Text)

If start >= 0 Then
  txtLogData.SelectionStart = start-1
  txtLogData.SelectionLength = len
  txtLogData.StyledText.Bold(start, Len) = False
  lastFindStart = start
  lastFindLen = len
End If

This works and the first occurrence of the text (not case sensitive) is located and highlighted. What I would like to do is change the button caption to “Next” (no problem) and then do subsequent searches beyond the current highlighted text. Can I do this within the TextArea or do I need to copy the text beyond what’s been found into a string a look for it there?

You need to pass the last found position (or zero) to InStr as a start parameter.

start = InStr(lastFindStart, txtLogData.Text, txtFind.Text)

Thank you. Got it working.

Var start As Integer = -1
Var length As Integer = txtFind.Text.Length

If lastStart = -1 Then
  start = txtLogData.Text.IndexOf(txtFind.Text)
Else
  start = txtLogData.Text.IndexOf(lastStart+length, txtFind.Text)
End If

If start >= 0 Then
  txtLogData.SelectionStart = start
  txtLogData.SelectionLength = length
  txtLogData.StyledText.Bold(start, length) = False
  lastStart = start
  btnFind.Caption = "Next"
End If