Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Help with RichEdit Control

  1. #1
    Senior Member catventure's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    630
    Blog Entries
    1
    Rep Power
    79

    Help with RichEdit Control

    Hi,


    I have a RichEdit control that contains hundreds of lines of text.

    I have long wanted to be able to search the text from the start for a particular string and then go to or have the line that contains it scrolled/positioned to appear at the top of the RichEdit control and move the text caret there for possible editing of the text.

    Struggling to do it. And I would like to finally get it to work.

    I made a [Find] button control and a Textbox control to enter the text to be searched for below the Richedit control itself.

    (Ideally, repeatedly pressing the [Find] button would cycle through all occurrence of the searchtext in the RichEdit and move the caret there for possible amending until the end of the text/lines and then start over again from the first line until the user stops pressing or a new search text is entered.]

    Obviously, I can easily copy the text into Notepad or text editor and do it that way - but thought it would be good to do it from inside my program if I could find a simple way.

    Code snippets below...


    
    Local sLines() as string
    Local myText as string
    Local searchstr as string
    Local numlines, i, respscroll as long
    
    'get text to search for - works OK
    Control Get Text hDlg 622 To searchstr
    
    
    'get all text of richedit - works OK
    Control Get Text hDlg, 620 To myText
    
    
    'get number of lines in richedit - works OK, I think
     numlines = Parse(myText, sLines, $CRLF)
    
    
    'find a Line Number that Contains the  'string' to be searched for - seems OK?
    For i=1 To numlines
    If InStr( sLines(i), searchstr )<>0 Then
    respscroll=i
    Exit For
    End If
    Next i
    
    
    ' if found - scroll to found line and show at top of richedit - doesn't work
    If respscroll>0 Then
    MsgBox hdlg, "Search Result...", %MB_ICONASTERISK,"Info"
    Control Send hdlg,620,%EM_LINESCROLL,0,respscroll
    exit function
    end if
    
    
    'If text not found show inform user with messagebox - works OK
    MsgBox hdlg, "Not Found", %MB_ICONWARNING, "Information"
    

    This doesn't work properly although it does scroll a bit - but not to the top line of the richedit like I wanted and not sure how to move the caret there either.


    Anyone got any ideas or can help solve this please?


    catventure/Phil.

    Click image for larger version. 

