Поиск слова в тексте без макроса - VB
Формулировка задачи:
Помогите, плз!!!
Kak nayti lyuboy slov iz teksta v VB 6? Naprimer Input: 'anyword' > sear > text2.text???
Cherez makros ya delal no drugogo metoda est'? Pojaluysta podskajite
Ochen' blagodaryu za pomosh
Joe
Решение задачи: «Поиск слова в тексте без макроса»
textual
Листинг программы
'---- для поиска повторяющихся (double) слов в тексте Text1.text... (var - find forward)
'---- объявим (Public) в модуле Form:
Public pos As Long
'==============================
'===== Answer 1: ============
Sub cmdFind_Click()
Dim i As Long
'Text1.Text - wordText
'search=: Text2.Text
'metod: i = InStr(pos, Text1.Text, Text2.Text)
i = InStr(1, Text1.Text, Text2.Text)
If i Then ' or: If i > 0 Then ' or: If i <> 0 Then
Text1.SelStart = i - 1
' позиция найденного текста в масиве Text1
Text1.SelLength = Len(Text2.Text) ' выделим найденное слово
Text1.SetFocus
' cursor in text1(=перенесем курсор в текст)
Else
MsgBox 'Found nothing... :('
End If
End Sub
'========= Answer 2: ============
Sub cmdFind_Click()
If InStr(1, Text1.Text, Text2.Text) <> 0 Then
Text1.SelStart = InStr(1, Text1.Text, Text2.Text) - 1
Text1.SelLength = Len(Text2.Text)
Text1.SetFocus
Else
MsgBox 'Found nothing... :('
End If
End Sub
'------------------------------------------------
'======== Answer : find forward ============
'---- для поиска повторяющихся (double) слов в тексте Text1.text... (var - find forward)
'---- объявим (Public) в модуле Form:
Public pos As Long
Sub cmdFind_Click()
Dim i As Long
If pos = 0 Then pos = 1 ' else Error if pos = 0
i = InStr(pos, Text1.Text, Text2.Text)
If i Then
Text1.SelStart = i - 1
Text1.SelLength = Len(Text2.Text)
pos = i - 1 + Len(Text2.Text) ' запомнить N-й раз найденное слово
Text1.SetFocus
Else
MsgBox "Found nothing... :("
pos = 1
End If
End Sub