Search text in listview

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
PepeViyuela
Posts: 30
Joined: 06 Sep 2022, 12:04

Search text in listview

21 Feb 2024, 05:27

Find text from an uploaded txt file in listview separated by tabs

Code: Select all

#NoEnv
#SingleInstance force
SetBatchLines, -1

Gui, Add, Text, ,Search:
Gui, Add, Edit, w400 vSearchTerm gSearch
Gui, Add, ListView, gGo grid xm w300 r25 x20 y100 -Multi vmyList1, Column1|Column2

goto,Go
Gui, Submit, NoHide
return
Go:
Gui,ListView, LV1
Change = %A_ScriptDir%\Soft.txt
LV_Delete()
Loop, read, %Change%
{
 StringSplit, Column, A_LoopReadLine, %A_Tab%
LV_Add("Icon",Column1, Column2) 
}

LV_ModifyCol(1,100),LV_ModifyCol(2,100),

TotalItems := Data.Length()
LV_ModifyCol()
Gui, Add, StatusBar, , % "   " . TotalItems . " of " . TotalItems . " Items"
LV_ModifyCol(1,100),LV_ModifyCol(2,100),
Gui, Show, , ListView
Return


Search:

Return


GuiClose:
ExitApp
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Search text in listview

21 Feb 2024, 05:59

Hello,

This :arrow: example shows how to select each row whose first field contains the designated text.

Search a ListView

Code: Select all

; This script selects ListView rows based on contents of an edit control
#Requires AutoHotkey v1.1.33
colsToSearch := 2
Gui Font, s10
Gui Add, Edit, w500 vfind gSearch
Gui Add, ListView, xm vLV wp r10, A|B
LV_Add("", "ab cd efg", "hi jkl mno")
LV_Add("", "cabby"    , "boats")
LV_Add("", "tabby"    , "moats")
LV_Add("", "xxxxxxxx" , "zzz")
LV_ModifyCol()
Gui Show,, Search
Return

Search:
LV_Modify(0, "-Select")            ; Deselect all rows
Gui Submit, NoHide                 ; Get the text to find
If (find != "") {
 wordList := StrSplit(find, " ")
 Loop % LV_GetCount() {            ; Loop through rows
  row := A_Index
  For each, word in wordList       ; Loop through words (find any)
   If (word != "")
    Loop % colsToSearch {          ; Loop through columns
     LV_GetText(txt, row, A_Index) ; Get column's text
     If InStr(txt, word)           ; If text contains the word,
      LV_Modify(row, "+Select")    ;  then select the row
    }
 }
}
Return
Last edited by mikeyww on 21 Feb 2024, 06:25, edited 1 time in total.
PepeViyuela
Posts: 30
Joined: 06 Sep 2022, 12:04

Re: Search text in listview

21 Feb 2024, 06:22

Ok thank you,
It's ok but if the txt has 100 records you have to search it.
It is not possible that they are only the searched.

I am sorry for my writing in English but I am Spanish.
Thanks
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Search text in listview

21 Feb 2024, 06:28

Google Translate might help you to get your point across more clearly. I think that your goal is to find text in the ListView. This is what the script does.

One way to filter lines instead of selecting them would be to create a second ListView, and then add rows to it as the loop executes.
PepeViyuela
Posts: 30
Joined: 06 Sep 2022, 12:04

Re: Search text in listview

21 Feb 2024, 06:31

If that is the objective.
But it will not be possible to filter only the searches.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Search text in listview

21 Feb 2024, 06:33

I do not understand the question, and I see no examples here.
PepeViyuela
Posts: 30
Joined: 06 Sep 2022, 12:04

Re: Search text in listview

21 Feb 2024, 06:35

for example
I have a txt with 100 countries if I search for germany it will only filter germany.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Search text in listview

21 Feb 2024, 06:37

One way to filter lines instead of selecting them would be to create a second ListView, and then add rows to it as the loop executes.
PepeViyuela
Posts: 30
Joined: 06 Sep 2022, 12:04

Re: Search text in listview

21 Feb 2024, 06:44

Ok thank you.

Greetings.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Search text in listview

21 Feb 2024, 07:06

Filter a ListView

Code: Select all

