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
Printable View
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
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
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.
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
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:
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
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
If it contains a large amount of text, it might be slow and need some tweaking...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
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
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
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
Hey,
Will give it a go and get back to you in the morning.
Thanks!
Jessee
Hey,
Thats a bit to confusing for me.
Any other ways?
Thanks!
Jessee
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.
EDIT: im pretty sure it doesn't matter but add the bold text.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
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.
Hey,
There is no missing reference :S
Any other ideas?
Thanks!
Jessee
Erm.. did you look at the article I referred to? :confused:
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.
Hey,
I have attatched an image of my references.
I will look over the link you suggested again :)
Attachment 71971
Thanks!
Jessee
Hey,
Found the fix for my mid$ problem. Adding VBA infront of it worked haha.
Thanks!