Jump to content

clickable control button problems


Recommended Posts

Hello,

I have the code below to create a simple interface whereby the user is given a choice of 2 buttons to click. Upon clicking one of the buttons I want the other button to "disappear" by using a 'destroy' command...what is happening however is that for some reason upon running the script the "Button_2" is immediately destroyed/removed without the user doing anything...

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <WinAPI.au3>
#include <GuiScrollBars.au3>
#include <ScrollBarConstants.au3>
#include <GDIPlus.au3>
#include <GuiButton.au3>
#include <ButtonConstants.au3>

Opt("GUIOnEventMode", 0) ;0=disabled, 1=OnEvent mode enabled

Global $MainGUI = GUICreate("MAIN STARTUP", 500, 500)
GUISetState(@SW_SHOW)

$font = "Arial"
Sleep(500)
Scenario_Window()
Sleep(500)
Database_Window()

While 1
$nMsg = GUIGetMsg()
Switch $nMsg

     Case $GUI_EVENT_CLOSE
     Exit
     Case $nMsg = $Button_1
     _GUICtrlButton_Destroy($Button_2) ;Remove $Button_2
     Case $nMsg = $Button_2
         _GUICtrlButton_Destroy($Button_1) ;Remove $Button_1

EndSwitch
WEnd

Func Scenario_Window()

Global $Scenario_Back = GUICreate("", 249, 249, 0, 150, $WS_CHILD, -1, $MainGUI)

GUICtrlCreateGraphic(1, 1, 248, 248)
GUICtrlSetBkColor(-1, 0xffffff)
GUICtrlSetColor(-1, 0)
$text = GUICtrlCreateLabel("Select if you wish to PLAY, CREATE, or EDIT a Scenario", 1, 5, 248, 40, BitOr($GUI_SS_DEFAULT_LABEL, $SS_CENTER), -1)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
Global $Button_1 = GUICtrlCreateButton("Scenarios", 72.5, 70, 100, 30, $BS_PUSHLIKE)
GUICtrlSetOnEvent($Button_1, "ScenarioPressed")
GUICtrlSetFont($text, 12, 400, 1, $font)
GUISetState(@SW_SHOW)
EndFunc

Func Database_Window()

Global $Data_Back = GUICreate("", 249, 249, 251, 150, $WS_CHILD, -1, $MainGUI)

GUICtrlCreateGraphic(1, 1, 248, 248)
GUICtrlSetBkColor(-1, 0xffffff)
GUICtrlSetColor(-1, 0)
$text2 = GUICtrlCreateLabel("Select if you wish to VIEW or EDIT a Database", 1, 5, 248, 40, BitOr($GUI_SS_DEFAULT_LABEL, $SS_CENTER), -1)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlSetFont($text2, 12, 400, 1, $font)
Global $Button_2 = GUICtrlCreateButton("Databases", 72.5, 70, 100, 30, $BS_PUSHLIKE)
GUICtrlSetOnEvent($Button_2, "DataPressed")
GUISetState(@SW_SHOW)
EndFunc

Func ScenarioPressed()
_GUICtrlButton_Destroy($Button_2) ;Remove $Button_2
EndFunc

Func DataPressed()
_GUICtrlButton_Destroy($Button_1) ;Remove $Button_1
EndFunc

If I modify/change the "While Loop" within this script to the following then neither button is 'clickable'...

While 1
     $msg = GUIGetMsg()
     Select
         Case $msg = $GUI_EVENT_CLOSE
         Exit
         Case $msg = $Button_1
         _GUICtrlButton_Destroy($Button_2) ;Remove $Button_2
         Case $msg = $Button_2
         _GUICtrlButton_Destroy($Button_1) ;Remove $Button_1
     EndSelect
WEnd

And if I modify/change the "While Loop" and also disable the "GUIonEventMode" again neither button is 'clickable'...

Opt("GUIOnEventMode", 1) ;0=disabled, 1=OnEvent mode enabled

; Just idle around
While 1
     Sleep(10)
WEnd

Why is it the buttons cannot be clicked (NOTE the label text DO NOT overlap the buttons...they are place too far apart)? Why in the first "While Loop" is the code automatically 'selecting' the "Button_1"...? Any help appreciated...