Name:	response.jpg 
Views:	59 
Size:	104.6 KB 
ID:	10239
    http://tab.thinbasic.com - Home Of The ThinBasic Adventure Builder Project. (Interactive Fiction/Text Adventure Maker)

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,089
    Blog Entries
    5
    Rep Power
    725
    Hi Phil,

    I would say the key to move the cursor is to perform a selection (even if from the same to the same char).

    This code snippet will set cursor on position 1000 in the rich edit:
                dword hRich
                control handle cbhndl, %ID_EDITOR to hRich
                SendMessage(hRich, %EM_SETSEL, 1000, 1000) ' 1000 for start, 1000 for end of selection - moves cursor there, selects nothing
    
    Maybe it can help you


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Senior Member catventure's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    630
    Blog Entries
    1
    Rep Power
    79
    Hi Petr,

    Thanks for your suggestion - can't just see how it can help immediately.. although I 'm sure it could do.

    I show another code snippet here.
    This I think would probably work but I'm having trouble getting the lines of text using %em_get line.
    If I could get the line loaded into a string I can check if the search string is within the line and then if it was scroll that line to the top of the richedit. Doesn't matter about the caret being positioned anywhere...

    Function findResp() As Long
    
    
    Local numlines As Long        ' for number of lines in richedit control
    Local i As Long               ' to store number in for/next loop      
    Local lineindex As Long       ' index # of the first visible line
    Local charindex As Long       ' index of the first character on that line
    Local linetextlen As Integer  ' length of that line (note the data type!)
    Local tempstr, tempstr2 as string 'to hold search string and linetext
    Local linetext As String      ' receives the line's text (HOW to do???)
     
    'first get search string from Find editbox
    Control Get Text hdlg, 627 To tempstr
    
    
    'If empty findstring then exit
    If tempstr="" Then Exit Function 
    
    
    'get total # of lines
    Control Send hdlg, 620, %EM_GETLINECOUNT,0,0 To numlines
     
    'get # of first visible line of control
    Control Send hdlg, 620, %EM_GETFIRSTVISIBLELINE, 0, 0 To lineindex  
    
    
    'Check all lines from first visible to end of richedit text 
    For i=lineindex To numlines
    ' get line number and length of line text 
    Control Send hdlg, 620, %EM_LINEINDEX,lineindex, 0  To charindex 
    Control Send hdlg, 620, %EM_LINELENGTH, charindex,0 To linetextlen
    linetext = (IIf(linetextlen >= 2, linetextlen, 2))
    
    
    '*************
    'How to copy the actual TEXT of the line into tempstr2?
    'Following line does not work. How do I  make it work?
    Control Send hdlg, 620, %EM_GETLINE,i, linetext To tempstr2 
    '*************
    
    
    'When line text is in tempstr2 Check to see if search string is in it.
    If InStr(tempstr2,tempstr)>0 Then
    respscroll=i
    Exit For
    End If
    Next i
    
    
    'If line contains it - scroll down To found Line And Show At top of richedit
    If respscroll>0 Then
    MsgBox hdlg, "Search Result...", %MB_ICONASTERISK,"Information"
    Control Send hdlg,620,%EM_LINESCROLL,0,(respscroll-lineindex)
    Control Send hdlg,620, %EM_LINEINDEX,respscroll , 0 
    'Control Send hdlg,620 ,%EM_SCROLLCARET, 0, 0                         
    Exit Function   
    End If
    
    
    MsgBox hdlg, "Not Found", %MB_ICONWARNING, "Information"
    End Function
    
    This may be a way; may not be the best way however.

    Regards,
    catventure.
    Last edited by catventure; 06-12-2020 at 20:52.
    http://tab.thinbasic.com - Home Of The ThinBasic Adventure Builder Project. (Interactive Fiction/Text Adventure Maker)

  4. #4
    Senior Member catventure's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    630
    Blog Entries
    1
    Rep Power
    79
    Hi Petr,
    I tried using %em_setsel as you suggested - but I don't fully understand how to scroll the richedit to the location of the found text.
    (Thought it would do so automatically)
    Please see my example. It is more simpler and elegant than the previous one and would be ideal if the scrolling worked...
    It does find all the occurrences of the searchstring without problem but does not highlight or scroll to the found text how I wished it.

    Function findResp() As Long
    
    
    Local sText, selText As String
    Global startFrom, foundPos As Integer
    Global previousfoundPos As Long
    Global lastselText As String
    
    
    'get search text from find box
    Control Get Text hdlg, 627  To selText
    
    
    ' If empty string EXIT
    If selText="" Then
    lastselText=""
    Exit Function 
    End If
    
    
    'get all of richedit text
    Control Get Text hdlg, 620  To sText
    
    ' if search text not same as last time...
    If lastselText <> selText Then
    startFrom=1
    previousfoundPos=0
    End If
    
    
    lastselText=selText
    
    ' if more than first time search...
    If previousfoundPos<>0 Then
    startFrom = previousfoundPos+Len(selText)
    Else
    startFrom=1
    End If
    
    ' locate next occurrence of search string (if any)
    x=InStr(startFrom,sText,selText)
    
    
    If x<>0 Then
    foundPos=x
    Else
    foundPos=0
    End If
    
    
    If foundPos>0 Then
    'MsgBox 0, foundPos - Yes! it is finding the start positions of the searched for text OK
    
    
    Control Send hdlg,620, %EM_SETSEL,foundPos,Len(selText)
    ' OK SO FAR -- BUT HOW TO SCROLL RICHEDIT OR MOVE CURSOR/CARET TO SELECTED TEXT?? 
    previousfoundPos = foundPos+(startFrom-1)
    Exit Function
    Else
    MsgBox hdlg, "Could Not Find"+$CRLF+""+selText
    startFrom=1
    previousfoundPos=0 
    End If
    End Function
    
    The example finds all occurrences in the text from the beginning of the richedit to the end then will start from the top again should the user keep pressing the button again with the same search string... That is good, I like that.
    But how to take the user to the found text in the control?? This is the only bit missing from the puzzle. LOL!

    I tried a few things like
    control redraw hdlg,620
    control send hdlg, 620, %em_scrollcaret ,0,0
    to make the scrolling happen but no luck!

    I will think some more - but tired now.
    catventure/Phil.
    http://tab.thinbasic.com - Home Of The ThinBasic Adventure Builder Project. (Interactive Fiction/Text Adventure Maker)

  5. #5
    Senior Member catventure's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    630
    Blog Entries
    1
    Rep Power
    79
    Cannot yet solve this!

    If there is not a way then I did think that a nice alternative idea would be to have a find/replace dialog for a richedit control...
    User could easily find text with that!

    I found this old script on Powerbasic forum.
    https://forum.powerbasic.com/forum/u...ontrol?t=24564

    I do not understand it all - but apparently works - could be great if converted so it worked in my thinbasic adventure program for searching the code for strings in a simple richedit control.

    Bye,
    catventure.
    http://tab.thinbasic.com - Home Of The ThinBasic Adventure Builder Project. (Interactive Fiction/Text Adventure Maker)

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,089
    Blog Entries
    5
    Rep Power
    725
    Phil,

    my apologies, I cannot dive to the topic due to high workload.

    However - could you please try this minimal example and see, if the text scrolls to selection?

    USES "UI"
    
    
    Begin ControlID
      %bGotoPosition0
      %bGotoPosition1000
      %richEdit
    End ControlID
    
    
    
    
    function TBMain()  
      dword hDlg
    
    
      DIALOG NEW pixels, 0, "RichEdit control example", 100, 50, 640, 480, %WS_DLGFRAME | %ds_center | %WS_CAPTION | %WS_SYSMENU to hDlg
    
    
      DIM wx, hy AS LONG
      DIALOG GET CLIENT hDlg TO wx, hy
      
      string sBuffer
      for i as long = 1 to 1000
        sBuffer += format$(i, "0000") + $CRLF
      next
      
      CONTROL ADD rtf_GetClass, hDlg, %richEdit, sBuffer, 0, 0, wx/2, hy, %WS_CHILD | %WS_CLIPCHILDREN | %WS_VISIBLE | %ES_MULTILINE | %WS_VSCROLL | %WS_HSCROLL | %ES_AUTOVSCROLL | %ES_AUTOHSCROLL | %ES_WANTRETURN, %WS_EX_CLIENTEDGE
      CONTROL ADD button, hDlg, %bGotoPosition0, "Go to position 0", wx/2+5, 10, 100, 30
      CONTROL ADD button, hDlg, %bGotoPosition1000, "Go to position 1000", wx/2+5, 100, 100, 30
          
      '---Show dialog in MODAL state 
      DIALOG SHOW MODAL hDlg, call dlgCallback
    
    
    end function
    
    
    callback function dlgCallback() as long
      static hRich as dword
      
      SELECT CASE cbMsg
    
    
        case %WM_INITDIALOG  
          CONTROL HANDLE cbhndl, %richEdit TO hRich
    
    
        CASE %WM_COMMAND
          SELECT CASE cbCtl
            case %bGotoPosition0
              
              SendMessage(hRich, %EM_SETSEL, 0, 0)
              control set focus cbhndl, %richEdit
    
    
            case %bGotoPosition1000
              SendMessage(hRich, %EM_SETSEL, 1000,1000)
              control set focus cbhndl, %richEdit
    
    
    
    
          END SELECT 
        
        
      END SELECT
    
    
    end function
    

    P
    Last edited by Petr Schreiber; 13-12-2020 at 23:58.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7
    Senior Member catventure's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    630
    Blog Entries
    1
    Rep Power
    79
    Quote Originally Posted by Petr Schreiber View Post
    Phil,

    my apologies, I cannot dive to the topic due to high workload.

    However - could you please try this minimal example and see, if the text scrolls to selection?

    P
    Hi Petr.
    Thanks - will check this out and report back.
    A quick look and I see the text does not scroll beyond 215 or 216; so does not go to #1000
    catventure.
    http://tab.thinbasic.com - Home Of The ThinBasic Adventure Builder Project. (Interactive Fiction/Text Adventure Maker)

  8. #8
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,089
    Blog Entries
    5
    Rep Power
    725
    Hi Phil,

    the position 1000 is in characters, not in lines. So the result is correct - each line is 5 chars - 4 for digits, one for line break. 5 * 200 = 1000.

    To go to beginning of line 1000, you would need to do SendMessage(hRich, %EM_SETSEL, 1000*5-5,1000*5-5).


    Petr
    Last edited by Petr Schreiber; 15-12-2020 at 22:28.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  9. #9
    Senior Member catventure's Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    630
    Blog Entries
    1
    Rep Power
    79

    Thumbs up

    Quote Originally Posted by Petr Schreiber View Post
    Hi Phil,

    the position 1000 is in characters, not in lines. So the result is correct - each line is 5 chars - 4 for digits, one for line break. 5 * 200 = 1000.

    To go to beginning of line 1000, you would need to do SendMessage(hRich, %EM_SETSEL, 1000*5-5,1000*5-5).

    Petr
    Yes - of course! I think you've solved it Petr. I can highlight the the selections from the curs pos by adding the length of the digits in the numbered text entries.

    Now to see if I can adapt it to my program.

    I will need to parse the whole richedit text string from the starting point of each search to the beginning of the 'found' string to count the number of $crlf's and add those to the position so that I can get the position of the SetSel correct. The number of $crlf's will differ with each subsequent search of the same string or different string - so need a bit of tinkering about. I guess there may be both $lf and $crlf's! Something to do before Xmas

    Good work and thanks for simple and effective code example.
    http://tab.thinbasic.com - Home Of The ThinBasic Adventure Builder Project. (Interactive Fiction/Text Adventure Maker)

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,089
    Blog Entries
    5
    Rep Power
    725
    Hi Phil,

    I am glad I could be of help I am sure there is a better solution, but I did not do Win32 coding for some time.


    I am really happy it works for you!,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Page 1 of 2 12 LastLast

Similar Threads

  1. RichEdit
    By martin in forum thinBasic General
    Replies: 4
    Last Post: 17-10-2011, 17:57
  2. RichEdit background control color
    By ErosOlmi in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 2
    Last Post: 21-09-2006, 08:31
  3. RichEdit - RTF. We need some testers
    By ErosOlmi in forum Announcements
    Replies: 2
    Last Post: 19-09-2006, 00:23
  4. RichEdit AutoScroll?
    By catventure in forum thinBasic General
    Replies: 13
    Last Post: 20-01-2006, 16:40

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •