Jump to content

Switch Statement not working with GuiButton Array


dinodod
 Share

Recommended Posts

I have a simple app that I am designing where I am trying to put the IDs of every button I generate into an array to be matched up in the Switch / End Switch section but it would appear that when i am clicking on a button, I am not getting the correct ID that was assigned to the button when it was created. Upon running this app, I have a popup which is returning the value of the button / tab that is being pressed.

I'm a bit eye sore right now as I can't understand how my Switch section is not reading the values correctly. Any advise is welcomed greatly!

INI File

=================

[Games]
DOD="game1"

[Apps]
MIRC="F:\mIRC\mirc.exe"
treMIRC="F:\mIRC\mirc.ex"

[test]
asdd="adsa"
acq="asd"
hjj="jhgjgh"

AU3 Script

=================

;~ ============================================
;~                              Includes
;~ ============================================
#include <GUIConstants.au3>




;~ ============================================
;~                              Variables
;~ ============================================
$MenuIniFile = "desktop menu.ini"
Dim $Button[1]
Dim $b = 0

$Array_INISectionNames = IniReadSectionNames ($MenuIniFile)
Dim $GUI_tabName[UBound($Array_INISectionNames)]



;~ ============================================
;~                          Verify INI file exists
;~ ============================================
If not FileExists ( $MenuIniFile ) Then
    msgbox(0,"File Missing","File not found - " & $MenuIniFile)
    Exit
EndIf

;~ ============================================
;~                              Create GUI
;~ ============================================
GUICreate("My GUI Tab",250,150); will create a dialog box that when displayed is centered
    
    $tab=GUICtrlCreateTab (10,10, 200,100)
    
            For $x = 1 to  $Array_INISectionNames[0]
                $GUI_tabName[$x]=GUICtrlCreateTabitem ($Array_INISectionNames[$x])
                
                    $INI_KeyValuePair = IniReadSection($MenuIniFile, $Array_INISectionNames[$x])
                    
                     If @error Then 
                        MsgBox(4096, "", "Trouble Reading Keys In: " & $MenuIniFile & "   Section: " & $Array_INISectionNames[$x]
                        Exit
                    EndIf
                    
                ;~ These variables are placed here so they get reset on each tab
                ;~ ======================================
                    Dim $Label_Y = 30, $Label_X = 20
                    
                    
                ;Read from the array and generate the menus
                ;=============================
                    For $i = 1 To $INI_KeyValuePair[0][0]
                        ReDim $Button[$b+1]
                        $Button[$b] = GUICtrlCreateButton ($INI_KeyValuePair[$i ][0],  $Label_X, $Label_Y + 15)
                    ;msgbox(0,"",$Button[$b] & " | " & $INI_KeyValuePair[$i ][0])
                        $b = $b + 1
                        $Label_Y = $Label_Y + 30
                    Next
            Next

    GUICtrlCreateTabitem ("")  ; end tabitem definition
    GUISetState () ;Display the GUI

msgbox(0,"Value of $Button[1] is: ",$Button[1])
;~ Exit

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    
;~  if $msg <> -11 or -3 then 
;~      if $msg <> -7  Then
;~              if $msg <> 0  Then
;~                  msgbox(0,"id",$msg)
;~              EndIf
;~      EndIf
;~  EndIf
    
         Switch $msg  
             
            
            Case -11
            
            Case 0

                    
            Case $msg = $Button[1] to $Button[$Button[0]]
                    
;~              For $x = 1 to UBound($Button) -1  
;~                      If $msg = $Button[$x] Then
                        msgbox(0,"found it","ID matched: " & $msg & " === " & $Button[1] & " - " & $Button[$Button[0]])
;~                      EndIf
;~                  Next


            Case $msg = $GUI_EVENT_CLOSE 
                ExitLoop
            
            Case Else
            ;msgbox(0,"",$msg)
            
            EndSwitch
    
Wend
Edited by dinodod

Digital Chaos - Life as we know it today.I'm a Think Tank. Problem is, my tank is empty.The Quieter you are, the more you can HearWhich would you choose - Peace without Freedom or Freedom without Peace?Digital Chaos Macgyver ToolkitCompletely Dynamic MenuSQLIte controlsAD FunctionsEXCEL UDFPC / Software Inventory UDFPC / Software Inventory 2GaFrost's Admin Toolkit - My main competitor :)Virtual SystemsVMWAREMicrosoft Virtual PC 2007

