Jump to content

Find items in Listview using _GUICtrlListView_ Find InText function but not getting the desired results


Recommended Posts

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $GUI, $Input_Name, $Button_Search, $ListView_Name, $search = -1

$GUI = GUICreate("Find the student's name", 326, 432, 192, 124)
$Input_Name = GUICtrlCreateInput("", 8, 8, 145, 21)
$Button_Search = GUICtrlCreateButton("Search", 160, 0, 75, 33)
$ListView_Name = GUICtrlCreateListView("Name|Year of Birth", 0, 72, 322, 358)
GUICtrlCreateListViewItem("Tuan|2000", $ListView_Name)
GUICtrlCreateListViewItem("Anh|1999", $ListView_Name)
GUICtrlCreateListViewItem("Ha|2000", $ListView_Name)
GUICtrlCreateListViewItem("Han|2000", $ListView_Name)
GUICtrlCreateListViewItem("Trinh|2000", $ListView_Name)
GUICtrlCreateListViewItem("Nhan|2000", $ListView_Name)
GUICtrlCreateListViewItem("Vuong|2000", $ListView_Name)
GUICtrlCreateListViewItem("Sinh|2001", $ListView_Name)
GUICtrlCreateListViewItem("Ngan|2000", $ListView_Name)

GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 200)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button_Search
            _SearchName()

    EndSwitch
WEnd

Func _SearchName()
If GUICtrlRead($Input_Name) <>  '' Then
$Search = _GUICtrlListView_FindInText($ListView_Name, GUICtrlRead($Input_Name), $Search)
_GUICtrlListView_DeleteAllItems($ListView_Name)
$txt = _GUICtrlListView_SetItemSelected($ListView_Name,$Search)
GUICtrlCreateListViewItem($txt[0]&'|'& $txt[1], $ListView_Name)
_GUICtrlListView_EnsureVisible($ListView_Name, $Search)
EndIf
EndFunc

I am looking to follow the student's name for example when looking for a letter A it will show the students with the letter A and the student's birth year but what I do is unsuccessful. I can only find when clicking on the search are displayed and jumped to it but don't show the same time the A letter is. Who helps me

Link to post
Share on other sites

??

Try with this:

Func _SearchName()
    If GUICtrlRead($Input_Name) <> '' Then
        _GUICtrlListView_SetItemSelected($ListView_Name, -1, False)
        $search = _GUICtrlListView_FindInText($ListView_Name, GUICtrlRead($Input_Name))
        If $search <> -1 Then
            _GUICtrlListView_ClickItem($ListView_Name, $search, "left", False, 1)
            _GUICtrlListView_EnsureVisible($ListView_Name, $search)
        EndIf
    EndIf
EndFunc   ;==>_SearchName

 

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites
4 hours ago, Chimp said:

??

It is also not entirely clear to me, what @Loc is trying to achieve ;).

@Loc :

(BTW : I am not yet using the latest beta versions of AutoIt, so the outcomes may differ)

I suppose you want to enter e.g. the letter A in the search field and expect a ListView of all names starting with A as a result (and date of birth), is that correct ?

1.) Your function _SearchName() can't even be compiled without errors ( @Chimp's does).

[...]
        $txt = _GUICtrlListView_SetItemSelected($ListView_Name, $search)
        GUICtrlCreateListViewItem($txt[0] & '|' & $txt[1], $ListView_Name)
        [...]

_GUICtrlListView_SetItemSelected sets whether the item is selected and returns True or False, not an array.

Instead, you assign the result to the variable $txt and use it like an array (... $txt[0] & '|' & $txt[1] ...). This leads to the error : "==> Subscript used on non-accessible variable".

2.) $search = _GUICtrlListView_FindInText($ListView_Name, GUICtrlRead($Input_Name), $search)

