Sign in to follow this  
Followers 0
Pharo

Guictrlcreate buttons and delete not working how I wanted them too

2 posts in this topic

I'm trying to make the below code work. I've been going through the help files which allowed me to get it to somewhat work. However if you hit the show all button 2 times in a row it stops working correctly. The 6 buttons "1" should individually go away when you click them. The "show all" and "hide all" buttons should display all or none of them. Once you have hit the show all 2 times in a row it stops working and may fail under other scenarios. I am sure I have put a lot of unnecessary lines in the code so some tips on shortening it would be much appreciated.

CODE
#include <GUIConstants.au3>

GUICreate ( "My GUI delete control", 300,400,0,0)

$1Y=GUICtrlCreateButton("show all", 0,50,50,20 )

$11=GUICtrlCreateButton("1", 50,50,20,20 )

$12=GUICtrlCreateButton("1", 70,-1,20,20 )

$13=GUICtrlCreateButton("1", 90,-1,20,20 )

$14=GUICtrlCreateButton("1", 110,-1,20,20 )

$15=GUICtrlCreateButton("1", 130,-1,20,20 )

$16=GUICtrlCreateButton("1", 150,-1,20,20 )

$1N=GUICtrlCreateButton("hide all", 170,50,50,20 )

GUISetState ()

Do

$msg = GUIGetMsg()

if $msg = $11 then GUICtrlDelete($11)

if $msg = $12 then GUICtrlDelete($12)

if $msg = $13 then GUICtrlDelete($13)

if $msg = $14 then GUICtrlDelete($14)

if $msg = $15 then GUICtrlDelete($15)

if $msg = $16 then GUICtrlDelete($16)

if $msg = $1Y then

$11=GUICtrlCreateButton("1", 50,50,20,20 )

$12=GUICtrlCreateButton("1", 70,-1,20,20 )

$13=GUICtrlCreateButton("1", 90,-1,20,20 )

$14=GUICtrlCreateButton("1", 110,-1,20,20 )

$15=GUICtrlCreateButton("1", 130,-1,20,20 )

$16=GUICtrlCreateButton("1", 150,-1,20,20 )

EndIf

if $msg = $1N then

GUICtrlDelete($11)

GUICtrlDelete($12)

GUICtrlDelete($13)

GUICtrlDelete($14)

GUICtrlDelete($15)

GUICtrlDelete($16)

EndIf

Until $msg = $GUI_EVENT_CLOSE

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'm trying to make the below code work. I've been going through the help files which allowed me to get it to somewhat work. However if you hit the show all button 2 times in a row it stops working correctly. The 6 buttons "1" should individually go away when you click them. The "show all" and "hide all" buttons should display all or none of them. Once you have hit the show all 2 times in a row it stops working and may fail under other scenarios. I am sure I have put a lot of unnecessary lines in the code so some tips on shortening it would be much appreciated.

CODE
#include <GUIConstants.au3>

GUICreate ( "My GUI delete control", 300,400,0,0)

$1Y=GUICtrlCreateButton("show all", 0,50,50,20 )

$11=GUICtrlCreateButton("1", 50,50,20,20 )

$12=GUICtrlCreateButton("1", 70,-1,20,20 )

$13=GUICtrlCreateButton("1", 90,-1,20,20 )

$14=GUICtrlCreateButton("1", 110,-1,20,20 )

$15=GUICtrlCreateButton("1", 130,-1,20,20 )

$16=GUICtrlCreateButton("1", 150,-1,20,20 )

$1N=GUICtrlCreateButton("hide all", 170,50,50,20 )

GUISetState ()

Do

$msg = GUIGetMsg()

if $msg = $11 then GUICtrlDelete($11)

if $msg = $12 then GUICtrlDelete($12)

if $msg = $13 then GUICtrlDelete($13)

if $msg = $14 then GUICtrlDelete($14)

if $msg = $15 then GUICtrlDelete($15)

if $msg = $16 then GUICtrlDelete($16)

if $msg = $1Y then

$11=GUICtrlCreateButton("1", 50,50,20,20 )

$12=GUICtrlCreateButton("1", 70,-1,20,20 )

$13=GUICtrlCreateButton("1", 90,-1,20,20 )

$14=GUICtrlCreateButton("1", 110,-1,20,20 )

$15=GUICtrlCreateButton("1", 130,-1,20,20 )

$16=GUICtrlCreateButton("1", 150,-1,20,20 )

EndIf

if $msg = $1N then

GUICtrlDelete($11)

GUICtrlDelete($12)

GUICtrlDelete($13)

GUICtrlDelete($14)

GUICtrlDelete($15)

GUICtrlDelete($16)

EndIf

Until $msg = $GUI_EVENT_CLOSE

You need some way to keep track of buttons which have been deleted, then only create a button if it no longer exist. The way you have done it the buttons are created again even if they weren't deleted so the reference to the original button is lost but the old button is still there. Then when you delete the button you see the old one which was never deleted.

It might be better to hide or show the buttons rather than to delete then recreate and then you don't have to worry about whether they were deleted or not. Also, I would recommend using an array for the buttons to simplify your code.

#include <GUIConstants.au3>
Dim $btn[6]
GUICreate("My GUI delete control", 300, 400, 0, 0)
$1Y = GUICtrlCreateButton("show all", 0, 50, 50, 20)
For $n = 0 To 5
    $btn[$n] = GUICtrlCreateButton("1", 50 + $n * 20, 50, 20, 20)
Next
$1N = GUICtrlCreateButton("hide all", 170, 50, 50, 20)
GUISetState()


Do
    $msg = GUIGetMsg()
    For $n = 0 To 5
        If $msg = $btn[$n] Then
            GUICtrlSetState($btn[$n], $GUI_HIDE)
        EndIf
    Next

    
    If $msg = $1Y Then
        For $n = 0 To 5
            GUICtrlSetState($btn[$n], $GUI_SHOW)
        Next

    EndIf
    If $msg = $1N Then
        For $n = 0 To 5
            GUICtrlSetState($btn[$n], $GUI_HIDE)
        Next
    EndIf
Until $msg = $GUI_EVENT_CLOSE
Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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