|
-
Jul 12th, 2009, 02:30 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Search line in multi line textbox
Hey guys and gals,
I have a mullti line textbox with an auto scroll feature.
Im looking for it to check the current top line in the textbox for a piece of text, then do an event. No idea at all how to do this...any ideas?
Thanks!
Jessee
-
Jul 12th, 2009, 03:05 AM
#2
Re: Search line in multi line textbox
Here's an example:
vb Code:
Option Explicit
Private Sub CheckTextBox()
Dim lonPos As Long, strLine As String
'Find end of first line (vbCr, carriage return)
lonPos = InStr(1, Text1.Text, vbCr)
If lonPos > 0 Then
'Get text left to the left of it
strLine = Left$(Text1.Text, lonPos - 1)
Else
'No carriage return found, only one line in textbox
strLine = Text1.Text
End If
'Now find the text in this line...
'vbTextCompare means the search is case-insensitive
If InStr(1, strLine, "Text you're searching for...", vbTextCompare) > 0 Then
'Raise the event here...
'Call MyEvent()
End If
End Sub
-
Jul 12th, 2009, 04:08 AM
#3
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
It works only when the vert scroll bar is at the top of the textbox.
It doesnt search through whilst scrolling.
Thanks!
Jessee
EDIT: Oops I had it on a keypress event. Im going to have a play around and will get back with any problems.
Last edited by Letsgetcoding; Jul 12th, 2009 at 04:19 AM.
-
Jul 12th, 2009, 07:35 AM
#4
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
Its only checking the line at the start of the textbox.
When it scrolls it checks that line again, therefore always returning with my event.
How do I fix this?
Thanks!
Jessee
-
Jul 12th, 2009, 08:39 AM
#5
Re: Search line in multi line textbox
Oh, you mean the first line that's visible in the textbox? You'll need to use the SendMessage() API function with the EM_GETFIRSTVISIBLELINE constant... ie:
Code:
Option Explicit
Private Const EM_GETFIRSTVISIBLELINE = &HCE
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Command1_Click()
Dim lonLine As Long
lonLine = SendMessage(Text1.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
MsgBox lonLine + 1
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 200
Text1.SelStart = Len(Text1.Text)
Text1.SelText = CStr(i) & vbCrLf
Next i
End Sub
That shows the line number visible at the top of the textbox... a quick example of showing the line's text:
Code:
Option Explicit
Private Const EM_GETFIRSTVISIBLELINE = &HCE
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Command1_Click()
Dim lonLine As Long, strLines() As String
lonLine = SendMessage(Text1.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
strLines = Split(Text1.Text, vbCrLf)
MsgBox strLines(lonLine)
Erase strLines
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 200
Text1.SelStart = Len(Text1.Text)
Text1.SelText = CStr(i) & vbCrLf
Next i
End Sub
If it contains a large amount of text, it might be slow and need some tweaking...
-
Jul 12th, 2009, 09:01 AM
#6
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
Sweet that works.
But how do I search the line now, and do an event?
Atm, it shows a messagebox with the top line.
Thanks!
Jessee
-
Jul 12th, 2009, 09:18 AM
#7
Re: Search line in multi line textbox
Code:
Option Explicit
Private Const EM_GETFIRSTVISIBLELINE = &HCE
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Command1_Click()
Dim lonLine As Long, strLines() As String
lonLine = SendMessage(Text1.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
strLines = Split(Text1.Text, vbCrLf)
'Search line...
If InStr(1, strLines(lonLine), "Enter text to search for here...", vbTextCompare) > 0 Then
'Raise your event...
Call MyEvent
End If
Erase strLines
End Sub
Private Sub MyEvent()
'Put your event code here...
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 200
Text1.SelStart = Len(Text1.Text)
Text1.SelText = CStr(i) & vbCrLf
Next i
End Sub
-
Jul 12th, 2009, 09:45 AM
#8
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
Thats working great!
Now for the next challenging part....
This is the text im searching for: <brfor=
The full text of that is <brfor=5>
The number 5 is a variable and is used to set the interval on a timer.
How am I able to take that number out from that text?
Thanks for all your help to date DigiRev!
Jessee
-
Jul 12th, 2009, 10:36 AM
#9
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
Will give it a go and get back to you in the morning.
Thanks!
Jessee
-
Jul 12th, 2009, 10:33 PM
#10
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
Thats a bit to confusing for me.
Any other ways?
Thanks!
Jessee
-
Jul 12th, 2009, 11:24 PM
#11
Hyperactive Member
Re: Search line in multi line textbox
here is a simple example i just whipped up for the parsing part. its a bit long but it will work for multiple tags. i hope its not too complicated for you to understand.
Code:
Option Explicit
Private Sub Command1_Click()
'call the function here
Me.Caption = ParseTextInBetween(Text1.Text, "<brfor=", ">")
End Sub
Private Function ParseTextInBetween(ByVal MyString as String, ByVal pTagStart As String, ByVal pTagStop As String) As String
Dim BgnTagPos As Long, EndTagPos As Long
BgnTagPos = InStr(MyString, pTagStart) 'find the start of the tag we want.
If BgnTagPos Then
BgnTagPos = BgnTagPos + Len(pTagStart) 'adding here so i dont have to do it 3 times in rest of the code
EndTagPos = InStr(BgnTagPos, MyString, pTagStop) 'find '>'
If EndTagPos Then
ParseTextInBetween = Mid$(MyString, BgnTagPos, EndTagPos - BgnTagPos) 'grab the data in between BgnTagPos and EndTagPos
End If
End If
End Function
EDIT: im pretty sure it doesn't matter but add the bold text.
Last edited by Billy Conner; Jul 14th, 2009 at 05:35 PM.
Reason: modified code
-
Jul 13th, 2009, 06:37 AM
#12
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
 Originally Posted by Billy Conner
here is a simple example i just whipped up for the parsing part. its a bit long but it will work for multiple tags. i hope its not too complicated for you to understand.
Code:
Option Explicit
Private Sub Command1_Click()
'call the function here
Me.Caption = ParseTextInBetween(Text1.Text, "<brfor=", ">")
End Sub
Private Function ParseTextInBetween(ByVal MyString, ByVal pTagStart As String, ByVal pTagStop As String) As String
Dim BgnTagPos As Long, EndTagPos As Long
BgnTagPos = InStr(MyString, pTagStart) 'find the start of the tag we want.
If BgnTagPos Then
BgnTagPos = BgnTagPos + Len(pTagStart) 'adding here so i dont have to do it 3 times in rest of the code
EndTagPos = InStr(BgnTagPos, MyString, pTagStop) 'find '>'
If EndTagPos Then
ParseTextInBetween = Mid$(MyString, BgnTagPos, EndTagPos - BgnTagPos) 'grab the data in between BgnTagPos and EndTagPos
End If
End If
End Function
The part that is highlighted, I get an error:
Cannot find project or library.
Thanks!
Jessee
-
Jul 13th, 2009, 10:07 AM
#13
Re: Search line in multi line textbox
That error is a bit of an odd one, and is almost certainly not actually related to the highlighted line - but caused by a missing Reference instead.
For an explanation of how to solve it, see the "Fixing Common VB errors" link in my signature.
-
Jul 14th, 2009, 06:10 AM
#14
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
There is no missing reference :S
Any other ideas?
Thanks!
Jessee
-
Jul 14th, 2009, 07:07 AM
#15
Re: Search line in multi line textbox
Erm.. did you look at the article I referred to? 
In addition to explaining how to check/solve a Missing reference, that article gives you 3 other ideas (which is all of the worthwhile ones that have been suggested in the hundreds of threads with that error).
However, I strongly suggest that you re-check for a Missing reference, as that is by far the most common cause of the error, and is also the easiest to fix.
-
Jul 14th, 2009, 07:22 AM
#16
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
I have attatched an image of my references.
I will look over the link you suggested again 

Thanks!
Jessee
-
Jul 31st, 2009, 02:54 PM
#17
Thread Starter
Addicted Member
Re: Search line in multi line textbox
Hey,
Found the fix for my mid$ problem. Adding VBA infront of it worked haha.
Thanks!
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
|