Results 1 to 17 of 17

Thread: [RESOLVED] Search line in multi line textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Resolved [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

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Search line in multi line textbox

    Here's an example:

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub CheckTextBox()
    4.    
    5.     Dim lonPos As Long, strLine As String
    6.    
    7.     'Find end of first line (vbCr, carriage return)
    8.     lonPos = InStr(1, Text1.Text, vbCr)
    9.    
    10.     If lonPos > 0 Then
    11.         'Get text left to the left of it
    12.         strLine = Left$(Text1.Text, lonPos - 1)
    13.     Else
    14.         'No carriage return found, only one line in textbox
    15.         strLine = Text1.Text
    16.     End If
    17.    
    18.     'Now find the text in this line...
    19.     'vbTextCompare means the search is case-insensitive
    20.     If InStr(1, strLine, "Text you're searching for...", vbTextCompare) > 0 Then
    21.         'Raise the event here...
    22.         'Call MyEvent()
    23.     End If
    24.    
    25. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    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

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    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

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: Search line in multi line textbox

    Hey,

    Will give it a go and get back to you in the morning.

    Thanks!
    Jessee

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: Search line in multi line textbox

    Hey,

    Thats a bit to confusing for me.
    Any other ways?

    Thanks!
    Jessee

  11. #11
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: Search line in multi line textbox

    Hey,

    Quote Originally Posted by Billy Conner View Post
    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

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: Search line in multi line textbox

    Hey,

    There is no missing reference :S

    Any other ideas?

    Thanks!
    Jessee

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    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
    Name:  Ref.jpg
Views: 523
Size:  34.4 KB

    Thanks!
    Jessee

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    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
  •  



Click Here to Expand Forum to Full Width