Search in listview gui Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Pilu
Posts: 84
Joined: 22 Jun 2018, 01:13

Search in listview gui

Post by Pilu » 05 Feb 2021, 10:12

Hi,

I created a Listview gui. How can i search text in every columns and list every matched lines what contains the text or the part of searched text.And if the search textbox is empty ,show all elements?

Code: Select all

#SingleInstance force
#NoEnv
SendMode Input
SetWorkingDir, %A_ScriptDir% 
SetBatchLines, -1

F1 = Data2.txt


Loop,Read,%F1%
{
	Line%A_Index% := A_LoopReadLine
	Line0 = %A_Index%
}


Gui, Add, Text,,Search:

Gui, Add, Edit, w400 vA_SearchTerm gSearch

Gui, Add, ListView,grid x5 y+5 r5 w1190 h450 -LV0x10, Barcode|Field_1|Field_2|Field_3|Field_4|Field_5|Field_6|Field_7|Field_8|Field_9|Field_10|Field_11|Field_12|Field_13|Field_14|Field_15|Field_16|Field_17|Field_18|Field_19|Field_20|Field_21|Field_22|Field_23|Field_24|Field_25|Field_26  ;27 columns



Loop, %Line0%
{   
	StringSplit, Array, Line%A_Index%, `,
	LV_Add("",Array1,Array2,Array3,Array4,Array5,Array6,Array7,Array8,Array9,Array10,Array11,Arra12,Array13,Array14,Array15,Array16,Array17,Array18,Array19,Array20,Array21,Array22,Array23,Array24,Array25,Array26,Array27,A_Index)
	
}


Gui, Show, h500 w1200

return

GuiClose:

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

Re: Search in listview gui

Post by mikeyww » 05 Feb 2021, 11:43

Code: Select all

headers := "1|2|3", del := []
Gui, 1:New
Gui, Add, ListView,, %headers%
LV_Add("", "A", "Boy", "C"), LV_Add("", "d", "e", "f"), LV_Add("", "Box", "e", "f")
InputBox, find, Search, Enter a search string.,, 300, 125
If ErrorLevel
 Return
Loop, % LV_GetCount() {
 lineNum := A_Index, found := False
 Loop, 3
  LV_GetText(cell, lineNum, A_Index), found |= Instr(cell, find) || find = ""
 del.Push(found)
}
Gui, 2:New
Gui, Font, s12
Gui, Add, ListView,, %headers%
For k, v in del {
 If !v
  Continue
 cell := []
 Gui, 1:Default
 Loop, 3
  LV_GetText(thisCell, k, A_Index), cell.Push(thisCell)
 Gui, 2:Default
 LV_Add("", cell.1, cell.2, cell.3)
}
LV_ModifyCol()
Gui, Show,, Matches
Last edited by mikeyww on 05 Feb 2021, 11:52, edited 1 time in total.
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Search in listview gui

Post by colt » 05 Feb 2021, 11:52

something like this

Code: Select all

#SingleInstance force
#NoEnv
SendMode Input
SetWorkingDir, %A_ScriptDir% 
SetBatchLines, -1

fileRead ,rawData,*t Data2.txt 

if(debug := true)
{
	rawData := ""
	strings := ["cat","dog","pig","duck"]
	loop 20
	{	
		rowNum := A_index
		rowText := ""
		loop % strings._maxIndex()	
		{
			colNum := A_index
			random,num,1,% strings._maxIndex()		
			rowText .= rowNum . "-" . colNum . ":" . strings[num] . ","
		}
		rowText := substr(rowText ,1,strlen(rowText )-1)
		rawData .= rowText . "`n"
	}
	rawData := substr(rawData,1,strlen(rawData)-1)
}
masterDataArr := strSplit(rawData,"`n") ;core array

Gui, Add, Text,,Search:
Gui, Add, Edit, w400 vA_SearchTerm gSearch
global LVHandle := ""
Gui, Add, ListView, hwndLVHandle grid x5 y+5 r5 w1190 h450 -LV0x10, Barcode|Field_1|Field_2|Field_3
populateTable(masterDataArr) 
Gui, Show, h500 w1200

