Sign in to follow this  
Followers 0
John117

GUICtrlSetOnEvent that works with buttons build by array?

5 posts in this topic

#1 ·  Posted (edited)

I have the following code that builds buttons from an array:

;BUILD BUTTONS
Dim $aButtons[$aRecords[0] + 1]
$aTop = 35
$aLeft = 10
For $x = 1 To $aRecords[0]
$aButtons[$x] = GUICtrlCreateButton($aRecords[$x], $aLeft, $aTop, 100, 24, $BS_NOTIFY)
$aTop = $aTop + 25
NextoÝ÷ Ù8^±æî¶Ú'±ªÞâ¶Þv)æÉhbíZ׺Çv'ßz·§µ§-ÛjÇ¢|!È[ºÛh޲Ǭ¶¦J)©ëޮȨ·¥£®¶­sevÆR¢b33c¶×6rÒuTvWD×6r¤f÷"b33c·ÒFòb33c¶&V6÷&G5³Ð¢bb33c¶×6rÒb33c¶'WGFöç5²b33c·ÒFVà¤×6t&÷ÂgV÷C´'WGFöâ&W76VBgV÷C²ÂuT7G&Å&VBb33c¶'WGFöç5²b33c·Ò¤VæD`¤æW@¥tVæ@

The real version is not so simple and makes the while 1 very hard to follow.

I would like to change the buttons to CtrlSetOnEvent, point to a function that contains a loop for the code and simplify my code.

Can anyone tell me how to add ctrlsetonevent to this and still keep it running smoothly?

Thanks

Edited by Hatcheda

Share this post


Link to post
Share on other sites



Well, usually, codes written with GUIGetMsg tend to require a near complete re-write when changed to OnEventMode

If I had your whole code, i could do more to help.

Share this post


Link to post
Share on other sites

Well, usually, codes written with GUIGetMsg tend to require a near complete re-write when changed to OnEventMode

If I had your whole code, i could do more to help.

Hey Paulie, Thanks for the reply!

Sorry I couldn't post the entire code earlier. Its getting pretty large and 80% of it is sqlite stuff and I didn't want to scare posters off or have them spending a day or two to just figure out what was happening.

I have been changing everything out to work with onevent but am left with the array button question.

I have created a working example for you below. If you could help me change this to on event and still function, that would be great! BTW, the names in the text file will not always have a pattern or be readily known, so I can't just hard code it :)

The whole point of this is to get the button name. ^_^ Thanks!

#include <GuiConstants.au3>
#include <file.au3>
Dim $aRecords
If Not _FileReadToArray(@ScriptDir & "\Add New Contacts Here.txt", $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
    Exit
EndIf

;The above section is dependand on a txt file with the following name and format.
;Add New Contacts Here.txt  -Must be in same directory script
;Userid1
;Userid2
;Userid3
;The txt file would only contain the above three examples or more if you wish. you should not leave space below any names

Dim $aButtons[$aRecords[0] + 1]
$aHeight = (UBound($aRecords) * 25) + 60

; GUI
$window = GUICreate(@UserName & "'s Example", 250, $aHeight)

; Buttons
$aTop = 35
$aLeft = 10
For $x = 1 To $aRecords[0]
    $aButtons[$x] = GUICtrlCreateButton($aRecords[$x], $aLeft, $aTop, 100, 24, $BS_NOTIFY)
    $aTop = $aTop + 25
Next

GUISetState()

;SET BUTTON FUNCTIONS
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
    For $x = 1 To $aRecords[0]
        If $msg = $aButtons[$x] Then
            MsgBox(0, "Button Pressed", GUICtrlRead($aButtons[$x]))
        EndIf
    Next
    Sleep(10)
WEnd

Don't forget to dl the txt file to the same directory or just create one with the same name and add a list to it like in the code.

Share this post


Link to post
Share on other sites

One method to identify the buttons is by just reading the text off of them:

#include <GuiConstants.au3>

Opt("GuiOnEventMode", 1)

; Simulate reading text file into $aRecords
Global $aRecords[6] = [5, "User One", "User Two", "User Three", "User Four", "User Five"]

$hGUI = GUICreate("Button Test", 350, 10 + (40 * $aRecords[0]))
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
For $n = 1 to $aRecords[0]
    GUICtrlCreateButton($aRecords[$n], 100, 10 + (40 * ($n - 1)), 150, 30)
    GUICtrlSetOnEvent(-1, "_ButtonHit")
Next
GUISetState()

While 1
    Sleep(20)
WEnd

Func _ButtonHit()
    MsgBox(64, "Button Clicked!", "You clicked: " & GUICtrlRead(@GUI_CtrlId), 5)
EndFunc

Func _Quit()
    Exit
EndFunc

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

@Psalty - that's exactly what I was hoping to learn :) thanks as always.

@Paulie - Thanks aswell!

Share this post


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
Sign in to follow this  
Followers 0