; This script filters ListView rows based on contents of an edit control
#Requires AutoHotkey v1.1.33
rows   := 10                  ; Number of rows
header := "A|B"
Gui Font, s10
Gui Add, Edit, w500 vfind gSearch
Gui Add, ListView, % "xm vLV1 wp r" rows " Section", % header
LV_Add(, "ab cd efg", "hi jkl mno")
LV_Add(, "cabby"    , "boats")
LV_Add(, "tabby"    , "moats")
LV_Add(, "Germany"  , "France")
LV_Add(, "xxxxxxxx" , "zzz")
Gosub SetColWidths
Gui Add, ListView, % "xm vLV2 wp r" rows " ys Hidden", % header
Gosub SetColWidths
Gui Show,, Search
cols := LV_GetCount("Col")    ; Number of columns
Return

SetColWidths:                 ; Set column widths
LV_ModifyCol(1, 90)
LV_ModifyCol(2, 250)
Return

Search:
Gui Submit, NoHide            ; Get the text to find
If Trim(find) != "" {
 Gui ListView, LV2
 LV_Delete(), wordList := StrSplit(find, " ")
 Gui ListView, LV1
 Loop % LV_GetCount() {       ; Loop through rows
  row := A_Index, cell := [], found := False
  Gui ListView, LV1
  Loop % cols {               ; Loop through columns
   LV_GetText(txt, row, A_Index), cell.Push(txt)
   For each, word in wordList ; Loop through words (find any)
    (word != "") && found |= InStr(txt, word)
  }
  Gui ListView, LV2
  (found) && LV_Add(, cell*)
 }
 GuiControl Show, LV2
} Else GuiControl Hide, LV2
Return
PepeViyuela
Posts: 30
Joined: 06 Sep 2022, 12:04

Re: Search text in listview

22 Feb 2024, 02:06

Ok thank you.
it is not exactly what I wanted, because when I select the searched text LV2 disappears.
The idea is for example.
TxT
Amazon https://www.amazon.com/exec/obidos/subst/home/home.html/ref%3Dtab_gw_gw_1/104-4554179-1673530
Autohotkey https://www.autohotkey.com/


When performing the search in the first column I filtered so I can select and go to Link
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Search text in listview

22 Feb 2024, 05:05

Code: Select all

#Requires AutoHotkey v1.1.33
#NoEnv
#SingleInstance force
SetBatchLines, -1

TxtFile := A_ScriptDir . "\Soft.txt"
; FileRead, Content, %TxtFile%

; Generate test content >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Countries := ["Germany", "Greece", "Spain", "Syria", "United Kingdom", "USA"]
Content := ""
Loop, 10 {
   Index := A_Index
   For I, Country In Countries
      Content .= Country . "`tValue " . Index . "`r`n"
}
; Remove the last line break
Content := RTrim(Content, "`r`n")
; Generate test content <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; Create a line array
LineArr := StrSplit(Content, "`n", "`r")
Lines := LineArr.Length()
; Create te GUI
Gui, Add, Text, , Search:
Gui, Add, Edit, w400 vSearchTerm gSearch
Gui, Add, ListView, gGo grid xm w300 r25 x20 y100 -Multi Count%Lines% vmyList1, Column1|Column2
Gui, Add, StatusBar, , % "   " . TotalItems . " of " . TotalItems . " Items"
Gui, Show, , ListView
GoSub, Search
Return

Go:
   ; whatever you want to do
   MsgBox, ListView's label has been triggered!
Return

Search:
GuiControlGet, SearchTerm
GuiControl, -Redraw, myList1
LV_Delete()
Rows := 0
For I, Line In LineArr {
   Cols := StrSplit(Line, "`t")
   If (Searchterm = "") || InStr(Cols[1], SearchTerm)
      Rows := LV_Add("", Cols*)
}
LV_ModifyCol(1, 100)
LV_ModifyCol(2, 100)
GuiControl, +Redraw, myList1
SB_SetText("   " . Rows . " of " . Lines . " Items")
Return

GuiClose:
ExitApp
?
PepeViyuela
Posts: 30
Joined: 06 Sep 2022, 12:04

Re: Search text in listview

22 Feb 2024, 05:55

Ok that's exactly what I wanted
Thank you very much
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Search text in listview

22 Feb 2024, 06:55

I like this one, too, because it's simpler in not creating a second ListView, and it provides ideas about how you could start with a plain text file of your data.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Marium0505, mcl and 357 guests