Jump to content

create menu item based on search results


Go to solution Solved by l3ill,

Recommended Posts

when i search for files i would like each file found to be a submenu item of existing menu and for it to be run when clicked

any way to do it?

i was going with this function but i cannot get a good result with random number of files found

Func search1()
    $searchtext = InputBox("Tekst", "Upisati traženi tekst:" & @CRLF & "(biti ce pretražena mapa" & " " & @ScriptDir & "\Izdane kartice\)", "", " M", 500, 140)
    If @error = 1 Then
        Sleep(100)
    Else
        $aArray = _RecFileListToArray($folder, "*" & $searchtext & "*.txt", 1)
        $nadenifile = _ArrayToString($aArray, "", 1)
        $beztxt = StringReplace($nadenifile, ".txt", ".")
        $found = StringSplit($beztxt, ".")
        $MenuItem10 = GUICtrlCreateMenu("<---->", -1, 2)
        $MenuItem11 = GUICtrlCreateMenu("&Pronadene datoteke", -1, 3)

        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem12 = GUICtrlCreateMenuItem($found[1], $MenuItem11)
        EndIf
        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem13 = GUICtrlCreateMenuItem($found[2], $MenuItem11)
        EndIf
        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem14 = GUICtrlCreateMenuItem($found[3], $MenuItem11)
        EndIf
        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem15 = GUICtrlCreateMenuItem($found[4], $MenuItem11)
        EndIf
        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem16 = GUICtrlCreateMenuItem($found[5], $MenuItem11)
        EndIf
        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem17 = GUICtrlCreateMenuItem($found[6], $MenuItem11)
        EndIf
        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem18 = GUICtrlCreateMenuItem($found[7], $MenuItem11)
        EndIf
        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem19 = GUICtrlCreateMenuItem($found[8], $MenuItem11)
        EndIf
        If Not IsDeclared("found") Then
            Sleep(10)
        Else
            $MenuItem20 = GUICtrlCreateMenuItem($found[9], $MenuItem11)
        EndIf
;~  _ArrayDisplay($aArray, ".TXT Files")
    EndIf
EndFunc   ;==>search1

thank you melba for _RecFileListToArray

Link to comment
Share on other sites

  • Solution

Hi dickjones007,

  you can use ubound to get the number of file returned by the array.

Bill

example:

$iJ = UBound($aArray, 1)

For $i = 1 To $iJ ;Loop
              "Bulid menu 1 item at a time"
       Next
Edited by billo
Link to comment
Share on other sites

when i put this

For $i = 1 To $iJ - 1 ;Loop
            $nadjeno = GUICtrlCreateMenuItem($found[$i], $MenuItem11)
            GUICtrlSetOnEvent($nadjeno, ShellExecute($folder & $found[$i] & ".txt", "", @ScriptDir, "edit"))
        Next

it opens all found files. and i only want them assigned to each menu item so the file is opened when menu item is clicked.

help?

Link to comment
Share on other sites

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Tornado.ico
#AutoIt3Wrapper_Outfile=Twister kartice.exe
#AutoIt3Wrapper_Res_Description=Vodenje twister mjesecnih, polugodišnjih i godišnjih kartica.
#AutoIt3Wrapper_Res_Fileversion=1.0.0.3
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=Darko Majetic
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <Array.au3>
#include <File.au3>
#include <GuiMenu.au3>
#include "RecFileListToArray.au3"