return

populateTable(data)
{
	LV_Delete()
	for lineNum,content in data
	{		
		lineArr := strSplit(content,",")
		LV_Add("",lineArr[1],lineArr[2],lineArr[2],lineArr[4])
	}
}

GuiClose:
	ExitApp
return

Search:
	gui Submit,noHide
	
	if(A_searchTerm)
	{
		searchResult:=[]
		for lineNum,content in masterDataArr 
		{
			if(instr(content,A_SearchTerm ))
			{				
				searchResult.push(content)
			}
		}	
		populateTable(searchResult)
	}
	else
	{
		populateTable(masterDataArr) 
	}
return
Pilu
Posts: 84
Joined: 22 Jun 2018, 01:13

Re: Search in listview gui

Post by Pilu » 08 Feb 2021, 06:10

colt wrote:
05 Feb 2021, 11:52
something like this

Code: Select all

#SingleInstance force
#NoEnv
SendMode Input
SetWorkingDir, %A_ScriptDir% 
SetBatchLines, -1

fileRead ,rawData,*t Data2.txt 

if(debug := true)
{
	rawData := ""
	strings := ["cat","dog","pig","duck"]
	loop 20
	{	
		rowNum := A_index
		rowText := ""
		loop % strings._maxIndex()	
		{
			colNum := A_index
			random,num,1,% strings._maxIndex()		
			rowText .= rowNum . "-" . colNum . ":" . strings[num] . ","
		}
		rowText := substr(rowText ,1,strlen(rowText )-1)
		rawData .= rowText . "`n"
	}
	rawData := substr(rawData,1,strlen(rawData)-1)
}
masterDataArr := strSplit(rawData,"`n") ;core array

Gui, Add, Text,,Search:
Gui, Add, Edit, w400 vA_SearchTerm gSearch
global LVHandle := ""
Gui, Add, ListView, hwndLVHandle grid x5 y+5 r5 w1190 h450 -LV0x10, Barcode|Field_1|Field_2|Field_3
populateTable(masterDataArr) 
Gui, Show, h500 w1200

return

populateTable(data)
{
	LV_Delete()
	for lineNum,content in data
	{		
		lineArr := strSplit(content,",")
		LV_Add("",lineArr[1],lineArr[2],lineArr[2],lineArr[4])
	}
}

GuiClose:
	ExitApp
return

Search:
	gui Submit,noHide
	
	if(A_searchTerm)
	{
		searchResult:=[]
		for lineNum,content in masterDataArr 
		{
			if(instr(content,A_SearchTerm ))
			{				
				searchResult.push(content)
			}
		}	
		populateTable(searchResult)
	}
	else
	{
		populateTable(masterDataArr) 
	}
return
Hi @colt , I try it. it works very good with few row, but when i work my database it will be slow and dont show the results. I work with 130 000 row and this number is grow day to day.
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Search in listview gui

Post by hasantr » 08 Feb 2021, 08:29

Pilu wrote:
05 Feb 2021, 10:12
Hi,

I created a Listview gui. How can i search text in every columns and list every matched lines what contains the text or the part of searched text.And if the search textbox is empty ,show all elements?

Code: Select all

#SingleInstance force
#NoEnv
SendMode Input
SetWorkingDir, %A_ScriptDir% 
SetBatchLines, -1

F1 = Data2.txt


Loop,Read,%F1%
{
	Line%A_Index% := A_LoopReadLine
	Line0 = %A_Index%
}


Gui, Add, Text,,Search:

Gui, Add, Edit, w400 vA_SearchTerm gSearch

Gui, Add, ListView,grid x5 y+5 r5 w1190 h450 -LV0x10, Barcode|Field_1|Field_2|Field_3|Field_4|Field_5|Field_6|Field_7|Field_8|Field_9|Field_10|Field_11|Field_12|Field_13|Field_14|Field_15|Field_16|Field_17|Field_18|Field_19|Field_20|Field_21|Field_22|Field_23|Field_24|Field_25|Field_26  ;27 columns



