Morthawt Posted January 11, 2014 Posted January 11, 2014 I need to have a dynamically created set of GUI buttons and have actions taken based on a number that will be corresponding to each button press. I have created buttons dynamically before by assigning array entries to them so that I can manipulate the buttons colour and placement, but I have never made those dynamically created buttons have an actual function. I thought about doing a case for $nMsg for anything above 0 but I am not sure if that will always only correspond with the buttons that I create. So do you know of a good way that I could acomplish this or will my idea work? The first button seems to throw a 3 always, then the second button send out a 4... So can I just take a > 2 value and subtract 2 from it and use that to access resources stored in an array corresponding to that number? Free and easy Autoit scripting video tutorials (plus more videos always coming!) General video tutorials, especially correct and safe TeamSpeak permissions tutorials.
Morthawt Posted January 11, 2014 Author Posted January 11, 2014 For example, to put it simply, is there anything wrong with doing things this way? #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $Button[2], $iValueToWorkWith #Region ### START Koda GUI section ### Form= $hGUI = GUICreate("Test", 205, 106, -1, -1) $Button[0] = GUICtrlCreateButton("Button1", 16, 16, 75, 25) $Button[1] = GUICtrlCreateButton("Button2", 16, 56, 97, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Select Case $nMsg = $GUI_EVENT_CLOSE ConsoleWrite($nMsg & @CRLF) Exit Case $nMsg > 2 ConsoleWrite(GUICtrlRead($nMsg) & ' ' & 'was pressed' & @CRLF) $iValueToWorkWith = $nMsg - 3 GUIDelete($hGUI) ExitLoop EndSelect WEnd ConsoleWrite('The value to use in the rest of our script now is: ' & $iValueToWorkWith & @CRLF) Free and easy Autoit scripting video tutorials (plus more videos always coming!) General video tutorials, especially correct and safe TeamSpeak permissions tutorials.
Moderators Melba23 Posted January 11, 2014 Moderators Posted January 11, 2014 Morthawt,personally, i always do it this way:#include <GUIConstantsEx.au3> Global $Button[2], $iValueToWorkWith $hGUI = GUICreate("Test", 205, 106, -1, -1) $iStart = GUICtrlCreateDummy() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $Button[0] = GUICtrlCreateButton("Button1", 16, 16, 75, 25) $Button[1] = GUICtrlCreateButton("Button2", 16, 56, 75, 25) $iEnd = GUICtrlCreateDummy() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE ConsoleWrite($nMsg & @CRLF) Exit Case $iStart To $iEnd ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ConsoleWrite(GUICtrlRead($nMsg) & ' ' & 'was pressed' & @CRLF) $iValueToWorkWith = $nMsg - $iStart ; Adjust as required GUIDelete($hGUI) ExitLoop EndSwitch WEnd ConsoleWrite('The value to use in the rest of our script now is: ' & $iValueToWorkWith & @CRLF)No need for hard-coded values this way. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Morthawt Posted January 11, 2014 Author Posted January 11, 2014 Morthawt, personally, i always do it this way: #include <GUIConstantsEx.au3> Global $Button[2], $iValueToWorkWith $hGUI = GUICreate("Test", 205, 106, -1, -1) $iStart = GUICtrlCreateDummy() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $Button[0] = GUICtrlCreateButton("Button1", 16, 16, 75, 25) $Button[1] = GUICtrlCreateButton("Button2", 16, 56, 75, 25) $iEnd = GUICtrlCreateDummy() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE ConsoleWrite($nMsg & @CRLF) Exit Case $iStart To $iEnd ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ConsoleWrite(GUICtrlRead($nMsg) & ' ' & 'was pressed' & @CRLF) $iValueToWorkWith = $nMsg - $iStart ; Adjust as required GUIDelete($hGUI) ExitLoop EndSwitch WEnd ConsoleWrite('The value to use in the rest of our script now is: ' & $iValueToWorkWith & @CRLF) No need for hard-coded values this way. M23 Oh my.. I believe I understand what is going on there. Very sneaky indeed I love it! Free and easy Autoit scripting video tutorials (plus more videos always coming!) General video tutorials, especially correct and safe TeamSpeak permissions tutorials.
Morthawt Posted January 11, 2014 Author Posted January 11, 2014 Now, building on this idea. That idea works directly if all I am doing is creating the buttons continually without break. What if I have other controls being created like tabs to break the buttons into sections? Then the next button no longer follows the track because the tab that was created now has an ID assigned to it so the button ends up getting the one after that. What would you suggest to negate the existence of the tabs so that the buttons still have their ID be a continuous number stream? Free and easy Autoit scripting video tutorials (plus more videos always coming!) General video tutorials, especially correct and safe TeamSpeak permissions tutorials.
Morthawt Posted January 11, 2014 Author Posted January 11, 2014 Just working with concepts before I jump into my real coding. Are there any technical issues with this method? #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <TabConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 615, 437, 192, 124) $Main = GUICtrlCreateTab(16, 64, 577, 313) GUICtrlCreateTabItem('Main') $button = GUICtrlCreateButton("Button1", 24, 88, 75, 25) Assign(GUICtrlGetHandle(-1), 1) $button1p5 = GUICtrlCreateButton("Button1.5", 24, 120, 75, 25) Assign(GUICtrlGetHandle(-1), 1.5) $test = GUICtrlCreateTabItem('Secondary') $button2 = GUICtrlCreateButton("Button2", 24, 88, 75, 25) Assign(GUICtrlGetHandle(-1), 2) GUICtrlCreateTabItem('Tertiary') $button3 = GUICtrlCreateButton("Button3", 24, 88, 75, 25) Assign(GUICtrlGetHandle(-1), 3) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ConsoleWrite(GUICtrlGetHandle($test) & @CRLF) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Main To $button3 If IsDeclared(GUICtrlGetHandle($nMsg)) Then MsgBox(0, 0, Eval(GUICtrlGetHandle($nMsg))) EndSwitch WEnd Free and easy Autoit scripting video tutorials (plus more videos always coming!) General video tutorials, especially correct and safe TeamSpeak permissions tutorials.
Moderators Melba23 Posted January 11, 2014 Moderators Posted January 11, 2014 Morthawt,Why not use GUISwitch and so keep the buttons in a single block? #include <GUIConstantsEx.au3> $Form1 = GUICreate("Form1", 615, 437, 192, 124) $Main = GUICtrlCreateTab(16, 64, 577, 313) $cTab_Main = GUICtrlCreateTabItem('Main') $cTab_Sec = GUICtrlCreateTabItem('Secondary') $cTab_Ter = GUICtrlCreateTabItem('Tertiary') GUICtrlCreateTabItem('') ; Close the tab item structure <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< GUISetState(@SW_SHOW) $iStart = GUICtrlCreateDummy() GUISwitch($Form1, $cTab_Main) ; Select the tab to use <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $button1 = GUICtrlCreateButton("Button1", 24, 88, 75, 25) $button1p5 = GUICtrlCreateButton("Button1.5", 24, 120, 75, 25) GUISwitch($Form1, $cTab_Sec) $button2 = GUICtrlCreateButton("Button2", 54, 88, 75, 25) GUISwitch($Form1, $cTab_Ter) $button3 = GUICtrlCreateButton("Button3", 84, 88, 75, 25) GUICtrlCreateTabItem('') GUICreate($Form1) $iEnd = GUICtrlCreateDummy() While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $iStart To $iEnd ConsoleWrite(GUICtrlRead($nMsg) & ' was pressed - code ' & $nMsg - $iStart& @CRLF) EndSwitch WEndM23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Morthawt Posted January 11, 2014 Author Posted January 11, 2014 lol you really know you're AutoIt . So basically if I understand, by creating all the tabs first, then "changing" to each tab during creation, the buttons all have continuous ID numbers? Hmm, so I can do that next time, but are there any issues with my previous example that should make me do it this new way? Free and easy Autoit scripting video tutorials (plus more videos always coming!) General video tutorials, especially correct and safe TeamSpeak permissions tutorials.
Moderators Melba23 Posted January 11, 2014 Moderators Posted January 11, 2014 Morthawt, you really know you're AutoItI rather hope so. the buttons all have continuous ID numbersControlIDs are the indices to an internal Autoit array which holds details of all the native created controls in the script. These ControlIDs are allocated in numerical order, so creating controls in immediate succession should give you consecutive values. The allocation is actually to the first vacant element in the array so if you have previously deleted some controls this is not guaranteed - and as it is undocumented it might not be true in future releases. Of course to be absolutely correct you should put the ControlIDs into an array and loop through it, but if you take care you can do as I did above. are there any issues with my previous exampleYes, it is a misuse of the Assign function which could well bite you later. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
kylomas Posted January 11, 2014 Posted January 11, 2014 Morthawt, Yes, it is a misuse of the Assign function which could well will bite you later. Another way to do this... #include <GUIConstantsEx.au3> #include <array.au3> #AutoIt3Wrapper_Add_Constants=n local $aButtons[13] GUICreate("GUI Buttons") For $i = 0 To UBound($aButtons)-1 $aButtons[$i]=GUICtrlCreateButton($i+1,$i*30+5,20,25,25) Next GUISetState() While 1 $msg = guigetmsg() switch $msg Case $GUI_EVENT_CLOSE Exit Case $aButtons[0] to $aButtons[ubound($aButtons)-1] MsgBox(1,1,"ButtonText=[" & GUICtrlRead($msg) & "] was clicked") EndSwitch WEnd kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now