Global $folder = @ScriptDir & "\Izdane kartice\", $folderslike = @ScriptDir & "\Slike\"
Local $ver = FileGetVersion("Twister kartice.exe")
Local $workingdir = @WorkingDir ; save
Opt("GUIOnEventMode", 1)
Opt("TrayIconHide", 1)
Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.
Local $exititem = TrayCreateItem("Izlaz")
Dim $zivotinja[46] = ["Alligator", "Ant", "Bat", "Bear", "Bee", "Bird", "Bull", "Bulldog", "Butterfly", "Cat", "Chicken", "Cow", "Crab", "Crocodile", "Deer", "Dog", "Donkey", "Duck", "Eagle", "Elephant", "Fish", "Fox", "Frog", "Giraffe", "Gorilla", "Hippo", "Horse", "Insect", "Lion", "Monkey", "Moose", "Mouse", "Owl", "Panda", "Penguin", "Pig", "Rabbit", "Rhino", "Rooster", "Shark", "Sheep", "Snake", "Tiger", "Turkey", "Turtle", "Wolf"]
$imeziv = $zivotinja[Random(0, UBound($zivotinja) - 1, 1)]
TraySetIcon(@ScriptDir & "\Ikone\" & $imeziv & ".ico")
TraySetToolTip("Twister kartice" & @CRLF & "Twisted" & " " & $imeziv)

#region --- CodeWizard generated code Start ---
;SpashImage features: Title=No, Width=900, Height=400, Always On Top
SplashImageOn("", $folderslike & "twisterfun.jpg", "900", "400", "-1", "-1", 1)
Sleep(200)
SplashOff()
#endregion --- CodeWizard generated code Start ---

#region ### GUI section ###
#region MENU
$Form1_1 = GUICreate("Twister kartice" & " " & $ver, 624, 450, @DesktopWidth / 2 - 624 / 2, @DesktopHeight / 2 - 450 / 2, $WS_BORDER)
$MenuItem1 = GUICtrlCreateMenu("&Izbornik")
$MenuItem4 = GUICtrlCreateMenuItem("&Novi unos", $MenuItem1)
GUICtrlSetOnEvent($MenuItem4, "svenovo1")
$MenuItem9 = GUICtrlCreateMenuItem("&Izdane kartice", $MenuItem1)
GUICtrlSetOnEvent($MenuItem9, "izdane1")
$MenuItem6 = GUICtrlCreateMenu("&Traži")
$MenuItem8 = GUICtrlCreateMenuItem("&Traži karticu", $MenuItem6)
GUICtrlSetOnEvent($MenuItem8, "search1")
$MenuItem7 = GUICtrlCreateMenu("&O programu")
$MenuItem2 = GUICtrlCreateMenuItem("&Autor", $MenuItem7)
GUICtrlSetOnEvent($MenuItem2, "oprogramu1")
$MenuItem3 = GUICtrlCreateMenuItem("---------------", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("I&zlaz (Ctrl+Shift+x)", $MenuItem1)
GUICtrlSetOnEvent($MenuItem5, "izlaz1")
Local $_AccelTable[1][2] = [["+^x", $MenuItem5]]
GUISetAccelerators($_AccelTable)
#endregion MENU
$default = GUICtrlCreateButton("DEFAULT", 300, 0, 60, 15)
GUICtrlSetOnEvent($default, "default1")
$djelatna = GUICtrlCreateLabel("Djelatnik:", 20, 17, 60, 15)
$djelatnici = GUICtrlCreateCombo("", 100, 13, 119, 25)
GUICtrlSetData($djelatnici, "Anita Tihi|Nikolina Matanovic", "Anita Tihi")
$imeprezime = GUICtrlCreateInput("", 100, 40, 120, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_UPPERCASE))
GUICtrlCreateLabel("Ime i prezime:", 20, 43, 80, 15)
#region KONTAKT INFO
$kontakt = GUICtrlCreateCombo("", 320, 40, 80, 21)
GUICtrlSetData($kontakt, "Majka|Otac|Djed|Baka|Brat|Sestra|Prijatelj|Ocuh|Pomajka|Stric|Strina|Ujak|Ujna|Bratic|Sestricna", "Majka")
GUICtrlCreateLabel("Kontakt osoba:", 243, 43, 77, 15)
$kontaktime = GUICtrlCreateInput("", 460, 40, 120, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_UPPERCASE))
GUICtrlCreateLabel("Ime:", 410, 43, 40, 15)
$kontaktel = GUICtrlCreateInput("", 460, 66, 120, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_UPPERCASE))
GUICtrlCreateLabel("Telefon:", 410, 69, 40, 15)
$dodaj = GUICtrlCreateButton("&Dodaj kontakt", 243, 90, 80, 25)
GUICtrlSetOnEvent($dodaj, "dodaj1")
$listakontakt = GUICtrlCreateEdit("", 329, 90, 252, 65, BitOR($ES_READONLY, $WS_VSCROLL))
$obrisi = GUICtrlCreateButton("Ob&riši kontakte", 243, 130, 80, 25)
GUICtrlSetOnEvent($obrisi, "obrisi1")
#endregion KONTAKT INFO
$datumrod = GUICtrlCreateDate("2001/01/01", 100, 66, 120, 20, $DTS_SHORTDATEFORMAT)
GUICtrlCreateLabel("Datum rodenja:", 20, 69, 80, 15)
$adresa = GUICtrlCreateInput("", 100, 91, 120, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_UPPERCASE))
GUICtrlCreateLabel("Ulica i broj:", 20, 94, 80, 15)
$posta = GUICtrlCreateInput("", 100, 117, 120, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER, $ES_OEMCONVERT))
GUICtrlSetLimit($posta, 5, 5)
GUICtrlCreateLabel("Broj pošte:", 20, 120, 80, 15)
$grad = GUICtrlCreateInput("", 100, 143, 120, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_UPPERCASE))
GUICtrlCreateLabel("Grad:", 20, 146, 80, 15)
$vrsta = GUICtrlCreateCombo("", 20, 210, 200, 25)
GUICtrlSetData($vrsta, "Mjesecna|Polugodisnja|Godisnja", "")
GUICtrlSetOnEvent($vrsta, "kartica1")
$brojkartlabel = GUICtrlCreateLabel("Broj kartice", 20, 240, 200, 15)
$brojkart = GUICtrlCreateInput("", 20, 260, 60, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
GUICtrlSetLimit($brojkart, 3, 3)
$datum = GUICtrlCreateDate(_NowDate(), 20, 310, 200, 20, $DTS_SHORTDATEFORMAT)
$datumizd = GUICtrlCreateLabel("Datum izdavanja kartice", 20, 290, 118, 15)
$vrijedido = GUICtrlCreateLabel("Vrijedi do:", 20, 340, 50, 17)
$vrijedidoread = GUICtrlCreateInput("", 70, 337, 150, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY))
$vrstakar = GUICtrlCreateLabel("Vrsta kartice", 20, 190, 200, 15)
$spremi = GUICtrlCreateButton("&Spremi", 20, 368, 75, 25)
GUICtrlSetOnEvent($spremi, "spremi1")
GUISetState()
#endregion ### GUI section ###

