Jump to content

Creating actions for dynamic buttons.


Recommended Posts

First of all I'm new to Autoit and it is refreshing to see that there is a community that is willing to help the noobs out with their coding misadventures. My question, I hope it's an easy solve for most of you, is that I created a function that will create some buttons based on a search in a directory for a specific file name. What happens is my buttons get named the same as the file names. What should happen next is if I click on any of the buttons, Notepad runs and opens the specific file based on the button.

Here's the code:

FUNC SearchServer()

$xlocation = 20

$ylocation = 145

$width = 100

$height = 20

$i = 0

FileChangeDir("H:\common")

$search = FileFindFirstFile("*.txt")

While $i <= 10

$filearray[$i] = FileFindNextFile($search)

If @error Then

$i = $i - 1

ExitLoop

Else

$i = $i + 1

EndIf

Wend

$i = 0

While $i <= 10

If $filearray[$i] = "" Then

ExitLoop

Else

If $i = 5 Then

$xlocation = 200

$ylocation = 150

EndIf

$filearray[$i] = StringUpper($filearray[$i])

GuICtrlCreateButton($filearray[$i], $xlocation, $ylocation, $width, $height)

GUICtrlSetOnEvent(-1, "ButtonExe")

$t = FileGetTime($filearray[$i],1)

$date = $t[1] & "/" & $t[2] & "/" & $t[0]

GUICtrlCreateLabel($date,$xlocation + 110,$ylocation,70,20)

EndIf

$ylocation = $ylocation + 25

$i = $i + 1

Wend

The Buttonexe() function doesn't work like it should because once the loop to create the buttons exits and all the buttons get created, $filearray[] gets reset back to 'nothing' and the action of the buttons fail.

Func ButtonExe()

Run(@systemdir & "notepad.exe" & $filearray[$i]")

EndFunc

Can you any of you guys offer any suggestions as to how I can make this work or a better way of doing what I'm trying to do? Thanks much!

Link to comment
Share on other sites

Xcal: "Are you actually using GUIOnEventMode, 1? If you aren't, then GUICtrlSetOnEvent won't work for you. You need to be using GuiGetMsg()."

Yes I am using GUIOnEventMode. The problem is with my ButtonExe() function. How do I make this function aware of which button I'm clicking on. Keep in mind that these buttons are created dynamically.

Link to comment
Share on other sites

Xcal: "Are you actually using GUIOnEventMode, 1? If you aren't, then GUICtrlSetOnEvent won't work for you. You need to be using GuiGetMsg()."

Yes I am using GUIOnEventMode. The problem is with my ButtonExe() function. How do I make this function aware of which button I'm clicking on. Keep in mind that these buttons are created dynamically.

Check out @GUI_CtrlId.

edit -

Here, I was bored...

#include <GUIConstants.au3>

Opt('GUIOnEventMode', 1)
GUICreate('')
GUISetOnEvent($GUI_EVENT_CLOSE, 'quit')

$top = 10

For $i = 1 To 5
    GUICtrlCreateButton('Button ' & $i, 10, $top, 60, 20)
    GUICtrlSetOnEvent(-1, 'myfunc')
    $top += 25
Next

GUISetState()

While 1
    Sleep(100)
WEnd

Func myfunc()
    MsgBox(0, 'button pressed is...', GUICtrlRead(@GUI_CtrlId))
EndFunc


Func quit()
    Exit
EndFunc
Edited by xcal
Link to comment
Share on other sites

Thanks Xcal, I will try using that. Meanwhile, if anyone wants to try to try running my script and seeing my problem here it is.

#include <GUIConstants.au3>

#include <Array.au3>

Opt("GUIOnEventMode",1)

Global $filearray[10]

DIM $xlocation

DIM $ylocation

DIM $width

DIM $height

DIM $i = 0

Dim $Startbutton

GUICreate("Button Test Menu", 800, 600)

GUISetBkColor (0xffdd77)

GUISetOnEvent($GUI_EVENT_CLOSE,"OnExit")

$Startbutton = GuiCtrlCreateButton ("Start",100,100,100,20)

GUICtrlSetOnEvent(-1, "SearchServer")

GUISetState() ; display the GUI

while 1

sleep(1000)

wend

Func OnExit()

Exit

EndFunc

FUNC SearchServer()

$xlocation = 20

$ylocation = 145

$width = 100

$height = 20

$i = 0

FileChangeDir("@systemdir")

$search = FileFindFirstFile("*.txt")

While $i <= 10

$filearray[$i] = FileFindNextFile($search)

If @error Then

$i = $i - 1

ExitLoop

Else

$i = $i + 1

EndIf

Wend

$i = 0

While $i <= 10

If $filearray[$i] = "" Then

ExitLoop

Else

If $i = 5 Then

$xlocation = 200

$ylocation = 150

EndIf

$filearray[$i] = StringUpper($filearray[$i])

GuICtrlCreateButton($filearray[$i], $xlocation, $ylocation, $width, $height)

GUICtrlSetOnEvent(-1, "ButtonExe")

$t = FileGetTime($filearray[$i],1)

$date = $t[1] & "/" & $t[2] & "/" & $t[0]

GUICtrlCreateLabel($date,$xlocation + 110,$ylocation,70,20)

EndIf

$ylocation = $ylocation + 25

$i = $i + 1

Wend

EndFunc

Func ButtonExe()

Run(@systemdir & "notepad.exe" & $filearray[$i])

EndFunc

Link to comment
Share on other sites

^^^ :whistle:

I'll sound like whoever you want me to sound like if you can help with this last thing. My button creation Function will run again if for example, the user clicks on a different button. I need a function that will delete all the buttons I just created and I am completely stomped as to how to even begin (noob talking here).

I'm not looking for someone to write the code for me (although I wouldn't refuse it). I just want suggestions as to how to proceed.

Link to comment
Share on other sites

Not sure if this is the best way, but what I would do is store all the button handles in an array as they're created. Then, you can just run the array through a loop to delete them.

edit -

Or, you could create your buttons like so...

$var &= GUICtrlCreateButton(etc etc) & '|'

Then, you'd have all the handles stored in $var. When you want to delete the buttons, you could do something like...

Func delbtns()
    $handles = StringSplit($var, '|')
    For $i = 1 To UBound($handles) - 1
        GUICtrlDelete($handles[$i])
    Next
EndFunc

edit - added delimiter

Edited by xcal
Link to comment
Share on other sites

$var &= GUICtrlCreateButton(etc etc) & '|'

For whatever reason, the above line didn't work for me. Autoit didn't like "&=". Maybe my version is too old. Anyhow, putting my buttons into another array during the button creation process and using that array as the handle worked like a charm. Once again thanks for your help!!!

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...