Jump to content

Search box for names


Recommended Posts

Hi guys

I have this script, that searches for all txt files in a given Dir, and shows the content (names) in a row for each txt file.

Would it be possible to add a search box at the bottom, where you can search for one of the names, and then it would highlight that name with a color in the rows.
Same name can be present multiple times in different rows.

Where do I begin? Thanks! :-)

 

 

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>

Global $g_hListView

Call("ListNames")

Func ListNames()
    Local $hGUI, $hImage
    $hGUI = GUICreate("Listview", 500, 400)
    Local $sDirPath = "C:\Temp"
    $g_hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 494, 368)
    _GUICtrlListView_SetExtendedListViewStyle($g_hListView, $LVS_EX_GRIDLINES)
    GUISetState(@SW_SHOW)

    Local $aFiles = _FileListToArray($sDirPath, "*.txt")
;~  _ArrayDisplay($aFiles)

    Local $n
    For $i = 1 To $aFiles[0]
         $tmp = _FileCountLines($sDirPath & "\" & $aFiles[$i])
         $n = ($n<$tmp) ? $tmp : $n
    Next
    For $i = 0 To $n
         _GUICtrlListView_AddItem($g_hListView, "")
   Next

    Local $aString = 0
    For $i = 1 To $aFiles[0]
         _GUICtrlListView_AddColumn($g_hListView, StringTrimRight($aFiles[$i], 4), 100)
          $aString = FileReadToArray($sDirPath & "\" & $aFiles[$i])
          If $i = 1 Then
                For $x = 0 To UBound($aString) - 1
                    _GUICtrlListView_SetItemText($g_hListView, $x, $aString[$x])
                Next
        Else
                 For $x = 0 To UBound($aString) - 1
                        _GUICtrlListView_AddSubItem($g_hListView, $x, $aString[$x], $i - 1)
                Next
        EndIf
    Next


    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>ListNames

 

Edited by david1337
Link to comment
Share on other sites

Hi JohnOne

Thanks for your answer. That got me started.
I now have the search bar at the bottom, as I wanted.
How can I make it highlight the name I write, with a given color?
Is it required that I have an "OK" button or something, to start the actual search?

 

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>

Global $g_hListView

Call("ListNames")

Func ListNames()



    Local $hGUI, $hImage
    $hGUI = GUICreate("Listview", 500, 400)


    ;Places the search bar
    $hcInput = GUICtrlCreateInput("", 150, 375, 200, 20)
    $sSearchFor = GuiCtrlRead($hcInput)
    ;___________________________________________________


    Local $sDirPath = "C:\temp"
    $g_hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 494, 368)
    _GUICtrlListView_SetExtendedListViewStyle($g_hListView, $LVS_EX_GRIDLINES)
    GUISetState(@SW_SHOW)

    Local $aFiles = _FileListToArray($sDirPath, "*.txt")





    GUISetState(@SW_SHOW)

    Local $n
    For $i = 1 To $aFiles[0]
         $tmp = _FileCountLines($sDirPath & "\" & $aFiles[$i])
         $n = ($n<$tmp) ? $tmp : $n
    Next
    For $i = 0 To $n
         _GUICtrlListView_AddItem($g_hListView, "")
   Next

    Local $aString = 0
    For $i = 1 To $aFiles[0]
         _GUICtrlListView_AddColumn($g_hListView, StringTrimRight($aFiles[$i], 4), 100)
          $aString = FileReadToArray($sDirPath & "\" & $aFiles[$i])
          If $i = 1 Then
                For $x = 0 To UBound($aString) - 1
                    _GUICtrlListView_SetItemText($g_hListView, $x, $aString[$x])
                Next
        Else
                 For $x = 0 To UBound($aString) - 1
                        _GUICtrlListView_AddSubItem($g_hListView, $x, $aString[$x], $i - 1)
                Next
        EndIf
    Next


    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>ListNames
Edited by david1337
Link to comment
Share on other sites

Yes you will need a button, make it the default button so pressing enter on input will trigger the same function the button does.

This is basic stuff, I'm surprised after 3 years here you don't know how.

Changing the individual row colour of a listview control is a little more convoluted, but there are examples around the forum you can search.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

JohnOne

Don't get me wrong, I do know how to make the button. My question was whether it was a MUST, or if the search could work while typing the name.
Say I wrote "J", then it highlighted J with yellow. Then "Jo", and only Jo was highlighted etc.

Thanks for your time, I will search the forum.

Btw: I could have just created my account 3 years ago, to get help with some mouse movement, and then come back 3 years later and ask for help with more advanced stuff ;)

Edited by david1337
Link to comment
Share on other sites

I understand.

I just gathered from your positioning of the functions I offered in your new code that did not now how to use a button and get the text of input box after pressing it. For the record you placed it incorrectly.

You do not absolutely need a button, you are right about that, but once again I try to answer based on the level I deduce from the askee, a button is the simplest way you see.

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I wrote this example.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>

Global $g_hListView, $hGUI, $g_SearchInput

Call("ListNames")

Func ListNames()
    Local $hImage, $btnSearch
    $hGUI = GUICreate("Listview", 500, 420)
    Local $sDirPath = "C:\Test"
    $g_hListView = _GUICtrlListView_Create($hGUI, "", 2, 2, 494, 368, $LVS_REPORT)
    _GUICtrlListView_SetExtendedListViewStyle($g_hListView, $LVS_EX_GRIDLINES)
    GUICtrlCreateLabel("Search For: ", 20, 385)
    $g_SearchInput = GUICtrlCreateInput("", 100, 380, 100, 22)
    $btnSearch = GUICtrlCreateButton("Highlight", 220, 380)
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    Local $aFiles = _FileListToArray($sDirPath, "*.txt")
;~  _ArrayDisplay($aFiles)


    Local $n
    For $i = 1 To $aFiles[0]
        $tmp = _FileCountLines($sDirPath & "\" & $aFiles[$i])
        $n = ($n < $tmp) ? $tmp : $n
    Next
    For $i = 0 To $n
        _GUICtrlListView_AddItem($g_hListView, "")
    Next


    Local $aString = 0
    For $i = 1 To $aFiles[0]
        _GUICtrlListView_AddColumn($g_hListView, StringTrimRight($aFiles[$i], 4), 100)
        $aString = FileReadToArray($sDirPath & "\" & $aFiles[$i])
        If $i = 1 Then
            For $x = 0 To UBound($aString) - 1
                _GUICtrlListView_SetItemText($g_hListView, $x, $aString[$x])
            Next
        Else
            For $x = 0 To UBound($aString) - 1
                _GUICtrlListView_AddSubItem($g_hListView, $x, $aString[$x], $i - 1)
            Next
        EndIf
    Next

    Local $idMsg = 0

    While 1
        $idMsg = GUIGetMsg()
        Select
            Case $idMsg = $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
            Case $idMsg = $btnSearch
                _GUICtrlListView_RedrawItems($g_hListView, 0, _GUICtrlListView_GetItemCount($g_hListView))
        EndSelect
    WEnd

EndFunc   ;==>ListNames

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $g_hListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    Local $iDrawStage = DllStructGetData($tCustDraw, "dwDrawStage")
                    If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW
                    If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW
                    Local $iSubItem = DllStructGetData($tCustDraw, "iSubItem")
                    Local $iItem = DllStructGetData($tCustDraw, "dwItemSpec")
                    Local $iColor = RGB2BGR(0xffffff)
                    If GUICtrlRead($g_SearchInput) <> "" Then
                        If _GUICtrlListView_GetItemText($g_hListView, $iItem, $iSubItem) = GUICtrlRead($g_SearchInput) Then
                            $iColor = RGB2BGR(0x00ff00)
                        EndIf
                    EndIf
                    DllStructSetData($tCustDraw, "clrTextBk", $iColor)
                    Return $CDRF_NEWFONT

            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
    Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc   ;==>RGB2BGR
_WinAPI_SetWindowsHookEx

Saludos

Link to comment
Share on other sites

Danyfirex,

you sure did mate! and NO WAY.. you just created the search button..!
How bad ass are you? :D

So the  GUIRegisterMsg refers to the new func, in which the highlight part, color code etc. is defined. The whole DllStruct part I have clue what does.

Anyway, can't thank you enough!

Saludos!

You're Wellcome.

You could hit the like button on his post.

hahaha nice comment.

 

Ahh is it the like button that raises a users reputation?

Probably.

 

Saludos

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...