Link to comment
Share on other sites

I have a simple app that I am designing where I am trying to put the IDs of every button I generate into an array to be matched up in the Switch / End Switch section but it would appear that when i am clicking on a button, I am not getting the correct ID that was assigned to the button when it was created. Upon running this app, I have a popup which is returning the value of the button / tab that is being pressed.

I'm a bit eye sore right now as I can't understand how my Switch section is not reading the values correctly. Any advise is welcomed greatly!

INI File

=================

[Games]
DOD="game1"

[Apps]
MIRC="F:\mIRC\mirc.exe"
treMIRC="F:\mIRC\mirc.ex"

[test]
asdd="adsa"
acq="asd"
hjj="jhgjgh"

AU3 Script

=================

;~ ============================================
;~                              Includes
;~ ============================================
#include <GUIConstants.au3>




;~ ============================================
;~                              Variables
;~ ============================================
$MenuIniFile = "desktop menu.ini"
Dim $Button[1]
Dim $b = 0

$Array_INISectionNames = IniReadSectionNames ($MenuIniFile)
Dim $GUI_tabName[UBound($Array_INISectionNames)]



;~ ============================================
;~                          Verify INI file exists
;~ ============================================
If not FileExists ( $MenuIniFile ) Then
    msgbox(0,"File Missing","File not found - " & $MenuIniFile)
    Exit
EndIf

;~ ============================================
;~                              Create GUI
;~ ============================================
GUICreate("My GUI Tab",250,150); will create a dialog box that when displayed is centered
    
    $tab=GUICtrlCreateTab (10,10, 200,100)
    
            For $x = 1 to  $Array_INISectionNames[0]
                $GUI_tabName[$x]=GUICtrlCreateTabitem ($Array_INISectionNames[$x])
                
                    $INI_KeyValuePair = IniReadSection($MenuIniFile, $Array_INISectionNames[$x])
                    
                     If @error Then 
                        MsgBox(4096, "", "Trouble Reading Keys In: " & $MenuIniFile & "   Section: " & $Array_INISectionNames[$x]
                        Exit
                    EndIf
                    
            ;~ These variables are placed here so they get reset on each tab
            ;~ ======================================
                    Dim $Label_Y = 30, $Label_X = 20
                    
                    
            ;Read from the array and generate the menus
            ;=============================
                    For $i = 1 To $INI_KeyValuePair[0][0]
                        ReDim $Button[$b+1]
                        $Button[$b] = GUICtrlCreateButton ($INI_KeyValuePair[$i ][0],  $Label_X, $Label_Y + 15)
                ;msgbox(0,"",$Button[$b] & " | " & $INI_KeyValuePair[$i ][0])
                        $b = $b + 1
                        $Label_Y = $Label_Y + 30
                    Next
            Next

    GUICtrlCreateTabitem ("") ; end tabitem definition
    GUISetState ();Display the GUI


; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    
;~  msgbox(0,"id",$msg)
         Switch $msg  
             
            
            Case -11
            
            Case 0
;~                  msgbox(0,"","Zero")
                    
            Case $msg = 1 to UBound($Button) 
                    
;~              For $x = 1 to UBound($Button) -1  
;~                      If $msg = $Button[$x] Then
                        msgbox(0,"found it","ID matched: " & $msg)
;~                      EndIf
;~                  Next


            Case $msg = $GUI_EVENT_CLOSE 
                ExitLoop
            
            Case Else
                msgbox(0,"",$msg)
            
            EndSwitch
    
Wend
You are trying to use the Select syntax in a Switch statement and You did not specify an array element in the starting point of the Case statement.

Case $msg = 1 to UBound($Button)

Case $Button[1] to Ubound($Button) -1

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

You are trying to use the Select syntax in a Switch statement and You did not specify an array element in the starting point of the Case statement.

Case $msg = 1 to UBound($Button)

Case $Button[1] to Ubound($Button) -1

Case $msg = $Button[1] to $Button[$Button[0]]

UBound(Button)-1 will not give me the correct number needed to match against the IDs of the button as UBOUND will tell me the highest number in the array, not the value of the last item in the array. I just figured that out myself :)

