Jump to content

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

Listbox with incremental search


  • Please log in to reply
28 replies to this topic
Boskoop
  • Members
  • 75 posts
  • Last active: Jan 11 2011 09:12 PM
  • Joined: 01 Jan 2005
This script makes a listbox which can be searched incrementally.
The choosen item is assigned to a variable for further processing.
I needed it as part of a larger script.

I used Keyboardfreak's script "iswitchw - Incrementally switch
between windows using substrings" as a template. Nearly all the code comes
from that script. Only the errors er mine. Thanx Keyboardfreak!

Knowing nearly nothing about programming, it took me quite a long time
to extract the code I needed from Keyboardfreaks script (11 pages, printed!).
So I decided to post it in case others have the same problem and may find it useful.
Boskoop

; ---------------------------------------------------------------------
; Name:  			Incremental Listbox        
; Date:             25.2.2005
; Autor: 			Boskoop
; Language:         english
; Platform:         tested with XP
; AHK-Version:		1.24
;
; Description:                                                
; ---------------------------------------------------------------------
; On pressing CapsLock, the script  
; shows a listbox. The listbox retrieves its contents from a text-file. 
; It's possible do to an incremental search in this listbox:
; The listbox shows only the items starting with the letter(s) you type.
;
; Most of the code is from the script "iswitchw - Incrementally switch 
; between windows using substrings" by Keyboardfreak. Thanks.
; All errors er mine. 
; ---------------------------------------------------------------------

; ---------------------------------------------------------------------
; -- Configuration: ---------------------------------------------------
; ---------------------------------------------------------------------
;Your favourite hotkey:
CapsLock::						;Hotkey CapsLock to start 


;The name of the textfile containing the contents of the listbox:
ListName=Boskoop_Testfile_.tmp


; ---------------------------------------------------------------------
; -- Initialize: -------------------------------------------------
; ---------------------------------------------------------------------
;The following part is just for getting a working script. It should be removed
;when using the script as part of another script.
;This produces a file, named Boskoop_Testfile_.tmp and places in the working directory. 
;Delete this file after playing with this script.
;The file contains a wordlist,which is used to fill the listbox.

