Jump to content

How do I create multiple controls with context menus for each to delete


Recommended Posts

I'm trying to create multiple controls (2 or more) where each control has its own context menu that I can use to delete specific controls. I can do this somewhat but I run into trouble with the control handles of the buttons. My problem is begins when I create two buttons, then only the firstly created button has a context menu, and then when I right click to delete that button, it deletes the second button instead of the first button. Now I now this has to do with control handles. I think I need to incorporate arrays into the handles but I have no clue how because I don't have much array knowledge. I'd appreciate it very much if someone could suggest how I could use arrays to solve this problem or help me implement whatever solution works. God Bless all. :)

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include <anygui.au3>

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Many credits to martin for solving problems
; with the code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


$bool = True

$gui = GUICreate("GUI", 377, 377)
GUISetBkColor(0xFF0000)
$button = GUICtrlCreateButton("Click me", 70, 277, 70)
GUISetState()

Global $pic = -1, $menu = -1, $item1 = -1, $pic2 = -1;<----- Global declaration so th evariables are available outside of the function

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case $button
            Test()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $item1
            ConsoleWrite("Clicked delete button" & @CRLF)
            GUISwitch($target)
            GUIDelete($pic[2])
            $pic = -1
            $menu = -1
            $item1 = -1
    EndSwitch
WEnd



Func Test()
    Global $target = _GuiTarget("GUI");<--I moved this out of the function. You only need to create the $target once for a particular window.
;~     If $pic = -1 Then
        If $bool = True Then
            $pic = _TargetAddButton("Button 1", 77, 77, 77, 77, -1, -1, $target)
        Else
            $pic = _TargetAddButton("Button 2", 177, 77, 77, 77, -1, -1, $target)
        EndIf
    GUISetState(); to show the newly created child window
;~     EndIf
    If $menu = -1 Then $menu = GUICtrlCreateContextMenu($pic[0])
    If $item1 = -1 Then $item1 = GUICtrlCreateMenuItem("Delete me", $menu)

    $bool = False
    GUISwitch($gui)
EndFunc ;==>Test
Edited by Hypertrophy
Link to comment
Share on other sites

That looks familiar.

#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include "G:\anygui\anygui.au3"

$bool = True

$gui = GUICreate("GUI", 377, 377)
GUISetBkColor(0xFF0000)
$button = GUICtrlCreateButton("Click me", 70, 277, 70)
GUISetState()
Global $target = _GuiTarget("GUI");<--I moved this out of the function AGAIN. You only need to create the $target once for a particular window.

Global $pic[2][3];allow up to 2 buttons so 2 lots of 3 variables
Global $menu[2][2];allow up to 2 menus each with 2 variables

$pic[0][0] = 0
$pic[1][0] = 0

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case 0

    Case $button
    Test()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $menu[0][1], $menu[1][1];if one of the menu items
    For $p = 0 To 1;find out which menu item was clicked
    If $msg[0] = $menu[$p][1] Then
    GUIDelete($pic[$p][2])
    $pic[$p][0] = 0;so we know it's free again 
    EndIf
    Next

    EndSwitch
WEnd



Func Test()
    Local $temppic, $n, $ib

    If $pic[0][0] = 0 Then
    $ib = 0
    ElseIf $pic[1][0] = 0 Then
    $ib = 1
    Else
    Return ;no more than 2 allowed
    EndIf


    $temppic = _TargetAddButton("Button " & $ib, $ib * 100 + 77, 77, 77, 77, -1, -1, $target)
    GUISetState(); to show the newly created child window

    For $n = 0 To 2
    $pic[$ib][$n] = $temppic[$n]
    Next

    $menu[$ib][0] = GUICtrlCreateContextMenu($pic[$ib][0])
    $menu[$ib][1] = GUICtrlCreateMenuItem("Delete me (" & $ib & ")", $menu[$ib][0])

    GUISwitch($gui)

EndFunc ;==>Test
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.
Link to comment
Share on other sites

That looks familiar.

#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include "G:\anygui\anygui.au3"

$bool = True

$gui = GUICreate("GUI", 377, 377)
GUISetBkColor(0xFF0000)
$button = GUICtrlCreateButton("Click me", 70, 277, 70)
GUISetState()
Global $target = _GuiTarget("GUI");<--I moved this out of the function AGAIN. You only need to create the $target once for a particular window.

Global $pic[2][3];allow up to 2 buttons so 2 lots of 3 variables
Global $menu[2][2];allow up to 2 menus each with 2 variables

$pic[0][0] = 0
$pic[1][0] = 0

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case 0

    Case $button
    Test()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $menu[0][1], $menu[1][1];if one of the menu items
    For $p = 0 To 1;find out which menu item was clicked
    If $msg[0] = $menu[$p][1] Then
    GUIDelete($pic[$p][2])
    $pic[$p][0] = 0;so we know it's free again 
    EndIf
    Next

    EndSwitch