; *** create these here and not over and over in a function
$pic = GUICtrlCreatePic($folderslike & "backg.jpg", 240, 14 + 150, 364, 228)

While 1
    Local $msg = TrayGetMsg()
    If $msg = $exititem Then ExitLoop
WEnd

Func svenovo1() ;put empty values into fields
    GUICtrlSetData($imeprezime, "") ;ime prezime
    GUICtrlSetData($kontaktime, "") ;ime kontakta
    GUICtrlSetData($kontaktel, "") ;telefon
    GUICtrlSetData($adresa, "") ;ulicu
    GUICtrlSetData($posta, "") ;postu
    GUICtrlSetData($grad, "") ;grad
    GUICtrlSetData($vrsta, "Mjesecna") ;vrsta kartice
    GUICtrlSetData($brojkart, "") ;broj kartice
    GUICtrlSetData($listakontakt, "") ;lista kontakata
    GUICtrlSetImage($pic, $folderslike & "backg.jpg")
EndFunc   ;==>svenovo1

Func default1() ;put default values into fields
    Dim $aWord[10] = ["Kruno", "Ilija", "Marko", "Filip", "Ivan", "Željka", "Marica", "Matilda", "Ana", "Anita"]
    $ime = $aWord[Random(0, UBound($aWord) - 1, 1)]
    Dim $bWord[10] = ["Majetic", "Krpic", "Markic", "Filipic", "Ivanovic", "Martic", "Klovic", "Zulic", "Ludic", "Varnic"]
    $prezime = $bWord[Random(0, UBound($bWord) - 1, 1)]
    GUICtrlSetData($imeprezime, $ime & " " & $prezime) ;ime prezime
    Dim $cWord[10] = ["Marica", "Barica", "Ivica", "Karlo", "Pera", "Marijana", "Klara", "Maja", "Verica", "Ðuka"]
    $kontime = $cWord[Random(0, UBound($cWord) - 1, 1)]
    GUICtrlSetData($kontaktime, $kontime) ;ime kontakta
    GUICtrlSetData($kontaktel, "035/212-055") ;telefon
    GUICtrlSetData($adresa, "HVARSKA 55") ;ulicu
    GUICtrlSetData($posta, "35000") ;postu
    GUICtrlSetData($grad, "SLAVONSKI BROD") ;grad
    GUICtrlSetData($vrsta, "Polugodisnja") ;vrsta kartice
    GUICtrlSetData($brojkart, StringFormat("%03u", Random(01, 100, 1))) ;broj kartice