Link to comment
Share on other sites

2 things.

1st you can't mix OnEvent mode and message loop mode in the same script, which you are doing with the controls in the Scenario_Window function, stick to one.

2nd, your Case statements in your While loop are wrong, see below.

; this line 
Case $nMsg = $Button_1
;should be written like this
Case $Button_1
;You're mixing the style for Switch with the style for Select, they aren't written the same.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hi,

Thanks for the reply. I commented out the GUICtrlsetOnEvent and the Opt(GUIonEventMode) and the associated Functions for the "OnEvent" stuff...I changed the "While" loop to:

While 1
$nMsg = GUIGetMsg()
Switch $nMsg

     Case $GUI_EVENT_CLOSE
     Exit
     Case $Button_1
     _GUICtrlButton_Destroy($Button_2) ;Remove $Button_2
     Case $Button_2
         _GUICtrlButton_Destroy($Button_1) ;Remove $Button_1

EndSwitch
WEnd

However I still cannot click the buttons...I also changed the "While" loop to this to try that out as well...:but it didn't help:

While 1
     $msg = GUIGetMsg()
     Select
         Case $msg = $GUI_EVENT_CLOSE
         Exit
         Case $msg = $Button_1
         _GUICtrlButton_Destroy($Button_2) ;Remove $Button_2
         Case $msg = $Button_2
         _GUICtrlButton_Destroy($Button_1) ;Remove $Button_1
     EndSelect
WEnd

I am sure there's something simple I must be missing here...

Link to comment
Share on other sites

If I add some radio buttons to the "Scenario_Window" Function such as:

Global $radio1 = GUICtrlCreateRadio("Radio 1", 10, 120, 120, 20)
Global $radio2 = GUICtrlCreateRadio("Radio 2", 10, 140, 120, 20)
Global $radio3 = GUICtrlCreateRadio("Radio 2", 10, 160, 120, 20)

And then modify the "While" loop code to reflect those additions:

While 1
$nMsg = GUIGetMsg()
Switch $nMsg

     Case $GUI_EVENT_CLOSE
     Exit
     Case $Button_1
        _GUICtrlButton_Destroy($Button_2) ;Remove $Button_2
     Case $Button_2
         _GUICtrlButton_Destroy($Button_1) ;Remove $Button_1
     Case $radio1 And BitAND(GUICtrlRead($radio1), $GUI_CHECKED) = $GUI_CHECKED
         MsgBox(64, 'Info:', 'You clicked the Radio 1 and it is Checked.')
     Case $radio2 And BitAND(GUICtrlRead($radio2), $GUI_CHECKED) = $GUI_CHECKED
         MsgBox(64, 'Info:', 'You clicked on Radio 2 and it is Checked.')
     Case $radio3 And BitAND(GUICtrlRead($radio3), $GUI_CHECKED) = $GUI_CHECKED
         MsgBox(64, 'Info:', 'You clicked on Radio 3 and it is Checked.')

EndSwitch
WEnd

Again the script "loops" endlessly displaying the message "You clicked the Radio 1 and it is Checked." and does upon script execution and without the user doing anything...?

Edited by Burgs
Link to comment
Share on other sites

I looked at your script in more depth and found several problems with how you're dealing with the GUI(s) you're creating. You have created 3 GUIs and are not dealing with that in the MessageLoop, your buttons are all on different GUIs so you have to read the window messages differently. Also, you have a graphic on those 2 child GUIs that is overlapping the buttons on them so the only thing you're clicking on is the graphic and not the buttons themselves. See the script below for a fix.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <WinAPI.au3>
#include <GuiScrollBars.au3>
#include <ScrollBarConstants.au3>
#include <GDIPlus.au3>
#include <GuiButton.au3>
#include <ButtonConstants.au3>

Opt("GUIOnEventMode", 0) ;0=disabled, 1=OnEvent mode enabled
Global $Button_1
Global $Data_Back
Global $Scenario_Back
Global $Button_2
Global $MainGUI = GUICreate("MAIN STARTUP", 500, 500)
GUISetState(@SW_SHOW)

