FindString in TextBox

Started by fred_tomke · 2008-07-04 01:19 UTC · 3 replies · SMF topic #372

Hello,

I tried to create a search & find function in a form.
I have two textboxes: one single line edit to input the pattern and another textbox as multiline edit for the text which includes the pattern. Because there is not Find-method for textboxes  I use Lisp-functions to do that. If the pattern is found the text is selected using SetSel successfully. But unfortunately, the multiline edit won't be scrolled to the line with the selected part when using SetSel. I tried a workaround using GetLineLength, sum up the linelengths until the start character shall be found. But I have the feeling that the sum of linelengths up to the line with the first position of the pattern doesn't match with the returning result of vl-string-search.

What would be the best way to scroll to the correct line of current selection?

Fred
Hello,

I did some research. The following workaround is not nice but it's working successfully:


  • store the orignal text in a variable

  • get the startposition of the pattern in the whole text

  • cut the rest off from the whole text after startposition of the pattern using substr

  • assign the left part to the multiline editbox

  • read out the number of lines into a variable intLine using GetLineCount

  • assign the original whole text to the multiline editbox again

  • use setsel

  • use linescroll with intLine



Here is a codesnip for imagination and for whom who wants to know:

(defun c:edt_search_OnReturnPressed ( / strText strSearch intOld intLine intGes)
    (if (not intSearch) (setq intSearch 0))
    (setq intOld intSearch)
    (if (and (setq strText (dcl_Control_GetText edt_html))
     (setq strSearch (dcl_Control_GetText edt_search))
     (/= (vl-string-trim " " strText) "")
     (/= (vl-string-trim " " strSearch) ""))
      (if (setq intSearch (vl-string-search (strcase strSearch) (strcase strText)
    (if (/= strSearch strOldSearch) (setq intSearch 0) intSearch)))
(progn
  (dcl_Control_SetText edt_html (substr strText 1 (1+ intSearch)))
  (setq intLine (dcl_TextBox_GetLineCount edt_html))
  (dcl_Control_SetText edt_html strText)
  (dcl_TextBox_SetSel edt_html intSearch (+ intSearch (strlen strSearch)))
  (dcl_TextBox_LineScroll edt_html (if (> intLine 0) (1- intLine) intLine))
  (setq intSearch (1+ intSearch))
  (setq strOldSearch strSearch)
); progn
(if (> intOld 0)
  (if (= (dcl_messagebox "I'm at the end of the document now. Shall I start from the beginning?" "Search" 5 3) 6)
    (progn
      (setq intSearch 0)
      (c:edt_search_OnReturnPressed)
    ); progn
  ); if
  (dcl_messagebox "Text was not found!" "Search" 2 1)
); if
      ); if
    ); if
  )


Maybe someone has another idea to make sure that the multiline editbox does automatically scroll to the current selection.

Fred
The edit control's SetSel function has an argument for scrolling the selection into view. Currently that argument is hard coded to "NoScroll". It is simple to add an optional argument to SetSel to specify whether to scroll the selection into view, so I will add that for the next build.
That's great, thanks!

Fred