WEnd



Func Test()
    Local $temppic, $n, $ib

    If $pic[0][0] = 0 Then
    $ib = 0
    ElseIf $pic[1][0] = 0 Then
    $ib = 1
    Else
    Return ;no more than 2 allowed
    EndIf


    $temppic = _TargetAddButton("Button " & $ib, $ib * 100 + 77, 77, 77, 77, -1, -1, $target)
    GUISetState(); to show the newly created child window

    For $n = 0 To 2
    $pic[$ib][$n] = $temppic[$n]
    Next

    $menu[$ib][0] = GUICtrlCreateContextMenu($pic[$ib][0])
    $menu[$ib][1] = GUICtrlCreateMenuItem("Delete me (" & $ib & ")", $menu[$ib][0])

    GUISwitch($gui)

EndFunc ;==>Test

Thank you so much this is exactly what I need. I appreciate the comments to as it helps me a lot to understand how the arrays are working in there. I appreciate all the time you have spent helping me and may God bless you. :)
Link to comment
Share on other sites

Why must there be Case 0 in the switch statement? When I take it out my gui is gone but is still running and when I leave it in my program goes to a function that was not even called?

The reason I put a Case 0 was that there are variables declared for controls which might be created later. Until the controls are crteated those variables will equate to zero, so if there is no event to handle and the return from GuiGetMsg is zero, the Case for the first unused variable will be executed. Case 0 at the start of the Switch traps the zero and does nothing.

The other way of dealing with this situation, and more common, is to initialise the variables to -1 say. I prefer not to because there might be a lot of initializing to do and so I add Case 0 to the start of my Switch sections instead.

If you get a function called that shouldn't be then there is some reason in your code but I can't guess what.

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.
Link to comment
Share on other sites

Wow, this Case 0 has messed up a lot of functions in my code. Depending on what I do it will go to different functions that have nothing to do with anything. I am positive now there is nothing wrong with my code. Is there any other way to not use Case 0 and use something else instead?

Would ContinueLoop after Case 0 work?

Edited by Hypertrophy
Link to comment
Share on other sites

You could try Removing the Case 0 and adding

Case Else
EndSwitch

at the bottom of the switch.

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

In that case I would think you should just comment out the Case 0

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

Wow, this Case 0 has messed up a lot of functions in my code. Depending on what I do it will go to different functions that have nothing to do with anything. I am positive now there is nothing wrong with my code. Is there any other way to not use Case 0 and use something else instead?

I don't see how Case 0 can cause a problem. Certainly I don't see how it can cause functions to be called that shouldn't be; the very reason for it is to prevent that. Case 0 should have no functions after it as in my post above. Can you give an example of your code?

The alternative is to set any variable to -1 say untill it is used for the ID of a control.

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.
Link to comment
Share on other sites

You could try Removing the Case 0 and adding

Case Else

EndSwitch

at the bottom of the switch.

Before even encountering this problem I am already using that to do something else.

That won't work!

If you declare a variable like this

Global $something

then $something will equate to 0

if you have a structure like this

Switch $var1

Case $something

_CalculatetheMeaningofLife()

Case Else

_forgetthatfirstinstruction()

EndSwitch

then when there is no event and the return from GuiGetMsg is 0 then Case $something will be true because $something is equal to $var1 and you will execute a function you don't want to

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.
Link to comment
Share on other sites

In that case I would think you should just comment out the Case 0

Can't do that either, notice what happens to the GUI when Case 0 is commeneted out.

#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include <anygui.au3>


$gui = GUICreate("GUI", 477, 377)
GUISetBkColor(0xFF0000)
$button = GUICtrlCreateButton("Click me", 70, 277, 70)
GUISetState()
Global $target = _GuiTarget("GUI");<--I moved this out of the function AGAIN. You only need to create the $target once for a particular window.

Global $pic[4][3];allow up to 2 buttons so 2 lots of 3 variables
Global $menu[4][3];allow up to 2 menus each with 2 variables

$pic[0][0] = 0
$pic[1][0] = 0
$pic[2][0] = 0
$pic[3][0] = 0

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
;~         Case 0
    Case $button
        Test()
    Case $GUI_EVENT_CLOSE
        Exit
    Case $menu[0][1], $menu[1][1], $menu[2][1], $menu[3][1]       ;if one of the menu items
        For $p = 0 To 3;find out which menu item was clicked
            If $msg[0] = $menu[$p][1] Then
                GUIDelete($pic[$p][2])
                $pic[$s][0] = 0;so we know it's free again
            EndIf
        Next
    Case $menu[0][2]
        For $s = 0  To 3
            GUIDelete($pic[$p][2])
            $pic[$s][0] = 0;so we know it's free again
        Next
    EndSwitch
