AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A search dialogue to work on an edit control

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Klaus



Joined: 12 May 2005
Posts: 323
Location: Münster, Germany

PostPosted: Mon Dec 22, 2008 3:08 pm    Post subject: A search dialogue to work on an edit control Reply with quote

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)

Code:
; 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


Last edited by Klaus on Tue Dec 23, 2008 8:53 am; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
SoggyDog



Joined: 02 May 2006
Posts: 783
Location: Greeley, CO

PostPosted: Mon Dec 22, 2008 7:55 pm    Post subject: Reply with quote

I used your code to modify TheWeatherGuy's Wx Script.

Thanks!
_________________

SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."


Last edited by SoggyDog on Sun Jan 04, 2009 5:28 am; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
nick



Joined: 24 Aug 2005
Posts: 549
Location: Berlin / Germany

PostPosted: Tue Dec 23, 2008 5:58 am    Post subject: Reply with quote

Klaus wrote:
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?

Code:
   ; 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%

Have a nice and peaceful Christmas and a happy new year,
nick Wink

BTW: http://de.autohotkey.com/forum/topic4004.html
_________________
nick Wink
Back to top
View user's profile Send private message
Klaus



Joined: 12 May 2005
Posts: 323
Location: Münster, Germany

PostPosted: Tue Dec 23, 2008 8:54 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message Send e-mail
SoggyDog



Joined: 02 May 2006
Posts: 783
Location: Greeley, CO

PostPosted: Thu Jan 08, 2009 9:38 pm    Post subject: Reply with quote

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.

Code:
; 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

_________________

SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
mAdDoG



Joined: 29 Dec 2004
Posts: 75

PostPosted: Sat Jan 10, 2009 2:58 pm    Post subject: Reply with quote

SoggyDog,

Try this:
Code:
; 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:
Code:
Q


...& this in the text to be searched box:
Code:
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!!!
Back to top
View user's profile Send private message
SoggyDog



Joined: 02 May 2006
Posts: 783
Location: Greeley, CO

PostPosted: Mon Jan 12, 2009 4:28 am    Post subject: Reply with quote

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

SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
mAdDoG



Joined: 29 Dec 2004
Posts: 75

PostPosted: Sat Nov 07, 2009 12:13 am    Post subject: Reply with quote

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.

Code:


#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!!!
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5010
Location: the tunnel(?=light)

PostPosted: Sat Nov 07, 2009 7:35 am    Post subject: Reply with quote

I'm betting the answer is here:

Code:
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:

Code:
offset := pos + Strlen(find)

_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
mAdDoG



Joined: 29 Dec 2004
Posts: 75

PostPosted: Sat Nov 07, 2009 2:36 pm    Post subject: Reply with quote

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!!!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group