Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

A search dialogue to work on an edit control


  • Please log in to reply
9 replies to this topic
Klaus
  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
Hi, Community,
this morning I posted a question of how to code a search dialogue
which would highlight the search string in the haystack and which
provides a repetitive search.
HugoV gave me a precious hint of how to find Laszlo's
script and here's the result.
Just load down the script and let it run.
Enter a search phrase in the edit field underneath the search button,
subsequently click the search button.

Known limitation:
When the text is wider than the edit area, the found string will be
marked but no horizontical scrolling will happen. Any ideas?

To be done:
Install hotkeys ctrl-f (start search) and F3 (continue search)

; a little gui with some text from the manual
Gui 1:Add, Button, gFindString, Search
Gui 1:Add, Edit, w100 vfind
Gui 1:Add, Edit, w330 r3 vContent HSCROLL
Gui 1:Add, StatusBar,, Type the search string to look for and click the Search-button
SB_SetParts(300)
Gui 1:Show,, FindSample
WinGet ControlID, ID, FindSample

someText=
(
Creating a script
Each script is a plain text file containing commands
to be executed by the program (AutoHotkey.exe).
A script may also contain hotkeys and hotstrings, or
even consist entirely of them. However, in the absence
of hotkeys and hotstrings, a script will perform its
commands sequentially from top to bottom the moment
it is launched.

To create a new script:

Open Windows Explorer and navigate to a folder of
your choice.
Pull down the File menu and choose New >> AutoHotkey
Script (or Text Document).
Type a name for the file, ensuring that it ends in .ahk.
For example: Test.ahk
Right-click the file and choose Edit Script.
On a new blank line, type the following:
#space::Run www.google.com
The symbol # stands for the Windows key, so #space means
holding down the Windows key then pressing the spacebar
to activate a hotkey. The :: means that the subsequent
command should be executed whenever this hotkey is
pressed, in this case to go to the Google web site.
To try out this script, continue as follows:

Save and close the file.
In Windows Explorer, double-click the script to launch
it. A new tray icon appears. Hold down the Windows key
and press the spacebar. A web page opens in the default
browser.
To exit or edit the script, right click its tray icon.
Note: Multiple scripts can be running simultaneously,
each with its own tray icon. Furthermore, each script
can have multiple hotkeys and hotstrings.
)
Guicontrol,,Edit2, %someText%
return

FindString:
   Gui, Submit, Nohide

   if (find != lastFind) {
    offset = 0
    hits = 0
   }

   GuiControl 1:Focus, Content                           ; focus on main help window to show selection
   SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID%  ; Scroll to top
   StringGetPos pos, Content, %find% ,,offset            ; find the position of the search string
   if (pos = -1) {
    if (offset = 0) {
      SB_SetText("'" . find . "' not found", 1)
      SB_SetText("", 2)
    }
    else {
      SB_SetText("No more occurrences of '" . find . "'")
      SB_SetText("", 2)
      offset = 0
      hits = 0
    }
    return
   }
   StringLeft __s, Content, %pos%                        ; cut off end to count lines
   StringReplace __s,__s,`n,`n,UseErrorLevel             ; Errorlevel <- line number
   addToPos=%Errorlevel%
   SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID% ; Scroll to visible
   SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID% ; Select search text
   ; http://msdn.microsoft.com/en-us/library/bb761637(VS.85).aspx
   ; Scroll the caret into view in an edit control:
   SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID% 
   offset := pos + addToPos + Strlen(find)
   lastFind = %find%
   hits++
   SB_SetText("'" . find . "' found in line " . addToPos + 1, 1)
   SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2)
Return
Let me know what you think and of course report me all the bugs.
I'm aware that I only played around with the excellent code of
Laszlo, honour to whom honour is due!

Have a nice and peaceful Christmas and a happy new year,
Klaus

SoggyDog
  • Members
  • 803 posts
  • Last active: Mar 04 2013 06:27 AM
  • Joined: 02 May 2006
I used your code to modify TheWeatherGuy's Wx Script.

Thanks!

nick
  • Members
  • 549 posts
  • Last active: Jul 03 2010 09:31 PM
  • Joined: 24 Aug 2005

Known limitation:
When the text is wider than the edit area, the found string will be
marked but no horizontical scrolling will happen. Any ideas?