According to the help, the search is case insensitive. It seaches for an item that contains the specified text (here letter A or a) anywhere in its text. So if you search for A or a, it will always return Index=0 (in @Chimp 's version), because the first listview entry (here "Tuan") includes an "a".

 

Perhaps you could describe the desired workflow more precisely.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites

_GUICtrlListView_ Find InText function. For example, if I search for the letter A, I click the search button, the listview will find the items with the letter A but it takes 1 turn. Instead I want to display all the results for just one search, so I use the _GUICtrlListView_DeleteAllItems function to delete the old item and display the items with the letter A, so I use the guictrlcreateitem function.

Link to post
Share on other sites
5 hours ago, Loc said:

Can anyone help me, I've been doing it for a few days and it still doesn't work

I'm still not quite sure what your goal is. Here is an approach where the listview is controlled via arrays.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>

Global $hGUI, $sInput_Name, $idButton_Search, $idListView_Name, $iCaseSensitive = 0
Global $aData[9][2] = [["Tuan","2000"],["Anh","1999"],["Ha","2000"],["Han","2000"],["Trinh","2000"], _
                       ["Nhan","2000"],["Vuong","2000"],["Sinh","2001"],["Ngan","2000"]]

$hGUI            = GUICreate("Find the student's name", 326, 482, 192, 124)
$idInput_Name    = GUICtrlCreateInput("", 8, 8, 145, 21)
$idButton_Search = GUICtrlCreateButton("Search", 160, 0, 75, 33)
$idButton_Reset  = GUICtrlCreateButton("Reset", 245, 0, 75, 33)
$idCheckbox_Case = GUICtrlCreateCheckbox("Case sensitive ", 8, 48, 150, 21)

$idLabel_Info    = GUICtrlCreateLabel("Info : ", 8, 74, 150, 21)
$idListView_Name = GUICtrlCreateListView("Name|Year of Birth", 0, 102, 322, 358)
For $i = 0 To UBound($aData) - 1
    GUICtrlCreateListViewItem($aData[$i][0] & "|" & $aData[$i][1], $idListView_Name)
Next

GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 200)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idButton_Search
            _SearchName()
        Case $idButton_Reset
            _ResetData()

        Case $idCheckbox_Case
            If _IsChecked($idCheckbox_Case) Then
                $iCaseSensitive = 1
            Else
                $iCaseSensitive = 0
            EndIf
    EndSwitch
WEnd

Func _SearchName()
    Local $aMatches
    GUICtrlSetData($idLabel_Info, "Info : ")

    If GUICtrlRead($idInput_Name) <> '' Then
        $aMatches = _ArrayFindAll($aData, GUICtrlRead($idInput_Name), Default, Default, $iCaseSensitive, 1)
        If Not @error Then
            GUICtrlSetData($idLabel_Info, "Info : match(es) found")
            _GUICtrlListView_DeleteAllItems($idListView_Name)
            For $i = 0 To UBound($aMatches) - 1
                GUICtrlCreateListViewItem($aData[$aMatches[$i]][0] & "|" & $aData[$aMatches[$i]][1], $idListView_Name)
            Next
        Else
            GUICtrlSetData($idLabel_Info, "Info : no match found")
        EndIf
    EndIf
EndFunc   ;==>_SearchName

Func _ResetData()
    _GUICtrlListView_DeleteAllItems($idListView_Name)
    GUICtrlSetData($idLabel_Info, "Info : ")
    For $i = 0 To UBound($aData) - 1
        GUICtrlCreateListViewItem($aData[$i][0] & "|" & $aData[$i][1], $idListView_Name)
    Next
EndFunc   ;==>_ResetData

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites

Thanks for helping me. Instead of looking in the $data[9][2] array, I will look for the item on the listview because the name I need to find it can't be put in the array but manually add or remove and save it to the file.

Link to post
Share on other sites

I put the data directly as an array ("$data[9][2]") in the source code, only for demo purposes. In a real application you can read and save the array e.g. from/to a CSV file. If you need to work with masses of data, then SQLite might be an option.

5 hours ago, Loc said:

I will look for the item on the listview because the name I need to find it can't be put in the array but manually add or remove and save it to the file.

Up to now, you only wanted to search by name, and display the matches in the listview.

If you also want to add or remove data via the listview, then the current approach is not sufficient.

Take a look at e.g. csv-file-editor from @pixelsearch and/or guilistviewex from @Melba23. @LarsJ has also published many brilliant scripts regarding listviews.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...