IfInString,Listname,Boskoop_Testfile_	
{
	Fileappend, car`nbicyle`ntrain`nplane`nroad`nrailway station`ntrack`nairport`ncontrol tower`nwheel`nred`ngreen`npink`nblue`ngrey
`nsilver`nblack`nyellow`nbrown`nwhite`nhair`nnose`neye`near`nface`nmustache`nneck`ncollar`narm`nhand`nforearm`nforehead
`nfinger`nthumb`npalm`nback`nstomach`nleg`nthigh`nfoot`ntoe`nshoe`nsock`nstocking`ntrousers`njumpsuit`nskirt`nblouse`ndress`nshirt
`ntie`nnecklace`nearring`nSun`nMoon`nMercur`nVenus`nEarth`nMars`nJupiter`nSaturn`nNeptun`nPluto
`nAfrica`nAmerica`nAntarctica`nAsia`nAustralia`nEurope`nCanada`nUSA`nMexico`nGuatemala`nHonduras`nCuba`n,%ListName%
}

; ---------------------------------------------------------------------
; -- Autoexecute ------------------------------------------------------
; ---------------------------------------------------------------------

Gui, Add, ListBox, vChoice gListBoxClick w300 h250 hscroll vscroll 
Gui, Add, Text, x6 y264 w50 h20, Search`:
Gui, Add, Edit, x66 y261 w240 h20

Gosub RefreshListBox

search =	

;The input-command in this loop processes keys pressed by the user 
;or send by the "send"-command
Loop
{
    Input, input, L1, {enter}{esc}{backspace}{up}{down}{pgup}{pgdn}{tab}{left}{right}
    	if ErrorLevel = EndKey:escape
		{
			Gui, cancel
			Gosub GuiClose
		}
    	if ErrorLevel = EndKey:enter
		{
			GoSub, WordRetrieve
			continue
		}
    	if ErrorLevel = EndKey:backspace
		{
			GoSub, DeleteSearchChar
			continue
		}
    	if ErrorLevel = EndKey:up
		{
			Send, {up}
			continue
		}
    	if ErrorLevel = EndKey:down
		{
			Send, {down}
			continue
		}
		if ErrorLevel = EndKey:pgup
		{
			Send, {pgup}
			continue
		}
		if ErrorLevel = EndKey:pgdn
		{
			Send, {pgdn}
			continue
		}
   
    search = %search%%input%					;Assembles the search string
    GuiControl,, Edit1, %search%				;Displays the search string in the edit control
    StringLen,SearchLength,Search
    Gosub RefreshListBox 
    continue
}

return


; ---------------------------------------------------------------------
; -- Subroutines ------------------------------------------------------
; ---------------------------------------------------------------------

;Assigns the chosen item to the variable "Choice". 
WordRetrieve:
Gui, submit, noHide
GuiControlGet, Choice  						; Retrieve the ListBox's current selection.
msgBox, You choose:`n`n%Choice%
return
; ---------------------------------------------------------------------

;Exits the script on closing the GUI
GuiClose:
GuiEscape:
	ExitApp
; ---------------------------------------------------------------------

;Refreshes the listbox according to the search criteria:
RefreshListBox:
Wordlist=

Loop, read, %ListName% 
{
	StringLeft, Fragment,A_LoopReadLine, %SearchLength%
	IfInString, Fragment,%Search%		
		Wordlist=%Wordlist% |%A_LoopReadLine%	
	Else
		continue
}

Gui, Show,
GuiControl,, ListBox1, %wordlist%
GuiControl, Choose, ListBox1, 1
return
;-------------------------------------------------------------------------

;Delete the last character and update Listbox:
DeleteSearchChar:

if search =
    return

StringTrimRight, search, search, 1
GuiControl,, Edit1, %search%
GoSub, RefreshListBox

return
;-------------------------------------------------------------------------

; Handle mouse click events on the list box:
ListBoxClick:

if A_GuiControlEvent = DoubleClick
    send, {enter}

return



Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I've a feeling this will be useful. Thanks.

keyboardfreak
  • Members
  • 217 posts
  • Last active: Sep 27 2010 07:21 PM
  • Joined: 09 Oct 2004
Good idea. Two comments:

Input, input, L1, {enter}{esc}{backspace}{up}{down}{pgup}{pgdn}{tab}{left}{right}

You probably don't need the {tab}{left}{right} keys in the Input command, since you don't handle those cases. They are probably leftovers from my original script.

As you probably know the Input command grabs the keyboard input, so you can't use the keyboard in other applications while this command is running. It is no problem for the window switcher, since you're not likely to use an other application while swicthing between windows.

However, if you plan to make your incremental listbox a part of a more usual application (where it's normal to use other apps while your script is running) then it could be a problem.

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
nice script! and a good addition to my scriptlet library! :)

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


Boskoop
  • Members
  • 75 posts
  • Last active: Jan 11 2011 09:12 PM
  • Joined: 01 Jan 2005
Thanks for your kind feedback. This was actually the first time I did post anything else but stupid questions.

@keyboardfreak: Your "iswitchw"-script i a treasury full of useful subroutines. Thanks again for it.

I noticed that "input" grabs all the keyboard input. It's no problem for my script (a keyword-tool, which sends the chosen item to a given control). But you are right- it may pose a problem in other applications.

I'm not sure, maybe it's possible to use an edit control instead of "input".

Another issue is that this gets really slow when using long lists. Its unusable for one of my lists with>20.000 keywords . I have no idea how to resolve this (except for buying a faster computer...).

Boskoop

ratz
  • Guests
  • Last active:
  • Joined: --
Great script! Question: How can i make this work with multiple words. I noticed that when i type a {space}, the character isnt shown in the listbox. Is this necessary to make tbe script work?

for example, if i put in the boskoop.tmp file, "railway station" and "railway stationary," the script wont let me go to the second word. Or if i put "railway student," there is no way of choosing this.

Is there anyway of making this work? Thanks.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Good idea, but the code is a bit outdated, no need for Input now: the Edit control can fire an event on each typed key.
Here is my take at this kind of script:
Gui Add, Edit, w300 h20 vsearchedString gIncrementalSearch
Gui Add, ListBox, vchoice gListBoxClick w300 h250 hscroll vscroll
Gui Add, Button, gListBoxClick Default, OK
Gosub FillListBox
Gui Show
Return

IncrementalSearch:
	Gui Submit, NoHide
	len := StrLen(searchedString)
	itemNb := 1
	Loop Parse, listContent, |
	{
		StringLeft part, A_LoopField, len
;~ 		Tooltip %part% (%A_LoopField%)
;~ 		Sleep 500
		If (part = searchedString)
		{
			itemNb := A_Index
			Break
		}
	}

	Tooltip %searchedString% (%itemNb%)
	SetTimer HideTooltip, 1000
	GuiControl Choose, choice, %itemNb%
Return

HideTooltip:
	SetTimer HideTooltip, Off
	Tooltip
Return

ListBoxClick:
	Gui Submit, NoHide
	MsgBox Choice: %choice%
Return

GuiClose:
GuiEscape:
ExitApp

FillListBox:
	listContent =
( Join|
car|bicyle|train|plane|road|railway station|track|airport|control tower|wheel
red|green|pink|blue|grey|silver|black|yellow|brown|white
hair|nose|eye|ear|face|mustache|neck|collar|arm|hand|forearm|forehead
finger|thumb|palm|back|stomach|leg|thigh|foot|toe
shoe|sock|stocking|trousers|jumpsuit|skirt|blouse|dress|shirt|tie|necklace|earring
Sun|Moon|Mercure|Venus|Earth|Mars|Jupiter|Saturn|Neptun|Pluto
Africa|America|Antarctica|Asia|Australia|Europe
Canada|USA|Mexico|Guatemala|Honduras|Cuba
Gui Add|Gui Show|Gui Submit|Gui Hide|Gui Destroy
)
	GuiControl, , choice, %listContent%
Return
Seems to work OK with Space.
If I missed some functionnality, perhaps I can add it.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

ratz
  • Guests
  • Last active:
  • Joined: --
Wow PhiLho! Perfect. Exactly what I was looking for. Works great, and much cleaner.

For the "FillListBox" routine, I can read these from a text file right? Ill try...

Thank you very much. Greatly appreciative.

fast
  • Guests
  • Last active:
  • Joined: --
great script!
Is there any way to make one script for read a txt file with a list of word eg:
able
about
above
across
act
action
actually
add
added
adding
and when I type "dd" see in the edit all the word content "dd" (in this case, add added adding,
like an "autocomplete" but in all position of the caracters) with istant refresh?
TNX

scriptmonkey
  • Members
  • 176 posts
  • Last active: Jan 08 2015 07:42 PM
  • Joined: 19 May 2006
Anyone know if someone made a script like this :

I have about 16,000 PDF files with unique filenames in a folder, but some of them revisions. Examples: 1234.pdf, 1234_00.pdf, 1234_01.pdf, etc. I would like the user to be able to type a filename they are looking for, and the script searches for all files with that #. So if the user types 1234, all files thta start with 1234 will show up in the search. The user would then be able to double-click any of the filenames to open the PDF.

fast
  • Guests
  • Last active:
  • Joined: --
ReallyUltraFast 320mph, sorry 3200mph Wink
10x-1000x more fast

http://www.autohotke... ... h&start=75

  • Guests
  • Last active:
  • Joined: --
I've adapted the subroutine WordRetrieve to send the item of the list (see :?: )

;Assigns the chosen item to the variable "Choice".
WordRetrieve:
Gui, submit, noHide
GuiControlGet, Choice ; Retrieve the ListBox's current selection.
Gui, Hide
Send, %Choice% ; adaption :?:
ExitApp
return

I have ot insert many items one after another. How do I restart the listbox automatically without hotkey?

Thanks in advance

Dirk

MsgBox
  • Members
  • 204 posts
  • Last active: Jan 05 2011 01:40 AM
  • Joined: 17 Nov 2005
Here is a similar script of Laszlo's that I've found to be very useful.
Incremental search for text to send

I made a list of all AHK commands and added it to List=
List =
#AllowSameLineComments|#ClipboardTimeout|#CommentFlag etc
Result = no more misspelt commands. :)

Boskoop
  • Members
  • 75 posts
  • Last active: Jan 11 2011 09:12 PM
  • Joined: 01 Jan 2005
The script that used "Listbox with incremental search" made some progress since february 05. Unfortunately I got stuck in the middle of the english translation (no time because of nice weather, work, and a lot of other projects) so I posted it in the German forum.

Kollektor is a crossbreed between clipboard tool and text module tool. You can add highlighted text to the phrase list by pressing CTRL-Capslock. Pressing Capslock shows a list of phrases. You can filter it by typing part of a word. By pressing "Enter" the phrase is inserted in the last active text-window.

Posted Image

There seems to be some interest in the functionality- so maybe someone finds this useful. You can download it here. Both the script's comments and the helpfile are in German. But I'm working on it...

Boskoop

robiandi
  • Guests
  • Last active:
  • Joined: --
settitlematchmode 3
is highly recommended in the useful script Kollektor