$font = "Arial"
Sleep(500)
Scenario_Window()
Sleep(500)
Database_Window()

While 1
    $nMsg = GUIGetMsg(1); use advanced mode when using multiple GUIs
    Switch $nMsg[1] ;Switch on the GUI sending the message
        Case $MainGUI
            Switch $nMsg[0] ; switch on the control from the GUI sending the message.
                Case $GUI_EVENT_CLOSE
                    Exit
            EndSwitch
        Case $Scenario_Back
            Switch $nMsg[0]
                Case $Button_1
                    _GUICtrlButton_Destroy($Button_2) ;Remove $Button_2
            EndSwitch
        Case $Data_Back
            Switch $nMsg[0]
                Case $Button_2
                    _GUICtrlButton_Destroy($Button_1) ;Remove $Button_1
            EndSwitch
    EndSwitch
WEnd

Func Scenario_Window()
    $Scenario_Back = GUICreate("", 249, 249, 0, 150, $WS_CHILD, -1, $MainGUI)
    GUICtrlCreateGraphic(1, 1, 248, 248)
    GUICtrlSetBkColor(-1, 0xffffff)
    GUICtrlSetColor(-1, 0)
    GUICtrlSetState(-1, $GUI_DISABLE) ; you have to disable the graphic or it overlaps all controls on it
    $text = GUICtrlCreateLabel("Select if you wish to PLAY, CREATE, or EDIT a Scenario", 1, 5, 248, 40, BitOR($GUI_SS_DEFAULT_LABEL, $SS_CENTER), -1)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    $Button_1 = GUICtrlCreateButton("Scenarios", 72.5, 70, 100, 30, $BS_PUSHLIKE)
    GUICtrlSetFont($text, 12, 400, 1, $font)
    GUISetState(@SW_SHOW)
EndFunc   ;==>Scenario_Window

Func Database_Window()
    $Data_Back = GUICreate("", 249, 249, 251, 150, $WS_CHILD, -1, $MainGUI)
    GUICtrlCreateGraphic(1, 1, 248, 248)
    GUICtrlSetBkColor(-1, 0xffffff)
    GUICtrlSetColor(-1, 0)
    GUICtrlSetState(-1, $GUI_DISABLE) ; you have to disable the graphic or it overlaps all controls on it
    $text2 = GUICtrlCreateLabel("Select if you wish to VIEW or EDIT a Database", 1, 5, 248, 40, BitOR($GUI_SS_DEFAULT_LABEL, $SS_CENTER), -1)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont($text2, 12, 400, 1, $font)
    $Button_2 = GUICtrlCreateButton("Databases", 72.5, 70, 100, 30, $BS_PUSHLIKE)
    GUISetState(@SW_SHOW)
EndFunc   ;==>Database_Window

Func ScenarioPressed()
    _GUICtrlButton_Destroy($Button_2) ;Remove $Button_2
EndFunc   ;==>ScenarioPressed

Func DataPressed()
    _GUICtrlButton_Destroy($Button_1) ;Remove $Button_1
EndFunc   ;==>DataPressed

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Wow thanks I didn't realize I had to "control" the message handler depending on what GUI the control is residing on...that is good info to know. I assume you are using an index value of "0" because (according to the "help file")...$array[0] = 0 or Event ID or Control ID...that is how the message handler knows which control in which GUI sent the click message, correct?

I will look at this in more depth tomorrow when I have time but it makes more sense now...thanks a lot for your help! Informative replies...

Link to comment
Share on other sites

You could simplify this script greatly by not using multiple GUIs at all, to do what you're doing, you can just put a graphic in the same location where you have the 2nd and 3rd GUIs and it would probably look the same. That would eliminate any switch statments that depend upon which GUI is sending the message and you wouldn't have to worry about using the advanced mode that way.

Also, why are you deleting the button when you press the other button? If it's just so that no one can click on the button when you're doing something with the other button's actions, you could use GUICtrlSetState($Button_1, $GUI_DISABLE) and this will make the button unusable until you reenable it with GUICtrlSetState($Button_1, $GUI_ENABLE).

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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