For Each очень удобен когда на перёд не известно количество элементов, а While и Repeat не охота пристраивать,
пример. Указанный Find/FindNext/FindPrevious будут работать при табличном представлении строчки, например как в Excel при указание Range.
Сдесь же можно переделать, добавив на форму ещё RichTextBox1 (для хранения строки и отображения результата) и TextBox1(для указания чего искать), указанный код к такому виду.
Код:

Public Class Form1
Dim FindingString As String = "asdgfsdfgsdfgsdfgStringsdfgsdfgsdfgStringsdfgsdfgsdfgString"
Dim FindString As String = "String"
'Dim FirstTemp As String = ""
'Dim SecondTemp As String = ""
'Dim ValidateTemp As Integer = 0
Dim start As Integer = 0
Dim indexOfSearchText As Integer = 0
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RichTextBox1.Text = FindingString
TextBox1.Text = FindString
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'If ValidateTemp = 0 Then
'ValidateTemp = 1
'FirstTemp = FindingString
'SecondTemp = Mid(FirstTemp, InStr(FirstTemp, FindString))
'FirstTemp = SecondTemp
'MsgBox(FirstTemp)
'Else
'SecondTemp = Mid(FirstTemp, InStr(FirstTemp, FindString) + Len(FindString))
'FirstTemp = SecondTemp
'SecondTemp = Mid(FirstTemp, InStr(FirstTemp, FindString))
'FirstTemp = SecondTemp
'MsgBox(FirstTemp)
'End If
Dim startindex As Integer = 0
If TextBox1.Text.Length > 0 Then
startindex = FindMyText(TextBox1.Text.Trim(), start, RichTextBox1.Text.Length)
End If
' If string was found in the RichTextBox, highlight it
If startindex >= 0 Then
' Set the highlight color as red
RichTextBox1.SelectionColor = Color.Red
' Find the end index. End Index = number of characters in textbox
Dim endindex As Integer = TextBox1.Text.Length
' Highlight the search string
RichTextBox1.Select(startindex, endindex)
' mark the start position after the position of
' last search string
start = startindex + endindex
End If
End Sub
Public Function FindMyText(ByVal txtToSearch As String, ByVal searchStart As Integer, ByVal searchEnd As Integer) As Integer
' Unselect the previously searched string
If searchStart > 0 AndAlso searchEnd > 0 AndAlso indexOfSearchText >= 0 Then
TextBox1.Undo()
End If
' Set the return value to -1 by default.
Dim retVal As Integer = -1
' A valid starting index should be specified.
' if indexOfSearchText = -1, the end of search
If searchStart >= 0 AndAlso indexOfSearchText >= 0 Then
' A valid ending index
If searchEnd > searchStart OrElse searchEnd = -1 Then
' Find the position of search string in RichTextBox
indexOfSearchText = RichTextBox1.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None)
' Determine whether the text was found in richTextBox1.
If indexOfSearchText <> -1 Then
' Return the index to the specified search text.
retVal = indexOfSearchText
End If
End If
End If
Return retVal
End Function
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
start = 0
indexOfSearchText = 0
End Sub
End Class
После трёх нажатий в предыдущей версии коде выскакивает ошибка.
mrcnn,
Это всё выключается здесь Project -> Options-> Text Editor Basic -> VB Specific
