Sign in to follow this  
Followers 0
Andrew Sparkes

Questions about right click menus in tree views

3 posts in this topic

#1 ·  Posted (edited)

I have a little account manager type program that I wrote, and I was pondering ways of adding a way to remove an account from the treelist view. I thought a right click->Delete option would be good, but am unsure about how to implement it. I searched through the forums and found few things of interest... I took a look at CMenu and thats too complicated for a newb like me to modify to my liking. I could add another button that would delete the section found in the username input box, but thats too simple, I'd rather take the hard way and learn something. Any helpful insight?

On another subject. I just learned autoit's syntax yesterday and have stuck to it for quite a few hours, but I am sure this code could be optimized or shortened. I ask anyone to take a look and see where my errors are and explain how to fix them or speed them up. Thanks.

EDIT: Note: the +35's you see in the top values were from experimenting with a banner picture at the top. The pictures wont load, as they arent included, but the code is the main subject, so no matter.

Another EDIT: Fixed right click in tree view problem. Done and over with. I now have another area of problem. Look at the testfunc code below (towards the bottom). GUICtrlRead($accountsmain) is supposed to give me the currently selected treeviewitem in the form of controlid, but it gives a different value than I expected. I assigned the treeviewitems to $1, $2, $3 and so on for as many as needed, but the GUICtrlRead($accountsmain) returns 20, 23, 26, 29 and so on, incrementing by 3 each time. The first section, first key is 21, first section, second key is 22 and it counts that way... How is this happening? I hope it's an easily fixable problem....

My solution was to find an equation to turn 20 into 1 and 23 into 2, and got this line: $sectionnames[(GUICtrlRead($accountsmain)-17)/3]

I currently have 11 sections and 2 keys apiece, and when I try to remove accounts, it does two flawlessly and on the third it crashes saying this error:

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

IniDelete($accountsini,$sectionnames[(GUICtrlRead($accountsmain)-17)/3])

IniDelete($accountsini,^ ERROR

ex. of naming convention

item1 - 20

item1a -21

item1b - 22

item2 - 23

item2a - 24

item2b - 25

Code:

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:        Sparkes
;
; Script Function:
;   Account Manager
;
; ----------------------------------------------------------------------------

#include <GUIConstants.au3>
$accountsini = "accounts.ini"
$error = "404 Error: File Not Found"
GUICreate("Account Manager", 210, 360 + 35 + 25, 100, 100)
opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "closewindow")
HotKeySet("{Esc}", "closewindow")
$filemenu = GUICtrlCreateMenu ("&File")
$editmenu = GUICtrlCreateMenu ("&Edit")
$updateaccount = GUICtrlCreateMenuitem("Update Account",$editmenu)
$deleteaccount = GUICtrlCreateMenuItem("Delete Account",$editmenu)
$testfunction = GUICtrlCreateMenuItem("TestFunc",$filemenu)
$openini = GUICtrlCreateMenuitem("Open Accounts File",$filemenu)
$exit = GUICtrlCreateMenuItem("Exit",$filemenu)
GUICtrlSetOnEvent($exit,"closewindow")
GUICtrlSetOnEvent($deleteaccount,"delacc")
GUICtrlSetOnEvent($updateaccount,"writetoini")
GUICtrlSetOnEvent($openini,"changeinifile")
GUICtrlSetOnEvent($testfunction,"testfunc")
GUICtrlCreatePic("accmanbanner.gif", 0, 0, 210, 35)
$accountsmain = GUICtrlCreateTreeView(5, 5 + 35, 200, 250, BitOR($tvs_hasbuttons, $tvs_haslines, $tvs_linesatroot, $tvs_disabledragdrop, $tvs_showselalways), $ws_ex_clientedge)
$accounts = GUICtrlCreateTreeViewItem("Accounts", $accountsmain)
$accountsmaincontext = GUICtrlCreateContextMenu($accountsmain)
$testfunccontext=GUICtrlCreateMenuitem("Remove",$accountsmaincontext)
GUICtrlSetOnEvent($testfunccontext,"removefromini")
$email = GUICtrlCreateInput("Email Address", 5, 260 + 35, 200, 20)
$password = GUICtrlCreateInput("Password", 5, 290 + 35, 92, 20)
$username = GUICtrlCreateInput("Username", 113, 290 + 35, 92, 20)
GUICtrlCreatePic("sparkes.gif", 5, 312 + 35, 100, 50)
$addbutton = GUICtrlCreateButton("Update", 140, 320 + 35, 60, 20)
GUICtrlSetOnEvent($addbutton, "writetoini")
Dim $sectionnames
Update()
GUISetState()
Func Update()
    $sectionnames = IniReadSectionNames($accountsini)
    If FileExists($accountsini) Then
        $g = 1
        For $i = 1 To $sectionnames[0]
            $g = GUICtrlCreateTreeViewItem($sectionnames[$i], $accounts)
            $emailfromsection = IniRead($accountsini, $sectionnames[$i], "email", $error)
            $passwordfromsection = IniRead($accountsini, $sectionnames[$i], "password", $error)
            GUICtrlCreateTreeViewItem($emailfromsection, $g)
            GUICtrlCreateTreeViewItem($passwordfromsection, $g)
            $g = $g + 1
        Next
    Else
        MsgBox(0, "", "Error")
    EndIf