EndFunc   ;==>default1

Func kartica1()
    Switch GUICtrlRead($vrsta)
        Case "Mjesecna"
            GUICtrlSetImage($pic, $folderslike & "mjesecna.jpg")
            $dodandatum = _DateAdd('M', 1, _NowCalcDate())
            $konvdatum = StringSplit($dodandatum, "/")
            $novidatum = Number($konvdatum[3]) & "." & Number($konvdatum[2]) & "." & StringRight($konvdatum[1], 4)

        Case "Polugodisnja"
            GUICtrlSetImage($pic, $folderslike & "polugodisnja.jpg")
            $dodandatum = _DateAdd('M', 6, _NowCalcDate())
            $konvdatum = StringSplit($dodandatum, "/")
            $novidatum = Number($konvdatum[3]) & "." & Number($konvdatum[2]) & "." & StringRight($konvdatum[1], 4)

        Case "Godisnja"
            GUICtrlSetImage($pic, $folderslike & "godisnja.jpg")
            $dodandatum = _DateAdd('M', 12, _NowCalcDate())
            $konvdatum = StringSplit($dodandatum, "/")
            $novidatum = Number($konvdatum[3]) & "." & Number($konvdatum[2]) & "." & StringRight($konvdatum[1], 4)
    EndSwitch
    GUICtrlSetData($vrijedidoread, $novidatum)
EndFunc   ;==>kartica1

Func dodaj1()
    If GUICtrlRead($listakontakt) = "" Then
        GUICtrlSetData($listakontakt, GUICtrlRead($kontakt) & @TAB & GUICtrlRead($kontaktime) & @TAB & GUICtrlRead($kontaktel), 1)
    Else
        GUICtrlSetData($listakontakt, @CRLF & GUICtrlRead($kontakt) & @TAB & GUICtrlRead($kontaktime) & @TAB & GUICtrlRead($kontaktel), 1)
    EndIf
EndFunc   ;==>dodaj1

Func obrisi1()
    If GUICtrlRead($listakontakt) = "" Then
        MsgBox(64, "Brisanje kontakt brojeva", "Nema dodanih kontakt brojeva")
    Else
        $return = MsgBox(4 + 32, "Brisanje kontakt brojeva", "Da li sigurno želite obrisati SVE kontakt brojeve?")
        If $return = 6 Then
            GUICtrlSetData($listakontakt, "")
        Else
            Sleep(100)
        EndIf
    EndIf
