|
-
Jul 2nd, 2002, 06:48 AM
#1
Thread Starter
Hyperactive Member
could someone help me with finding a word in this textbox, please?
Hi, I can't for the life of me figure out the code for this. I want to find the word 'hello'. If 'hello' appears more than once, I want it to find the next 'hello', and so on. I've looked at numerous snippits of code but all this seltext, selposition, sellength stuff has made me goggley eyed.
I've attached my code below, in the hope someone can help. When the first 'hello' is found, it should move on to find the next one.
Thanks for any help!
Private Sub Command1_Click()
Dim intStart As Integer
Dim lenSelected, posSelected As Long
intStart = InStr(1, Text1.Text, "hello")
Text1.SetFocus
Text1.SelStart = intStart - 1
Text1.SelLength = Len("hello")
If Len(Text1.SelText) <> 0 Then
Text1.SetFocus
posSelected = InStr(1, Text1, Text1.SelText)
lenSelected = Len(Text1.SelText)
Text1.SelStart = Len(Text1)
Text1.SelStart = posSelected - 1
Text1.SelLength = lenSelected
End If
End Sub
-
Jul 2nd, 2002, 07:14 AM
#2
Moon pie and chips...£67.99!!!
U can only highlight one "Hello" at a time...do you want a message box to say "Find Next?" and then it finds the next hello in the text, and so on....???
-
Jul 2nd, 2002, 07:31 AM
#3
Hyperactive Member
Hi,
Modified your code a bit...
VB Code:
Dim i As Long
Dim sSearch As String
sSearch = "hello"
Text1.SelStart = i
Text1.SelLength = 0
For i = 1 To Len(Text1.Text)
If Mid(Text1.Text, i, Len(sSearch)) = sSearch Then
Text1.SelStart = i - 1
Text1.SelLength = Len(sSearch)
Text1.SelBold = True
Text1.SelColor = vbRed
End If
Next
You ll see that when you click th button, every 'hello' word is highlighted.
If you want to make it a bit more flexible, place another textbox on your form, and place the value typed in in variabele sSearch.
You can also place a button to make all text backt to normal after a search.
Greetz, Luc
-
Jul 2nd, 2002, 07:32 AM
#4
Addicted Member
I think it could be quite easy. You just have to set the next starting point to the end of the hello. Which is in fact 5 position further. This way the text you look in shortens.
eg.
this is hello 1 and then hello 2.
The first hello is at position 9 len =5. Your next start should thus be 14. the next hello is at 26 len=5. The thing is that the user will possibly never see the text being selected as a pc handles this too fast for the eye to see.
-
Jul 2nd, 2002, 07:42 AM
#5
Hyperactive Member
Like Killah said, you could search step by step too:
VB Code:
Option Explicit
Dim i As Long
Private Sub Command1_Click()
Dim sSearch As String
sSearch = "hello"
Text1.SelStart = i
For i = i To Len(Text1.Text)
If Mid(Text1.Text, i, Len(sSearch)) = sSearch Then
Text1.SelStart = i - 1
Text1.SelLength = Len(sSearch)
Text1.SelBold = True
Text1.SelColor = vbRed
i = i + 1
Exit For
End If
Next
End Sub
Private Sub Form_Load()
i = 1
End Sub
-
Jul 2nd, 2002, 08:00 AM
#6
Note:
.SelBold and .SelColor are not properties of a standard textbox (like Text1), but properties of a RichTextBox. Consequently, the word bolding and highlighting will only work if using the latter control.
-
Jul 2nd, 2002, 08:08 AM
#7
Hyperactive Member
Iguessed ianpaisley was using the richtextbox allready, since he got some code allready....
So ianpaisley, if you are trying to solve this with a standard textbox, remove him from your form, and place a richtextbox instead. (Project / Components / Microsoft rich textbox control)
Greetz, Luc
-
Jul 2nd, 2002, 10:02 AM
#8
Thread Starter
Hyperactive Member
LucGuldentops & Co, thanks for your help. I was hoping I could use my code because I spent a lot of effort putting the damn thing together, and want to know how it works.
Wokawidget, I don't want a 'Find Next' box - it will automatically find and highlight the next 'hello' when the command button is clicked.
I thought replacing intStart with:
intStart = InStr(intStart + Len("hello"), Text1.Text, "hello")
would work, but it doesn't, unfortunately. Do I need a For....Next statement or something?
Grateful if you could help, thanks again
-
Jul 3rd, 2002, 01:28 AM
#9
Hyperactive Member
ianpaisley,
i believe this code (from above)will do the thing, not?
VB Code:
Dim i As Long
Dim sSearch As String
sSearch = "hello"
Text1.SelStart = i
Text1.SelLength = 0
For i = 1 To Len(Text1.Text)
If Mid(Text1.Text, i, Len(sSearch)) = sSearch Then
Text1.SelStart = i - 1
Text1.SelLength = Len(sSearch)
Text1.SelBold = True
Text1.SelColor = vbRed
End If
Next
Greetz, Luc
-
Jul 3rd, 2002, 01:31 AM
#10
Hyperactive Member
ianpaisley,
Never use code because you spent a long time working on it, if it isn't good, and you got something else, throw it away (or comment it out)
Luc,
-
Jul 3rd, 2002, 02:04 AM
#11
Member
It seems to me that he wants to use his own code but only slightly modified to work. I could be wrong, but just in case, I modified his code to work how I think he wants it?
Code:
'Added this to hold the new position to search from
Dim newPos As Long
Private Sub Command1_Click()
Dim intStart As Integer
Dim posSelected, lenSelected As Long
'Here you search from the new position
intStart = InStr(newPos, Text1.Text, "hello")
'If it couldn't find "hello" again, start from the beginning
If intStart = 0 Then
newPos = 1
intStart = InStr(newPos, Text1.Text, "hello")
End If
Text1.SetFocus
Text1.SelStart = intStart - 1
Text1.SelLength = Len("hello")
If Len(Text1.SelText) <> 0 Then
Text1.SetFocus
posSelected = InStr(newPos, Text1, Text1.SelText)
lenSelected = Len(Text1.SelText)
Text1.SelStart = Len(Text1)
Text1.SelStart = posSelected - 1
Text1.SelLength = lenSelected
'Set the new position to just after the last found word
newPos = Text1.SelStart + Len("hello")
End If
End Sub
Private Sub Form_Load()
'Make sure the start position is 1 at first
newPos = 1
End Sub
Now I know I could've made this code much more efficient and cleaner, but it seems to me he just wants to understand how the code works and what better way to learn than from his own code? If he didn't want his own code modified - oops 
-
Jul 3rd, 2002, 04:24 AM
#12
Thread Starter
Hyperactive Member
JuiCe, your code did EXACTLY what I wanted to acheive - positioning 'hello' on first line and all. Thanks again to everyone.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|