Loop, %Line0%
{   
	StringSplit, Array, Line%A_Index%, `,
	LV_Add("",Array1,Array2,Array3,Array4,Array5,Array6,Array7,Array8,Array9,Array10,Array11,Arra12,Array13,Array14,Array15,Array16,Array17,Array18,Array19,Array20,Array21,Array22,Array23,Array24,Array25,Array26,Array27,A_Index)
	
}


Gui, Show, h500 w1200

return

GuiClose:

ExitApp
You're talking about 130,000 rows, and it's going to be 28 Columns. Searching efficiently on such a large text file would be very difficult. How many Mb in file size? Would you consider using SQLite? I can help with db in SQLite. Can you send me a sample for a few rows?(Data2.txt to understand what the file looks like)
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Search in listview gui  Topic is solved

Post by colt » 08 Feb 2021, 14:11

I agree with @hasantr, using pure ahk will be slow. You can speed up this code by turning off redraw of the listview until all data is added to it. I also added a loop break in the event that the search term is updated while it is trying to populate the listview. As you can see by the tooltip the time is not spent searching the db but rather populating the listview. The more search results you have the longer it will take to display.

Code: Select all

#SingleInstance force
#NoEnv
SendMode Input
SetWorkingDir, %A_ScriptDir% 
SetBatchLines, -1

global newSearchString
global gListView1

fileRead ,rawData,*t Data2.txt 

if(debug := true)
{
	tooltip,building test db
	rawData := ""
	strings := ["cat","dog","pig","duck","chicken","bush","tree","whale","shark"]
	loop 130000
	{	
		rowNum := A_index
		rowText := ""
		loop % strings._maxIndex()	
		{
			colNum := A_index
			random,num,1,% strings._maxIndex()		
			rowText .= rowNum . "-" . colNum . ":" . strings[num] . ","
		}
		rowText := substr(rowText ,1,strlen(rowText )-1)
		rawData .= rowText . "`n"
	}
	rawData := substr(rawData,1,strlen(rawData)-1)
}
masterDataArr := strSplit(rawData,"`n") ;core array

Gui, Add, Text,,Search:
Gui, Add, Edit, w400 vA_SearchTerm gSearch
Gui, Add, ListView, vgListView1 grid x5 y+5 r5 w1190 h450 -LV0x10, Barcode|Field_1|Field_2|Field_3
populateTable(masterDataArr) 
Gui, Show, h500 w1200
return

populateTable(data)
{
	GuiControl, -Redraw, gListView1
	LV_Delete()
	newSearchString := false
	for lineNum,content in data
	{	
		if(newSearchString) ;break because user has typed new letter changing search
		{			
			return
		}	
		lineArr := strSplit(content,",")
		LV_Add("",lineArr[1],lineArr[2],lineArr[2],lineArr[4])
	}	
	GuiControl, +Redraw, gListView1	
	tooltip populated table
}

GuiClose:
	ExitApp
return

Search:	
	newSearchString := true
	setTimer, applySearch, -20	
return

applySearch:
	gui Submit,noHide
	if(A_searchTerm)
	{
		searchResult:=[]
		for lineNum,content in masterDataArr 
		{
			if(instr(content,A_SearchTerm ))
			{				
				searchResult.push(content)
			}
		}
		tooltip % searchResult.count() . " hit results found. Populating table"
		populateTable(searchResult)
	}
	else
	{
		populateTable(masterDataArr) 
	}
return
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Search in listview gui

Post by hasantr » 08 Feb 2021, 14:45

The size of a 130,000 * 28 table can be very large. When all of them are transferred to RAM at once, the application grows too large and the system becomes unstable. For Listview, so many lines are also a huge burden. 130,000 rows of list views are meaningless to the human eye. The option to view all rows is incorrect. It is still good to show only 50 lines when there are thousands of results. It tells you that it is necessary to construct this search better.
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Search in listview gui

Post by hasantr » 08 Feb 2021, 14:57

https://www.autohotkey.com/boards/viewtopic.php?f=6&t=85394

There is such a method. But I don't know how it works in a very large file.
Post Reply

Return to “Ask for Help (v1)”