[color=red]; [/color]SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID% ; Scroll to visible
   SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID% ; Select search text
   ; http://msdn.microsoft.com/en-us/library/bb761637(VS.85).aspx
   ; Scroll the caret into view in an edit control:
   [color=red]SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%[/color]
Have a nice and peaceful Christmas and a happy new year,
nick :wink:

BTW: http://de.autohotkey.../topic4004.html
nick :wink:

Klaus
  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
Hi, nick,

thanks for your most welcome contribution!
That horizontal scrolling was necessary.

Merry Christmas and a new year as you desire it!
Klaus

PS: Gruß nach Berlin!

SoggyDog
  • Members
  • 803 posts
  • Last active: Mar 04 2013 06:27 AM
  • Joined: 02 May 2006
Okay...
Here I go again twiddling with other peoples code...
I have modified this script to automatically populate the 'Content' (text to search) using OnClipboardChange.

; http://www.autohotkey.com/forum/viewtopic.php?t=39120

#NoEnv
#SingleInstance Ignore
#Persistent
SendMode Input

Gui 1: Add, Button, x9 y5 w46 h23 gFindString Default, Search
Gui 1: Add, Edit, x9 y34 w100 h21 vfind, 
Gui 1: Add, Edit, x9 y61 w330 h64 r3 vContent HSCROLL,
Gui 1: Add, Text, x116 y37 w200 h20 , <----- Enter 'Search String' here
Gui 1: Add, StatusBar,, Copy text to search, enter Search String and click 'Search'
SB_SetParts(300)
Gui 1: Show,, Simple Text search
WinGet ControlID, ID, Simple Text search

searchText = Initializing...

Guicontrol,,Edit2, %searchText%
Return