Now it is almost working but I have anotehr issue I am needing to work on.... Stay tuned :P

Digital Chaos - Life as we know it today.I'm a Think Tank. Problem is, my tank is empty.The Quieter you are, the more you can HearWhich would you choose - Peace without Freedom or Freedom without Peace?Digital Chaos Macgyver ToolkitCompletely Dynamic MenuSQLIte controlsAD FunctionsEXCEL UDFPC / Software Inventory UDFPC / Software Inventory 2GaFrost's Admin Toolkit - My main competitor :)Virtual SystemsVMWAREMicrosoft Virtual PC 2007

Link to comment
Share on other sites

Ok, I just updated the AU3 code on my 1st post. Now I am confused over the readngs I am getting here. The value of the 1st button is 7. My Case statement is looking for values between 7-12 (IDs of the buttons) yet now when i click on the 1st button, it tells me the $msg is 5? And when i click on the tabs, $msg is 3 yet it's triggering the wrong Case trigger???

Digital Chaos - Life as we know it today.I'm a Think Tank. Problem is, my tank is empty.The Quieter you are, the more you can HearWhich would you choose - Peace without Freedom or Freedom without Peace?Digital Chaos Macgyver ToolkitCompletely Dynamic MenuSQLIte controlsAD FunctionsEXCEL UDFPC / Software Inventory UDFPC / Software Inventory 2GaFrost's Admin Toolkit - My main competitor :)Virtual SystemsVMWAREMicrosoft Virtual PC 2007

Link to comment
Share on other sites

Did you REALLY read my last reply?

Take the Msg = out of the case statement. You have already told it to switch based on $Msg. Also you are right that Ubound($Button) will return the number of elements. That's why I said Ubound($Button) -1

On top of that, the way I count, the first button is control #5. Of course I may have mis-counted.

I'll try to clean up your code enough to test here but I'm just winging it.

;~ ============================================
;~                               Create GUI
;~ ============================================
GUICreate("My GUI Tab",250,150); will create a dialog box that when displayed is centered
    
    $tab=GUICtrlCreateTab (10,10, 200,100)
    
               For $x = 1 to  $Array_INISectionNames[0]
                   $GUI_tabName[$x]=GUICtrlCreateTabitem ($Array_INISectionNames[$x])
                   
                    $INI_KeyValuePair = IniReadSection($MenuIniFile, $Array_INISectionNames[$x])
                    
                     If @error Then 
                        MsgBox(4096, "", "Trouble Reading Keys In: " & $MenuIniFile & "   Section: " & $Array_INISectionNames[$x]
                        Exit
                    EndIf
                    
               ;~ These variables are placed here so they get reset on each tab
               ;~ ======================================
                    Dim $Label_Y = 30, $Label_X = 20
                    
                    
               ;Read from the array and generate the menus
               ;=============================
                    For $i = 1 To $INI_KeyValuePair[0][0]
                        ReDim $Button[$b+1]
                        $Button[$b] = GUICtrlCreateButton ($INI_KeyValuePair[$i ][0],  $Label_X, $Label_Y + 15)
                   ;msgbox(0,"",$Button[$b] & " | " & $INI_KeyValuePair[$i ][0])
                        $b = $b + 1
                        $Label_Y = $Label_Y + 30
                    Next
               Next

    GUICtrlCreateTabitem ("") ; end tabitem definition
    GUISetState ();Display the GUI

msgbox(0,"Value of $Button[1] is: ",$Button[1])
;~ Exit
For $I = $button[1] To Ubound($button)-1
  MsgBox(0, "First test", "The control ID is " & $I & @CRLF & GUICtrlRead($I))
Next

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    
         Switch $msg  

            Case $GUI_EVENT_CLOSE 
                ExitLoop
            
            Case Else
               msgbox(0,"Control #",$msg)
            
          EndSwitch
    
Wend

Run this and see what values are being returned.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I think I am getting confused over this. I re-read your post and removed the $msg :) Sorry about that.

However, from what I can see, you are trying to determine the Control ID of the button in which case it is 5 but the value of $Button[1] = 7 [ $Button[$b] = GUICtrlCreateButton (...) ] so how is this happening?

I then re-read the help file to see if I was looking for the wrong information but according to the help file, the variable assigned to the CreateButton will hold the Identifier (ControlID). I don't understand how I am getting 2 seperate values for one button.