WEnd



Func Test()
    Local $temppic, $n, $ib

    If $pic[0][0] = 0 Then
        $ib = 0
    ElseIf $pic[1][0] = 0 Then
        $ib = 1
    ElseIf $pic[2][0] = 0 Then
        $ib = 2
    ElseIf $pic[3][0] = 0 Then
        $ib = 3
    Else
        Return ;no more than 2 allowed
    EndIf


    $temppic = _TargetAddButton("Button " & $ib, $ib * 100 + 77, 77, 77, 77, -1, -1, $target)
    GUISetState(); to show the newly created child window

    For $n = 0 To 2
    $pic[$ib][$n] = $temppic[$n]
    Next

    $menu[$ib][0] = GUICtrlCreateContextMenu($pic[$ib][0])
    $menu[$ib][1] = GUICtrlCreateMenuItem("Delete me (" & $ib & ")", $menu[$ib][0])
    $menu[$ib][2] = GUICtrlCreateMenuItem("Delete all", $menu[$ib][0])

    GUISwitch($gui)

EndFunc ;==>Test
Edited by Hypertrophy
Link to comment
Share on other sites

Then it's back in martins very capable hands becuase I don't have the time or inclination to test the code today.

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

Can't do that either, notice what happens to the GUI when Case 0 is commeneted out.

#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include <anygui.au3>


$gui = GUICreate("GUI", 477, 377)
GUISetBkColor(0xFF0000)
$button = GUICtrlCreateButton("Click me", 70, 277, 70)
GUISetState()
Global $target = _GuiTarget("GUI");<--I moved this out of the function AGAIN. You only need to create the $target once for a particular window.

Global $pic[4][3];allow up to 2 buttons so 2 lots of 3 variables
Global $menu[4][3];allow up to 2 menus each with 2 variables

$pic[0][0] = 0
$pic[1][0] = 0
$pic[2][0] = 0
$pic[3][0] = 0

While 1
 $msg = GUIGetMsg(1)
 Switch $msg[0]
;~ Case 0
 Case $button
        Test()
 Case $GUI_EVENT_CLOSE
        Exit
 Case $menu[0][1], $menu[1][1], $menu[2][1], $menu[3][1] ;if one of the menu items
        For $p = 0 To 3;find out which menu item was clicked
            If $msg[0] = $menu[$p][1] Then
                GUIDelete($pic[$p][2])
                $pic[$s][0] = 0;so we know it's free again
            EndIf
        Next
    Case $menu[0][2]
        For $s = 0 To 3
            GUIDelete($pic[$p][2])
            $pic[$s][0] = 0;so we know it's free again
        Next
 EndSwitch
WEnd



Func Test()
 Local $temppic, $n, $ib

 If $pic[0][0] = 0 Then
        $ib = 0
    ElseIf $pic[1][0] = 0 Then
        $ib = 1
    ElseIf $pic[2][0] = 0 Then
        $ib = 2
    ElseIf $pic[3][0] = 0 Then
        $ib = 3
    Else
        Return ;no more than 2 allowed
 EndIf


 $temppic = _TargetAddButton("Button " & $ib, $ib * 100 + 77, 77, 77, 77, -1, -1, $target)
 GUISetState(); to show the newly created child window

 For $n = 0 To 2
 $pic[$ib][$n] = $temppic[$n]
 Next

 $menu[$ib][0] = GUICtrlCreateContextMenu($pic[$ib][0])
 $menu[$ib][1] = GUICtrlCreateMenuItem("Delete me (" & $ib & ")", $menu[$ib][0])
 $menu[$ib][2] = GUICtrlCreateMenuItem("Delete all", $menu[$ib][0])

 GUISwitch($gui)

EndFunc ;==>Test

But I don't see why you want to comment it out. What is the problem you get by leaving it there?

Of course it all goes wrong if you comment it out; I tried to explain it in post #5 and again in #12.

BTW you have $s instead of $p

Case $menu[0][2]
        For $s = 0 To 3
            GUIDelete($pic[$p][2])
            $pic[$s][0] = 0;so we know it's free again<--------should be $p
        Next
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.
Link to comment
Share on other sites

But I don't see why you want to comment it out. What is the problem you get by leaving it there?

Of course it all goes wrong if you comment it out; I tried to explain it in post #5 and again in #12.

BTW you have $s instead of $p

Case $menu[0][2]
        For $s = 0 To 3
            GUIDelete($pic[$p][2])
            $pic[$s][0] = 0;so we know it's free again<--------should be $p
        Next

I think I fixed it by adding ContinueLoop as shown below? Is this possible?

Case 0
     ContinueLoop
Link to comment
Share on other sites

I think I fixed it by adding ContinueLoop as shown below? Is this possible?

Case 0
 ContinueLoop

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