EndFunc ;==>Update
GUICtrlSetState($accounts, $GUI_EXPAND)
GUISetState()
While 1
    Sleep(1000)
WEnd
Func closewindow(); close the window
    Exit
EndFunc ;==>closewindow
Func writetoini()
    IniWrite($accountsini, GUICtrlRead($username), "email", GUICtrlRead($email))
    IniWrite($accountsini, GUICtrlRead($username), "password", GUICtrlRead($password))
    MsgBox(0, "Account Updated", "Account Successfully Updated.")
    Refresh()
EndFunc ;==>writetoini
Func Refresh()
    GUICtrlDelete($accountsmain)
    $accountsmain = GUICtrlCreateTreeView(5, 5 + 35, 200, 250, BitOR($tvs_hasbuttons, $tvs_haslines, $tvs_linesatroot, $tvs_disabledragdrop, $tvs_showselalways), $ws_ex_clientedge)
    $accounts = GUICtrlCreateTreeViewItem("Accounts", $accountsmain)
    $sectionnames = IniReadSectionNames($accountsini)
    If FileExists($accountsini) Then
        $g = 1
        For $i = 1 To $sectionnames[0]
            $g = GUICtrlCreateTreeViewItem($sectionnames[$i], $accounts)
            $emailfromsection = IniRead($accountsini, $sectionnames[$i], "email", $error)
            $passwordfromsection = IniRead($accountsini, $sectionnames[$i], "password", $error)
            GUICtrlCreateTreeViewItem($emailfromsection, $g)
            GUICtrlCreateTreeViewItem($passwordfromsection, $g)
            $g = $g + 1
        Next
    Else
        MsgBox(0, "", "Error")
    EndIf
    $accountsmaincontext = GUICtrlCreateContextMenu($accountsmain)
    $testfunccontext=GUICtrlCreateMenuitem("Remove",$accountsmaincontext)
    GUICtrlSetOnEvent($testfunccontext,"removefromini")
    GUICtrlSetState($accounts, $GUI_EXPAND)
    GUISetState()
EndFunc ;==>Refresh
Func delacc()
EndFunc
Func changeinifile()
EndFunc
Func testfunc()
;Msgboxs for debugging purposes.
;MsgBox(0,"","read: "&$sectionnames[(GUICtrlRead($accountsmain)-17)/3])
;MsgBox(0,"","read: "&GUICtrlRead($accountsmain))
EndFunc
Func removefromini()
    IniDelete($accountsini,$sectionnames[(GUICtrlRead($accountsmain)-17)/3])
    MsgBox(0, "Account Deleted", "Account Successfully Deleted.")
    Refresh()
EndFunc

Thanks again.

EDIT2: Code Update

Edited by Andrew Sparkes

---Sparkes.

Share this post


Link to post
Share on other sites



I'm also interested. It seems context menues will not trigger on controles. Is there a way to impliment a right click menu for a tree view?

Share this post


Link to post
Share on other sites

They will, see above code.

$accountsmaincontext = GUICtrlCreateContextMenu($accountsmain)
$testfunccontext=GUICtrlCreateMenuitem("Remove",$accountsmaincontext)
GUICtrlSetOnEvent($testfunccontext,"removefromini")

Explained:

$[ The name of the context menu]=GUICreateContextMenu($[ The name of the control you want it on])
$[ The name of an item on the context menu]=GUICtrlCreateMenuitem("[ Item name shown on context menu]",$[ The context menu it is on])
GUICtrlSetOnEvent($[ The name of the context menu item ],"[ The name of its called function]")

I hope that helps. PM for questions about it. I don't know much about it, but I will share what I do know. Thanks.


---Sparkes.

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