EndFunc   ;==>obrisi1

Func spremi1()

    If FileExists($folder) Then
        Sleep(100)
    Else
        DirCreate($folder)
    EndIf

    $imeprezimetxt = GUICtrlRead($imeprezime)
    $datumrodtxt = GUICtrlRead($datumrod)
;~  $kontakttxt = ControlGetText("Twister kartice", "", "[CLASS:Edit; INSTANCE:3]")
;~  $kontaktimetxt = GUICtrlRead($kontaktime)
    $kontakteltxt = GUICtrlRead($kontaktel)
    $adresatxt = GUICtrlRead($adresa)
    $postatxt = GUICtrlRead($posta)
    $gradtxt = GUICtrlRead($grad)
    $brojkarttxt = StringFormat("%03d", GUICtrlRead($brojkart))
    $datumizdtxt = GUICtrlRead($datum)
    $djelatnicitxt = GUICtrlRead($djelatnici)
    $vrstatxt = ControlGetText("Twister kartice", "", "[CLASS:Edit; INSTANCE:10]")
;~  $vrstatxt = GUICtrlRead($vrstakart)
    $vrijedidotxt = GUICtrlRead($vrijedidoread)
    $listakontakttxt = GUICtrlRead($listakontakt)
    Local $avArray[10][3] = [["IME I PREZIME", $imeprezimetxt, ""],["DATUM ROÐENJA", $datumrodtxt, ""],["KONTAKT OSOBA", @CRLF & $listakontakttxt, ""],["", "", ""],["ULICA I BROJ", $adresatxt, ""],["GRAD/MJESTO", $postatxt, $gradtxt],["BROJ KARTICE", $brojkarttxt, $vrstatxt],["KARTICA IZDANA", $datumizdtxt, ""],["VRIJEDI DO", $vrijedidotxt, ""],["DJELATNICA", $djelatnicitxt, ""]]
;~  _ArrayDisplay($avArray, "Podatci o korisniku kartice")

    If GUICtrlRead($listakontakt) = "" Then
        $nemakontakt = MsgBox(4 + 64, "Spremanje podataka", "Nema dodanih kontakt brojeva." & @CRLF & "Da li sigurno želite nastaviti sa spremanjem?")
        If $nemakontakt = 6 Then
            $sFile = FileSaveDialog("Izaberite ime.", $folder, "Text files (*.txt)", 16, $vrstatxt & "_" & $brojkarttxt & "_" & $imeprezimetxt & ".txt")
            If @error Then
                MsgBox(4096, "", "Spremanje otkazano.")
            Else
                _FileWriteFromArray($sFile, $avArray, 0, "", "      ")
                FileChangeDir($workingdir)
                svenovo1()
            EndIf
        Else
            Sleep(100)
        EndIf
    Else
        $sFile = FileSaveDialog("Izaberite ime.", $folder, "Text files (*.txt)", 16, $vrstatxt & "_" & $brojkarttxt & "_" & $imeprezimetxt & ".txt")
        If @error Then
            MsgBox(4096, "", "Spremanje otkazano.")
        Else
            _FileWriteFromArray($sFile, $avArray, 0, "", "      ")
            FileChangeDir($workingdir)
            svenovo1()
        EndIf
    EndIf

EndFunc   ;==>spremi1

Func izdane1()
    If FileExists($folder) Then
        Sleep(100)
    Else
        DirCreate($folder)
    EndIf
    Run("Explorer.exe " & $folder)
EndFunc   ;==>izdane1

Func oprogramu1()

    MsgBox(0, "Autor", "Darko Majetic" & @CRLF & "darkman@doctor.com")
EndFunc   ;==>oprogramu1