HELP File Example

------------------------------

$Button_1 = GUICtrlCreateButton ("Run Notepad", 10, 30, 100)

...

Case $msg = $Button_1

Run('Notepad.exe') ; Will Run/Open Notepad

So I have modified the code to this to trap the click event and display $msg. When on the second or third tab, I am getting negative numbers for $msg!!! This is so confusing

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    
         Switch $msg  
             
    Case 0
    Case -11
    Case -8
        
                    
            Case 1 to 15;Ubound($Button) -1 ;$Button[$Button[0]]
                    msgbox(0,"",$msg)
                    
;~              For $x = 1 to UBound($Button) -1  
;~                      If $msg = $Button[$x] Then
                        msgbox(0,"found it","ID matched: " & $msg & " === " & $Button[1] & " - " & $Button[$Button[0]])
;~                      EndIf
;~                  Next


            Case $msg = $GUI_EVENT_CLOSE 
                ExitLoop
            
            Case Else
                msgbox(0,"",$msg)
            
        EndSwitch
    
Wend
Edited by dinodod

Digital Chaos - Life as we know it today.I'm a Think Tank. Problem is, my tank is empty.The Quieter you are, the more you can HearWhich would you choose - Peace without Freedom or Freedom without Peace?Digital Chaos Macgyver ToolkitCompletely Dynamic MenuSQLIte controlsAD FunctionsEXCEL UDFPC / Software Inventory UDFPC / Software Inventory 2GaFrost's Admin Toolkit - My main competitor :)Virtual SystemsVMWAREMicrosoft Virtual PC 2007

Link to comment
Share on other sites

Example with GUIOnEventMode:

#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)


Dim $button[18][3]
$Form1 = GUICreate("Form1", 271, 215, 193, 125)

GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")

$i = 0
For $x = 8 To 184 Step 88
    For $y = 8 to 168 Step 32
        $button[$i][0] = GUICtrlCreateButton("Button " & $i, $x, $y, 75, 25, 0)
        $button[$i][1] = GUICtrlGetHandle ($button[$i][0])
        $button[$i][2] = "Title " & $i
        GUICtrlSetOnEvent (-1, "Button")
        $i += 1
    Next
Next

GUISetState(@SW_SHOW)

While 1
    Sleep (10)
WEnd

Func Button ()
    $ctrl = @GUI_CTRLID
    $text = ""
    For $g = 0 to UBound ($button) - 1
        If $ctrl = $button[$g][0] Then
            $text = "Control ID: " & $button[$g][0] & @CRLF
            $text &= "Handle: " & $button[$g][1] & @CRLF
            $text &= "Text: " & $button[$g][2] & @CRLF
            MsgBox (0, "Control Pressed", $text)
        EndIf
    Next
EndFunc

Func SpecialEvents()
    Select
        Case @GUI_CTRLID = $GUI_EVENT_CLOSE
            MsgBox(0, "Close Pressed", "ID=" & @GUI_CTRLID & " WinHandle=" & @GUI_WINHANDLE)
            Exit
        Case @GUI_CTRLID = $GUI_EVENT_MINIMIZE
            MsgBox(0, "Window Minimized", "ID=" & @GUI_CTRLID & " WinHandle=" & @GUI_WINHANDLE)
        Case @GUI_CTRLID = $GUI_EVENT_RESTORE
            MsgBox(0, "Window Restored", "ID=" & @GUI_CTRLID & " WinHandle=" & @GUI_WINHANDLE)
    EndSelect   
EndFunc
Link to comment
Share on other sites

BERT!!!! I bow before you! OMG That snippet is so much cleaner! And with a little tweaking, it should do the trick :)

Thank you!!!!

The onEventMode really makes a huge difference in the code.

Digital Chaos - Life as we know it today.I'm a Think Tank. Problem is, my tank is empty.The Quieter you are, the more you can HearWhich would you choose - Peace without Freedom or Freedom without Peace?Digital Chaos Macgyver ToolkitCompletely Dynamic MenuSQLIte controlsAD FunctionsEXCEL UDFPC / Software Inventory UDFPC / Software Inventory 2GaFrost's Admin Toolkit - My main competitor :)Virtual SystemsVMWAREMicrosoft Virtual PC 2007

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