Announcement

Collapse
No announcement yet.

TreeView Edit Problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • TreeView Edit Problem

    Below is my TreeView code. I noticed that when I click
    on an item to edit it, the change doesn't stay unless I
    force it by deleting the old item and rewriting the new one.
    Is that how it's supposed to work ? I get the feeling I
    did something wrong here.

    Best Regards

    Jim
    Code:
    #compile exe
    #debug error off
    #register none
    #include "win32api.inc"
    #include "commctrl.inc"
    #resource "dialogs.pbr"
     
    global hinstance&
    declare function dlgproc(byval long,byval long,byval long,byval long) as long
    declare function gettext(byval long) as string
     
    ''treeview functions
    declare function addstringtreeview(byval long,byval long,string) as long
    declare function inserttreeview(byval long,byval long,byval long,string) as long
    declare function deletetreeview(byval long,byval long) as long
    declare function ensurevisibletreeview(byval long,byval long) as long
    declare function totlinestreeview(byval long) as long
    declare function getedittreeview(byval long) as long
    declare function gettexttreeview(byval long,byval long) as string
    declare function setseltreeview(byval long,byval long) as long
    declare function sortchildrentreeview(byval long,byval long) as long
    declare function getparenttreeview(byval long,byval long) as long
    declare function getcurseltreeview(byval long) as long
    declare function islowestchildtreeview(byval long,byval long) as long
    declare function edititemtreeview(byval long,byval long) as long
     
    function winmain(byval hinst&,byval hprev&,cmdline as asciiz ptr,byval cmdshow&) as long
    
         hinstance&=hinst&
    
         dialogbox hinstance&,"TABLTREE",0,codeptr(dlgproc)
    
         function=1
         exit function
    end function
     
    function dlgproc(byval hwnd&,byval msg&,byval a&,byval xparam&) as long
    
         select case msg&
           case %wm_initdialog
    
             static htree&
             static oldedit$,newedit$
    
             htree&=getdlgitem(hwnd&,100)
    
             hroot&=addstringtreeview(htree&,%tvi_root,"Root 1")
    
             hparent&=addstringtreeview(htree&,hroot&,"Category 1")
             addstringtreeview htree&,hparent&,"Item 1"
             addstringtreeview htree&,hparent&,"Item 2"
    
             hparent&=addstringtreeview(htree&,hroot&,"Category 2")
             addstringtreeview htree&,hparent&,"Item 1"
             addstringtreeview htree&,hparent&,"Item 2"
    
             hroot&=addstringtreeview(htree&,%tvi_root,"Root 2")
    
             hparent&=addstringtreeview(htree&,hroot&,"Category 1")
             addstringtreeview htree&,hparent&,"Item 1"
             addstringtreeview htree&,hparent&,"Item 2"
    
             hparent&=addstringtreeview(htree&,hroot&,"Category 2")
             addstringtreeview htree&,hparent&,"Item 1"
             addstringtreeview htree&,hparent&,"Item 2"
    
           case %wm_syscommand
             if lowrd(a&)<>%sc_close then exit select
             enddialog hwnd&,1
             function=1
             exit function
           case %wm_command
    
             select case lowrd(a&)
               case 201,%idok  ''open
                 if hiwrd(a&)<>%bn_clicked then exit select
    
                 hitem&=getcurseltreeview(htree&)
                 if islowestchildtreeview(htree&,hitem&)=0 then exit select
    
                 item$=gettexttreeview(htree&,hitem&)
    
                 hparent&=getparenttreeview(htree&,hitem&)
                 cat$=gettexttreeview(htree&,hparent&)
    
                 hroot&=getparenttreeview(htree&,hparent&)
                 root$=gettexttreeview(htree&,hroot&)
    
                 txt$=root$+" - "+cat$+" - "+item$
                 msgbox txt$,%mb_ok,"Selected Item"
         
               case 202  ''add
                 if hiwrd(a&)<>%bn_clicked then exit select
    
                 hitem&=getcurseltreeview(htree&)
                 hparent&=getparenttreeview(htree&,hitem&)
                 hnewitem&=inserttreeview(htree&,hparent&,hitem&,"New Item")
                 setseltreeview htree&,hnewitem&
                 edititemtreeview htree&,hnewitem&
               case 203  ''delete
                 if hiwrd(a&)<>%bn_clicked then exit select
    
                 hitem&=getcurseltreeview(htree&)
    
                 item$=gettexttreeview(htree&,hitem&)
    
                 hparent&=getparenttreeview(htree&,hitem&)
    
                 if hparent&=%null then
                   txt$="Are You Sure You Want To Delete "+item$+" ?"
                 else
                   cat$=gettexttreeview(htree&,hparent&)
    
                   hroot&=getparenttreeview(htree&,hparent&)
    
                   if hroot&=%null then
                     txt$="Are You Sure You Want To Delete "+cat$+" - "+item$+" ?"
                   else
                     root$=gettexttreeview(htree&,hroot&)
    
                     txt$="Are You Sure You Want To Delete "+root$+" - "+cat$ _
                         +" - "+item$+" ?"
                   end if
    
                 end if
    
                 if msgbox(txt$,%mb_yesno,"Delete Item")=%idyes then
                   deletetreeview htree&,hitem&
                 end if
    
             end select
    
           case %wm_notify
             dim nmh as nmhdr ptr
             nmh=xparam&
    
             select case @nmh.code
               case %tvn_beginlabeledit
                 hedit&=getedittreeview(byval htree&)
                 oldedit$=gettext(hedit&)
               case %tvn_endlabeledit
                 hedit&=getedittreeview(byval htree&)
                 newedit$=gettext(hedit&)
    
                 hitem&=getcurseltreeview(htree&)
                 hparent&=getparenttreeview(htree&,hitem&)
                 inserttreeview htree&,hparent&,hitem&,newedit$
                 deletetreeview htree&,hitem&
    
                 txt$=chr$(34)+oldedit$+chr$(34)+" was changed to "+chr$(34) _
                     +newedit$+chr$(34)
                 msgbox txt$,%mb_ok,"Text Changed"
    
               case %tvn_selchanged
                 ''dim lptv as nm_treeview ptr
                 ''lptv=xparam&
                 ''hitem&[email protected]
                 ''b$=gettexttreeview(htree&,hitem&)
                 ''msgbox "Selection Changed To: "+b$,&mb_ok,"Selection Changed"
               case %nm_dblclk
                 postmessage hwnd&,%wm_command,maklng(201,%bn_clicked),0
             end select
    
         end select
    
    end function
     
    function gettext(byval hwnd&) as string
         local ztext as asciiz*255
         getwindowtext hwnd&,ztext,255
         txt$=ztext
         function=trim$(txt$)
    end function
     
    function addstringtreeview(byval htree&,byval hparent&,txt$) as long
    
         dim tvinsert as tv_insertstruct
         dim tvnewitem as tv_item
    
         if hparent&<>%tvi_root then  ''parent has children
           tvnewitem.mask=%tvif_children or %tvif_handle
           tvnewitem.hitem=hparent&
           tvnewitem.cchildren=1
           treeview_setitem htree&,tvnewitem
         end if
    
         tvinsert.hparent&=hparent&
         tvinsert.hinsertafter=%tvi_last
         tvinsert.item.mask=%tvif_text
         tvinsert.item.psztext=strptr(txt$)
         tvinsert.item.cchtextmax=len(txt$)
    
         function=treeview_insertitem(htree&,tvinsert)
    end function
     
    function inserttreeview(byval htree&,byval hparent&,byval hafter&,txt$) as long
    
         dim tvinsert as tv_insertstruct
         dim tvnewitem as tv_item
    
         if hparent&<>%tvi_root then  ''parent has children
           tvnewitem.mask=%tvif_children or %tvif_handle
           tvnewitem.hitem=hparent&
           tvnewitem.cchildren=1
           treeview_setitem htree&,tvnewitem
         end if
    
         tvinsert.hparent&=hparent&
         tvinsert.hinsertafter=hafter&
         tvinsert.item.mask=%tvif_text
         tvinsert.item.psztext=strptr(txt$)
         tvinsert.item.cchtextmax=len(txt$)
    
         function=treeview_insertitem(htree&,tvinsert)
    end function
     
    function deletetreeview(byval htree&,byval hitem&) as long
         function=treeview_deleteitem(htree&,hitem&)
    end function
     
    function ensurevisibletreeview(byval htree&,byval hitem&) as long
         function=treeview_ensurevisible(htree&,hitem&)
    end function
     
    function totlinestreeview(byval htree&) as long
         function=treeview_getcount(htree&)
    end function
     
    function getedittreeview(byval htree&) as long
         function=treeview_geteditcontrol(htree&)
    end function
     
    function gettexttreeview(byval htree&,byval hitem&) as string
    
         dim ztext as asciiz*255
         dim tvitem as tv_item
    
         tvitem.hitem=hitem&
         tvitem.mask=%tvif_text
         tvitem.psztext=varptr(ztext)
         tvitem.cchtextmax=255
         treeview_getitem htree&,tvitem
    
         txt$=ztext
    
         function=txt$
    end function
     
    function setseltreeview(byval htree&,byval hitem&) as long
         function=treeview_select(htree&,hitem&,%tvgn_caret)
    end function
     
    function sortchildrentreeview(byval htree&,byval hparent&) as long
         function=treeview_sortchildren(htree&,hparent&,0)
    end function
     
    function getparenttreeview(byval htree&,byval hitem&) as long
         function=treeview_getnextitem(htree&,hitem&,%tvgn_parent)
    end function
     
    function getcurseltreeview(byval htree&) as long
         function=treeview_getnextitem(htree&,%null,%tvgn_caret)
    end function
     
    function islowestchildtreeview(byval htree&,byval hitem&) as long
    
         if treeview_getnextitem(htree&,hitem&,%tvgn_child)=%null then
           function=1
         else
           function=0
         end if
    
    end function
     
    function edititemtreeview(byval htree&,byval hitem&) as long
         function=treeview_editlabel(htree&,hitem&)
    end function
          
    'resource in dialogs.rc
    '
    '#include "d:\pbdll32\winapi\resource.h"
    '
    'TABLTREE DIALOG LOADONCALL MOVEABLE 0, 0, 240, 170
    'STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | DS_MODALFRAME | DS_LOCALEDIT | DS_CENTER
    'CAPTION "TreeView Example"
    'BEGIN
    '  CONTROL "",100,"SysTreeView32", WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | WS_BORDER | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_EDITLABELS | TVS_DISABLEDRAGDROP | TVS_SHOWSELALWAYS, 5, 5, 185, 160
    '  CONTROL "Open", 201, "BUTTON", WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP | BS_DEFPUSHBUTTON, 195, 5, 40, 14
    '  CONTROL "New", 202, "BUTTON", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 195, 120, 40, 14
    '  CONTROL "Delete", 203, "BUTTON", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 195, 135, 40, 14
    'END
    ------------------
    Jim Seekamp
    Jim Seekamp

  • #2
    Jim;

    I think that is the way it works. Notes from my TreeView book...

    '---
    You should handle the TVN_BEGINLABELEDIT and the TVN_ENDLABELEDIT notifications.
    Actually it is not necessary to handle the TVN_BEGINLABELEDIT notification but
    it is a good idea to do so and limit the length of the text that the user may
    enter into the edit control.

    It is necessary for you to handle the TVN_ENDLABELEDIT notification. If you don’t
    do so, then although the user will be able to edit the labels, but at the end of
    the edit the value of the label will be set to the previous value.
    The handler is also a good place to update any internal data structure you
    maintain to track the state of the tree view control.
    '---

    HTH
    Regards,
    Jules
    Best regards
    Jules
    www.rpmarchildon.com

    Comment


    • #3
      Thanks Jules

      Best Regards

      Jim


      ------------------
      Jim Seekamp
      Jim Seekamp

      Comment

      Working...
      X