OnClipboardChange:
if A_EventInfo = 1
{
	If searchText = Initializing...
	{
		sleep, 1000
		searchText = Ready...`nCopy text to search here.
	}
	else
	{
		searchText = %clipboard%
	}
	GuiControl,,Content,%searchText%
	Gui, Show
}
Return

FindString:
   Gui, Submit, Nohide

   if (find != lastFind) {
    offset = 0
    hits = 0
   }

   GuiControl 1:Focus, Content
   SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID%
   StringGetPos pos, Content, %find% ,,offset
   if (pos = -1) {
    if (offset = 0) {
      SB_SetText("'" . find . "' not found", 1)
      SB_SetText("", 2)
    }
    else {
      SB_SetText("No more occurrences of '" . find . "'")
      SB_SetText("", 2)
      offset = 0
      hits = 0
    }
    Return
   }
   StringLeft __s, Content, %pos%
   StringReplace __s,__s,`n,`n,UseErrorLevel
   addToPos=%Errorlevel%
   SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID%
   SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%
   offset := pos + addToPos + Strlen(find)
   lastFind = %find%
   hits++
   SB_SetText("'" . find . "' found in line " . addToPos + 1, 1)
   SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2)
Return

GuiClose:
ExitApp


mAdDoG
  • Members
  • 75 posts
  • Last active: Jan 11 2010 11:05 PM
  • Joined: 29 Dec 2004
SoggyDog,

Try this:
; a little gui with some text from the manual 
Gui 1:Add, Button, gFindString, Search 
Gui 1:Add, Edit, w100 vfind 
Gui 1:Add, Edit, w330 r40 vcontent HSCROLL 
Gui 1:Add, StatusBar,, Type the search string to look for and click the Search-button 
;SB_SetParts(300) 
Gui 1:Show,, FindSample 
WinGet ControlID, ID, FindSample 


FindString: 
Gui, Submit, Nohide 

if (find != lastFind) { 
offset = 0 
hits = 0 
} 

GuiControl 1:Focus, Content ; focus on main help window to show selection 
SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID% ; Scroll to top 
StringGetPos pos, Content, %find% ,,offset ; find the position of the search string 
if (pos = -1) { 
if (offset = 0) { 
SB_SetText("'" . find . "' not found", 1) 
SB_SetText("", 2) 
} 
else { 
SB_SetText("No more occurrences of '" . find . "'") 
SB_SetText("", 2) 
offset = 0 
hits = 0 
} 
return 
} 
StringLeft __s, Content, %pos% ; cut off end to count lines 
StringReplace __s,__s,`n,`n,UseErrorLevel ; Errorlevel <- line number 
addToPos=%Errorlevel% 
SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID% ; Scroll to visible 
SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID% ; Select search text 
; http://msdn.microsoft.com/en-us/library/bb761637(VS.85).aspx 
; Scroll the caret into view in an edit control: 
SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID% 
offset := pos + addToPos + Strlen(find) 
lastFind = %find% 
hits++ 
SB_SetText("'" . find . "' found in line " . addToPos + 1, 1) 
SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2) 
Return 

What I'm trying to figure out is why the script skips characters during a search...

Paste this into the search for box:
Q

...& this in the text to be searched box:
QQQ
QQQQ
QQQQQ

When the search button is repeadedly pressed, why does the script skip characters during the search?
-buttons, buttons,...
I like to push all the buttons!!!

SoggyDog
  • Members
  • 803 posts
  • Last active: Mar 04 2013 06:27 AM
  • Joined: 02 May 2006
@mAdDoG

a) I like a larger 'Content' area as well, but why did you disable the hit counter?

B) I hadn't noticed the problem with repeating characters (though you are clearly correct).
I never really tested the code Klaus posted, rather just made some mods to his existing 'engine'.
Perhaps he can explain why this happens.

mAdDoG
  • Members
  • 75 posts
  • Last active: Jan 11 2010 11:05 PM
  • Joined: 29 Dec 2004
Can someone help me figure this out?

If you run a copy of this script & repeatedly hit the Search-button,
You'll find an obviuos skipping pattern.


#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

text = xxxxxx`nxxxxxx`nxxxxxx`nxxxxxx

Gui 1:Add, Button, , Search
Gui 1:Add, Edit, w100 vfind, x
Gui 1:Add, Edit, w330 r30 vContent HSCROLL, %text%
Gui 1:Add, StatusBar,, Type the search string to look for and click the Search-button
SB_SetParts(300)
Gui 1:Show,, FindSample
WinGet ControlID, ID, FindSample
return


ButtonSearch:


   Gui, Submit, Nohide
   if (find != lastFind) 
   {
    offset = 0
    hits = 0
   }


   GuiControl 1:Focus, Content                                ; focus on main help window to show selection
   SendMessage 0xB6, 0, -999, Edit2, ahk_id %ControlID%       ; Scroll to top
   StringGetPos pos, Content, %find% ,,offset                 ; find the position of the search string


   if (pos = -1)
   {
    if (offset = 0) 
    {
      SB_SetText("'" . find . "' not found", 1)
      SB_SetText("", 2)
    }
    else 
    {
      SB_SetText("No more occurrences of '" . find . "'")
      SB_SetText("", 2)
      offset = 0
      hits = 0
    }
    return
   }


   StringLeft __s, Content, %pos%                        ; cut off end to count lines
   StringReplace __s,__s,`n,`n,UseErrorLevel             ; Errorlevel <- line number
   addToPos=%Errorlevel%


   SendMessage 0xB6, 0, ErrorLevel, Edit2, ahk_id %ControlID%                                    ; Scroll to visible
   SendMessage 0xB1, pos + addToPos, pos + addToPos + Strlen(find), Edit2, ahk_id %ControlID%     ; Select search text
   SendMessage, EM_SCROLLCARET := 0xB7, 0, 0, Edit2, ahk_id %ControlID%      ; Scroll the caret into view in an edit


   offset := pos + addToPos + Strlen(find)  
   lastFind = %find%


   hits++
   SB_SetText("'" . find . "' found in line " . addToPos + 1, 1)
   SB_SetText(hits . (hits = 1 ? " hit" : " hits"), 2)


Return


guiclose:
ExitApp


I've spent hours trying to figure out why it behaves like this & need some help.
-buttons, buttons,...
I like to push all the buttons!!!

sinkfaze
  • Moderators
  • 6367 posts
  • Last active:
  • Joined: 18 Mar 2008
I'm betting the answer is here:

offset := pos + addToPos + Strlen(find)

I can't figure out why addToPos is added to the offset, but with each increase in the ErrorLevel moving to the next line the offset (and thus the results) gets more skewed. Try modifying that line to this:

offset := pos + Strlen(find)


mAdDoG
  • Members
  • 75 posts
  • Last active: Jan 11 2010 11:05 PM
  • Joined: 29 Dec 2004
That was it.

I was just about to rip everything up & start from scratch, & thought to see if anyone answered the post.
Thanks!
-buttons, buttons,...
I like to push all the buttons!!!