Func search1()
    $searchtext = InputBox("Tekst", "Upisati traženi tekst:" & @CRLF & "(biti ce pretražena mapa" & " " & @ScriptDir & "\Izdane kartice\)", "", " M", 500, 140)
    If @error = 1 Then
        Sleep(100)
    Else
        $aArray = _RecFileListToArray($folder, "*" & $searchtext & "*.txt", 1)
        $nadenifile = _ArrayToString($aArray, "", 1)
        $beztxt = StringReplace($nadenifile, ".txt", ".")
        $found = StringSplit($beztxt, ".")
        $MenuItem10 = GUICtrlCreateMenu("<---->", -1, 2)
        $MenuItem11 = GUICtrlCreateMenu("&Pronadene datoteke", -1, 3)
        $iJ = UBound($aArray, 1)
        For $i = 1 To $iJ - 1 ;Loop
            $nadjeno = GUICtrlCreateMenuItem($found[$i], $MenuItem11)
;~          GUICtrlSetOnEvent($nadjeno, ShellExecute($folder & $found[$i] & ".txt", "", @ScriptDir, "edit"))
        Next
;~  _ArrayDisplay($aArray, ".TXT Files")
    EndIf
EndFunc   ;==>search1

Func izlaz1()
    Exit
EndFunc   ;==>izlaz1

its the function near the end of script.

do i need to elaborate on the problem more?

Link to comment
Share on other sites

Interesting...  You want to build a Main Menu Item (like: File, Edit, View) by item, with a folder search and then you want them to be executable and then clear them to make a new list?

Sorry, cant help you... My GUI skills stop at making the list. To execute them you would have to build the switch case or similar to ShellExecute the found items when clicked. Maybe there is a workaround I don't know about.

I personally would go at it differently anyway. Maybe the GUI gurus will have a look when they get a chance.

Bill

Link to comment
Share on other sites

Add the full path of the text files to the menu and then read the text of the control clicked to ShellExecute the text that was read. The _Menu function will open the test files when clicked in the menu. Use File -> Exit Program to exit the test.

Opt("GUIOnEventMode", 1)

GUICreate('')

$menu_main = GUICtrlCreateMenu('File')

GUICtrlCreateMenuItem('Add Entries', $menu_main)
GUICtrlSetOnEvent(Default, '_Add')

GUICtrlCreateMenuItem('Remove Entries', $menu_main)
GUICtrlSetOnEvent(Default, '_Remove')

GUICtrlCreateMenuItem('Exit Program', $menu_main)
GUICtrlSetOnEvent(Default, '_Exit')

Global $menu_edit
_add()

GUISetState()

While 1
    Sleep(100)
WEnd

Func _Add()
    Local $file[2] = [@ScriptDir & '\test1.txt', @ScriptDir & '\test2.txt']
    ; delete the ctrl if assigned a CtrlID
    If $menu_edit Then
        GUICtrlDelete($menu_edit)
        $menu_edit = 0
    EndIf
    ; create edit ctrl and entries
    $menu_edit = GUICtrlCreateMenu('Edit')
    For $1 In $file
        ; create test files
        If Not FileExists($1) Then FileWrite($1, $1 & @CRLF)
        ; create menu items of test files
        GUICtrlCreateMenuItem($1, $menu_edit)
        GUICtrlSetOnEvent(Default, '_Menu')
    Next
EndFunc

Func _Menu()
    ; run the entry
    ShellExecute('"' & GUICtrlRead(@GUI_CtrlId, 1) & '"')
    ; delete the entry
    GUICtrlDelete(@GUI_CtrlId)
EndFunc

Func _Remove()
    ; remove edit menu ctrl and then recreate it
    GUICtrlDelete($menu_edit)
    $menu_edit = GUICtrlCreateMenu('Edit')
EndFunc

Func _Exit()
    Exit
EndFunc

The example does some control adding and deleting as you were inquiring about. Perhaps will inspire you to do something similar to your script. :)

Edit: Put quotes in ShellExecute function parameter

Edited by MHz
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...