DrLarch Posted November 24, 2012 Share Posted November 24, 2012 Maybe this is a picky issue, but no idea why the treeview behaves this way. TreeBuild() is the func that sets up the tree, but buttons don't appear right away. I can get around this behavior by expanding & collapsing everything first this way:_GUICtrlTreeView_Expand($hTV,"",True)_GUICtrlTreeView_Expand($hTV,"",False)...but why should this be necessary? Is there something I'm not doing right?(Apologies: Code not super-clean, but it works up to this point... )(input txt file is attached, script needs this to have something to import)expandcollapse popup#include <Array.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <GUIListBox.au3> #include <StaticConstants.au3> #include <TreeViewConstants.au3> #include <WindowsConstants.au3> #include <GuiConstants.au3> #include <GuiTreeView.au3> ;#include <WinAPI.au3> #include <GuiListView.au3> #include <File.au3> Opt("GUIOnEventMode", 1) Global $hGUI, $hTV, $idTV, $hLV, $hToCount, $AddressArray, $AddorEdit, $AddEdit, $NumberInputText, $FavCheck, $FavTest,$hTVSelectedItemText Global $TreeRebuild = 1;Default is yes, rebuild the tree Global $AddressFile = (@ScriptDir & "\CPAddresses.txt") Global $FavAdd = 1 $hGUI = GUICreate("Form1", 486, 398, 197, 124) GUISetOnEvent($GUI_EVENT_CLOSE, "hGUIClose") GUISetOnEvent($GUI_EVENT_MINIMIZE, "hGUIMinimize") GUISetOnEvent($GUI_EVENT_MAXIMIZE, "hGUIMaximize") GUISetOnEvent($GUI_EVENT_RESTORE, "hGUIRestore") $hTV = _GUICtrlTreeView_Create($hGUI, 224, 32, 249, 321, -1, BitOR($WS_EX_CLIENTEDGE,$WS_EX_STATICEDGE,$TVS_HASBUTTONS)) $idTV = _WinAPI_GetDlgCtrlID($hTV) $hLV = _GUICtrlListView_Create($hGUI, "Name |Number", 8, 32, 201, 279, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS)) _GUICtrlListView_SetExtendedListViewStyle($hLV, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_TRACKSELECT)) $Label1 = GUICtrlCreateLabel("To:", 8, 8, 29, 24) GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif") $Label2 = GUICtrlCreateLabel("Address Book:", 224, 8, 121, 24) GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif") $Send = GUICtrlCreateButton("Send", 56, 352, 97, 33) GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif") GUICtrlSetOnEvent(-1, "bSend") $Button2 = GUICtrlCreateButton("Delete Selected", 8, 318, 97, 25) GUICtrlSetOnEvent(-1, "bDeleteItem") $bClear = GUICtrlCreateButton("Clear All", 112, 318, 97, 25) GUICtrlSetOnEvent(-1, "bClearClick") $bFavorite = GUICtrlCreateButton("Toggle Fav", 224, 360, 81, 25) GUICtrlSetTip(-1, "Toggles Favorites On/Off") GUICtrlSetOnEvent(-1, "bFavorite") $bAddrAdd = GUICtrlCreateButton("Add", 368, 360, 49, 25) GUICtrlSetOnEvent(-1, "bAddrAdd") $bAddrDelete = GUICtrlCreateButton("Delete", 424, 360, 49, 25) GUICtrlSetOnEvent(-1, "bAddrDelete") $Button7 = GUICtrlCreateButton("Edit", 312, 360, 49, 25) GUICtrlSetOnEvent(-1, "bAddrEdit") $Label3 = GUICtrlCreateLabel("(Double-click to add...)", 352, 13, 111, 17) GUISetState(@SW_SHOW) GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY") GUISetState() Startup() Do Sleep(100) Until GUIGetMsg() = $GUI_EVENT_CLOSE Func _WM_NOTIFY($hWnd, $Msg, $wParam, $lParam) Local $tagNMHDR = "int hwndFrom; int idFrom; int code" Switch $wParam Case $idTV Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam) If @error Then Return If DllStructGetData($tNMHDR, "code") = $NM_DBLCLK Then $hTVItem = _GUICtrlTreeView_GetText($hTV, _GUICtrlTreeView_GetSelection($hTV)) $hTVItemArray = StringSplit ($hTVItem, ":");Split the array string into Name/Number fields $ItemExists = _GUICtrlListView_FindText($hLV, $hTVItemArray[1]);Check if the item has already been added. If $hTVItemArray[0] > 1 And $ItemExists = -1 Then _GUICtrlListView_AddItem($hLV, $hTVItemArray[1]) _GUICtrlListView_AddSubItem($hLV, 0 + $hToCount, StringStripWS($hTVItemArray[2],8), 1) $hToCount = $hToCount + 1 _GUICtrlListView_SimpleSort($hLV,False, 0) EndIf ;~ Tried to set already present item as the sole selection, but doesn't work - not a big deal... ;~ If $hTVItemArray[0] > 1 And $ItemExists <> -1 Then ;~ ;_GUICtrlListView_SetItemSelected($hLV, -1) ;~ _GUICtrlListView_SetItemSelected($hLV, $ItemExists) ;~ EndIf EndIf EndSwitch $tNMHDR = 0 Return $GUI_RUNDEFMSG EndFunc ;==>_WM_NOTIFY Func bClearClick() _GUICtrlListView_DeleteAllItems($hLV) $hToCount = 0 EndFunc Func bFavorite() $FavAdd = 0 $hTVSelectedItem = _GUICtrlTreeView_GetSelection($hTV) ;MsgBox(16,"Fax Address Book","$hTVSelectedItem: " & $hTVSelectedItem) If $hTVSelectedItem = 0 Then MsgBox(16,"Fax Address Book","Nothing selected!") Else $hTVSelectedItemText = _GUICtrlTreeView_GetText($hTV, $hTVSelectedItem) $FavTest = _ArraySearch($AddressArray,$hTVSelectedItemText & "§") $ItemPos = _ArraySearch($AddressArray,$hTVSelectedItemText); Search for item in array - if not found sets var to -1(ie. a parent at this point?) If $FavTest = -1 and $ItemPos <> -1 Then ;Didn't find entry in array with "§" at the end & checked if item is a parent (shouldn't be) ;MsgBox(0,"test","Not a favorite, toggling...") ;$ItemPos = _ArraySearch($AddressArray,$hTVSelectedItemText) $AddressArray[$ItemPos] = $AddressArray[$ItemPos] & "§"; Add favorite marker: "§" ;_GUICtrlTreeView_Expand($hTV, $hTVFav, True);Expand Favorites when new Fav added... not working all the TimerDiff ;Trying this (below): $FavAdd = 1 ElseIf $FavTest <> -1 Then ;Item is already a favorite (and presumably a child...) ;MsgBox(0,"test","Already a favorite, toggling...") $AddressArray[$FavTest] = StringTrimRight($AddressArray[$FavTest],1); Trim favorite marker: "§" Else ;Must be a parent item, so do nothing... ;MsgBox(0,"test","Parent item?") $TreeRebuild = 0 EndIf If $TreeRebuild = 1 Then ;No need to rebuild tree if the item selected was a parent... ;MsgBox(0,"test","Rebuilding tree...") TreeRebuild() EndIf $TreeRebuild = 1;Set $TreeRebuild on for next time... ;_ArrayDisplay($AddressArray); For testing EndIf EndFunc Func bAddrAdd() $AddorEdit = 1 Add_EditGUI() EndFunc Func bAddrEdit() $TreeRebuild = 1 $hTVSelectedItem = _GUICtrlTreeView_GetSelection($hTV) If $hTVSelectedItem = 0 Then MsgBox(16,"Fax Address Book","Nothing selected!") Else $hTVSelectedItemText = _GUICtrlTreeView_GetText($hTV, $hTVSelectedItem) $FavTest = _ArraySearch($AddressArray,$hTVSelectedItemText & "§");$FavTest will be -1 if the item is NOT found marked as a favorite $ItemPos = _ArraySearch($AddressArray,$hTVSelectedItemText); Search array - if not found sets var to -1(must be a parent) ;MsgBox(0,"Fax Address Book","$FavTest: " & $FavTest & @CRLF & "$ItemPos: " & $ItemPos) If $FavTest = -1 and $ItemPos = -1 Then;Item must be a parent $TreeRebuild = 0 EndIf EndIf If $TreeRebuild <> 0 Then $AddorEdit = 2;Set Edit GUI to import Name, Number and Favorite status Add_EditGUI() EndIf EndFunc Func bAddrDelete();Delete an item from the tree view $KillMe = _GUICtrlTreeView_GetSelection($hTV) $KillMeText = _GUICtrlTreeView_GetText($hTV, $KillMe) $KillMePos = _ArraySearch($AddressArray,$KillMeText) If $KillMePos = -1 Then;Text not found, must a favorite, search for it as a favorite $KillMePos = _ArraySearch($AddressArray,$KillMeText & "§") EndIf ;_ArrayDisplay($AddressArray) ;MsgBox(0,"test","$KillMePos: " & $KillMePos) If $KillMePos <> -1 Then _ArrayDelete($AddressArray, $KillMePos) $AddressArray [0] = ($AddressArray [0] -1);Modify the array count in Row 0 TreeRebuild() EndIf ;_ArrayDisplay($AddressArray) EndFunc Func bDeleteItem() _GUICtrlListView_DeleteItemsSelected($hLV) $hToCount = _GUICtrlListView_GetItemCount($hLV) EndFunc Func bSend() EndFunc Func hGUIClose() ;_ArrayDisplay($AddressArray) _ArrayDelete($AddressArray[0], 0);Delete the array index count from row 0... _FileWriteFromArray($AddressFile, $AddressArray,1) Exit EndFunc Func hGUIMaximize() EndFunc Func hGUIMinimize() EndFunc Func hGUIRestore() EndFunc Func TreeBuild() Global $ParentItemsCount = 1 _ArraySort($AddressArray) ;_ArrayDisplay($AddressArray);Testing only ;This section builds the index items first (Favorites, & first index character like "A", "B", etc)----- For $i = 1 to $AddressArray[0] $FirstChr = StringUpper(StringLeft($AddressArray[$i],1)) $FirstChrExists = _GUICtrlTreeView_FindItem($hTV, $FirstChr);Test if the index has already been added. If $FirstChrExists = 0 Then; Add the index if it doesn't already exist. Assign("$hTV" & $FirstChr,_GUICtrlTreeView_Add($hTV, 0, $FirstChr)) $ParentItemsCount = $ParentItemsCount + 1 EndIf Next ;Sort the tree and then add Favorites as first item---------------------------------------------------- _GUICtrlTreeView_Sort($hTV) Global $hTVFav = _GUICtrlTreeView_AddFirst($hTV, 0, "Favorites") ;$hTVFavHandle = _GUICtrlTreeView_GetParentHandle($hTV,$hTVSelectedItem) ;Find the Favorites and add---------------------------------------------------------------------------- $hTV_FavFound = _ArrayFindAll($AddressArray, "§", 0, $AddressArray[0], 0, 1) ;_ArrayDisplay($hTV_FavFound);Testing only If $hTV_FavFound <> -1 Then For $i = 0 to _ArrayMaxIndex($hTV_FavFound) _GUICtrlTreeView_AddChild($hTV, $hTVFav, StringTrimRight($AddressArray[$hTV_FavFound[$i]], 1)) Next EndIf ;Then the subitems are added to the tree--------------------------------------------------------------- For $i = 1 to $AddressArray[0] $FirstChr = StringUpper(StringLeft($AddressArray[$i],1)) If StringRight($AddressArray[$i],1) = "§" Then _GUICtrlTreeView_AddChild($hTV, Eval("$hTV" & $FirstChr), StringTrimRight($AddressArray[$i],1)) Else _GUICtrlTreeView_AddChild($hTV, Eval("$hTV" & $FirstChr), $AddressArray[$i]) EndIf Next EndFunc Func ReadAddressFile() _FileReadToArray($AddressFile,$AddressArray) EndFunc Func Startup() ReadAddressFile() TreeBuild() ;_GUICtrlTreeView_Expand($hTV,"",True);Why is this necessary? ;_GUICtrlTreeView_Expand($hTV,"",False) _GUICtrlTreeView_Expand($hTV, $hTVFav, True);Expand Favorites on startup EndFunc Func GetExpandedItems() Global $ExpandedParentArray[$ParentItemsCount + 1][2] ;MsgBox(0,"test","$ParentItemsCount: " & $ParentItemsCount) $hTVParent = $hTVFav ;For $k = 0 to ($ParentItemsCount - 1) For $k = 1 to ($ParentItemsCount) $hTVParentText = _GUICtrlTreeView_GetText($hTV, $hTVParent) $ExpandedParentArray [$k][0] = $hTVParentText $ExpandedParentArray [$k][1] = _GUICtrlTreeView_GetExpanded($hTV, $hTVParent) ;MsgBox(0,"test","$hTVParentText: " & $hTVParentText) $hTVParent = _GUICtrlTreeView_GetNextSibling($hTV,$hTVParent) ;_ArrayDisplay($ExpandedParentArray) Next $ExpandedParentArray [0][0] = $k ;_ArrayDisplay($ExpandedParentArray) EndFunc Func RestoreExpandedItems() For $l = 1 to ($ExpandedParentArray [0][0] -1) If $ExpandedParentArray[$l][1] = True Then $hTVParentFound = _GUICtrlTreeView_FindItem($hTV, $ExpandedParentArray[$l][0]) If $hTVParentFound <> 0 Then;Only try to expand the tree if the parent item is actually found! ;MsgBox(0,"test","Expanding: " & _GUICtrlTreeView_GetText($hTV, $hTVParentFound)) _GUICtrlTreeView_Expand($hTV, $hTVParentFound) EndIf If $FavAdd = 1 Then _GUICtrlTreeView_Expand($hTV, $hTVFav, True);Expand Favorites on startup EndIf EndIf Next EndFunc Func TreeRebuild() GetExpandedItems() _GUICtrlTreeView_DeleteAll($hTV) TreeBuild() RestoreExpandedItems() EndFunc ;Add/Edit GUI--------------------------------------------------------------- Func AddEditCancel() ;MsgBox(0,"test","Closing Edit GUI") GUIDelete($AddEdit) GUISwitch($hGUI) EndFunc Func AddEditClose() ;MsgBox(0,"test","Closing Edit GUI") GUIDelete($AddEdit) GUISwitch($hGUI) EndFunc Func Add_EditGUI() ;$AddorEdit: 1 = Add, 2 = Edit #Region ### START Koda GUI section ### Form=C:\Program Files\AutoIt3\koda_1.7.3.0\Forms\CallPilot Address Book Add-Edit.kxf $AddEdit = GUICreate("AddEdit", 338, 184, -1, -1, BitOR($WS_POPUPWINDOW, $WS_THICKFRAME, $WS_CAPTION),-1,$hGUI) ;$AddEdit = GUICreate("AddEdit", 338, 184, 204, 128, BitOR($WS_POPUPWINDOW, $WS_THICKFRAME, $WS_CAPTION),-1,$hGUI) GUISetOnEvent($GUI_EVENT_CLOSE, "AddEditCancel") $NameInput = GUICtrlCreateInput($hTVSelectedItemText, 16, 32, 153, 21) $NameLabel = GUICtrlCreateLabel("Name", 16, 12, 45, 18) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") $NumberInput = GUICtrlCreateInput($NumberInputText, 16, 80, 305, 21) $NumberLabel = GUICtrlCreateLabel("Number(s):", 16, 60, 80, 18) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") $InfoLabel1 = GUICtrlCreateLabel('Note: A single "Name" can include multiple fax', 16, 104, 277, 20) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $InfoLabel2 = GUICtrlCreateLabel("numbers. Just separate each number with a comma.", 16, 120, 311, 20) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $AddEditOK = GUICtrlCreateButton("OK", 256, 144, 65, 25) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") GUICtrlSetOnEvent(-1, "OKFunc") $AddEditCancel = GUICtrlCreateButton("Cancel", 184, 144, 65, 25) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") GUICtrlSetOnEvent(-1, "AddEditClose") $FavCheck = GUICtrlCreateCheckbox("Favorite?", 192, 29, 89, 25) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") GUICtrlSetState($FavCheck,$GUI_UNCHECKED) If $FavTest <> -1 Then GUICtrlSetState($FavCheck,$GUI_CHECKED) EndIf GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUISwitch($AddEdit) EndFuncCPAddresses.txt Link to comment Share on other sites More sharing options...
jmon Posted November 24, 2012 Share Posted November 24, 2012 Try this, it worked for me: expandcollapse popupFunc TreeBuild() Global $ParentItemsCount = 1 _ArraySort($AddressArray) _GUICtrlTreeView_BeginUpdate($hTV) ;_ArrayDisplay($AddressArray);Testing only ;This section builds the index items first (Favorites, & first index character like "A", "B", etc)----- For $i = 1 To $AddressArray[0] $FirstChr = StringUpper(StringLeft($AddressArray[$i], 1)) $FirstChrExists = _GUICtrlTreeView_FindItem($hTV, $FirstChr);Test if the index has already been added. If $FirstChrExists = 0 Then; Add the index if it doesn't already exist. Assign("$hTV" & $FirstChr, _GUICtrlTreeView_Add($hTV, 0, $FirstChr)) $ParentItemsCount = $ParentItemsCount + 1 EndIf Next ;Sort the tree and then add Favorites as first item---------------------------------------------------- _GUICtrlTreeView_Sort($hTV) Global $hTVFav = _GUICtrlTreeView_AddFirst($hTV, 0, "Favorites") ;$hTVFavHandle = _GUICtrlTreeView_GetParentHandle($hTV,$hTVSelectedItem) ;Find the Favorites and add---------------------------------------------------------------------------- $hTV_FavFound = _ArrayFindAll($AddressArray, "§", 0, $AddressArray[0], 0, 1) ;_ArrayDisplay($hTV_FavFound);Testing only If $hTV_FavFound <> -1 Then For $i = 0 To _ArrayMaxIndex($hTV_FavFound) _GUICtrlTreeView_AddChild($hTV, $hTVFav, StringTrimRight($AddressArray[$hTV_FavFound[$i]], 1)) Next EndIf ;Then the subitems are added to the tree--------------------------------------------------------------- For $i = 1 To $AddressArray[0] $FirstChr = StringUpper(StringLeft($AddressArray[$i], 1)) If StringRight($AddressArray[$i], 1) = "§" Then _GUICtrlTreeView_AddChild($hTV, Eval("$hTV" & $FirstChr), StringTrimRight($AddressArray[$i], 1)) Else _GUICtrlTreeView_AddChild($hTV, Eval("$hTV" & $FirstChr), $AddressArray[$i]) EndIf Next _GUICtrlTreeView_EndUpdate($hTV) EndFunc ;==>TreeBuild I added _GUICtrlTreeView_BeginUpdate and _GUICtrlTreeView_EndUpdate [center]www.jmontserrat.comFile Sequence UDF - _StringExtractPaths - _StringTrimPattern - GuiCtrlSetOnTop - CalendarUDF[/center] Link to comment Share on other sites More sharing options...
DrLarch Posted November 25, 2012 Author Share Posted November 25, 2012 (edited) Thanks so much, jmon! I'm still very new to these particular UDF's (first time using a treeview) - and it's much faster too! Edited November 25, 2012 by DrLarch Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now