Announcement

Collapse
No announcement yet.

Treeview

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

  • Treeview

    Ive got a treeview working for the first time in my life and I'm pretty stoked about it! Ive got pretty much everything sussed out about it, but I have just two questions ...
    1 - how can I detect a right-click on a treeview item? (i'd like to display a popup menu then)
    2 - how can I determine the text of the parent one level above the item selected? eg.:
    Code:
        Fruits
          |- Citrus
             |-Oranges
    if I clicked on Oranges, how can I determine that Citrus was the item that I came from? (eg. not the parent, Fruits, but the sub-parent, Citrus)


    ------------------
    -

  • #2
    For right click i recommend WM_CONTEXTMENU instead.
    Now you can use the Window key too.

    Code:
        Dim pNMHDR      As NMHDR Ptr
    
        Case %WM_NOTIFY
    
    
                If CbLparam = 0 Then Exit Select
    
                pNMHDR = CbLparam
    
                Select Case @pNMHDR.code
                Case %NM_CLICK
                Case %NM_DBLCLK
                Case %NM_RCLICK
                Case %NM_RETURN
                Case %LVN_ITEMCHANGED
                Case %LVN_DELETEITEM
                Case %LVN_DELETEALLITEMS
    
                    Function = 1
                    SetWindowLong CbHndl, %DWL_MSGRESULT, 1
    
                End Select

    ------------------
    hellobasic

    Comment


    • #3
      Thanks Edwin, I was already hooking %TVN_SELCHANGED so it was easy to add in your extra code


      ------------------
      -

      Comment


      • #4
        The following code creates a simple tree with a few items - 3 levels deep. When a new item is selected, it remembers the item, so that when you right-click it can tell you what item is currently selected. All I need to do now is find out the name of the item one level above what I clicked.
        eg. It creates this tree:
        Code:
         Parent
           Child
             SubItem 1
             SubItem 2
        If i right-click on SubItem1 or SubItem2, i want it to msgbox "One level above is Child". If I click on "Child", i want it to say "One level above is Parent". Can this be done?

        Code:
        #compile exe
        #include "win32api.inc"
        #include "commctrl.inc"
        
        declare callback function DlgProc
        GLOBAL hTreeItem As Long, hDlgTree As Long, hTree As Long
        GLOBAL CurrentItem AS STRING
        %ID_TREE = 100
        
        function TVInsertItem(byval hTree as long, byval hParent as long, sTxt as string) as long
            local tv_insert as TV_INSERTSTRUCT
            local tv_item as TV_ITEM
            if hParent then
                tv_item.mask      = %TVIF_CHILDREN OR %TVIF_HANDLE
                tv_item.hItem     = hParent
                tv_item.cchildren = 1
                TreeView_SetItem hTree, tv_item
            end if
            tv_insert.hParent              = hParent
            tv_insert.Item.Item.mask       = %TVIF_TEXT
            tv_insert.Item.Item.pszText    = strptr(sTxt)
            tv_insert.Item.Item.cchTextMax = len(sTxt)
            function = TreeView_InsertItem(hTree, tv_insert)
        end function
        
        function pbmain
          local hDlg as long
          dialog new 0, "TreeView32",,,420, 240, %WS_SYSMENU or %WS_MINIMIZEBOX or %DS_CENTER to hDlg
          control add "SysTreeView32", hDlg, %ID_TREE, "", 0,0,416,180, _
                      %WS_CHILD or %WS_VISIBLE or %TVS_HASBUTTONS or %TVS_HASLINES or _
                      %TVS_LINESATROOT or %TVS_SHOWSELALWAYS, %WS_EX_CLIENTEDGE
          dialog show modal hDlg call DlgProc
        end function
        
        callback function DlgProc
        local hParent as long, hRoot as long, hTree as long
        hDlgTree = GetDlgItem(CbHndl,%ID_TREE)
        Local tItem As TV_ITEM              ' treeview item
        Local szTxt As AsciiZ * 61
        select case cbmsg
         case %WM_INITDIALOG
            control handle cbhndl, %ID_TREE to hTree
            hRoot = TVInsertItem(hTree, 0, "Parent")
            hParent = TVInsertItem(hTree, hRoot, "Child #1")
            TVInsertItem hTree, hParent, "Subitem 1"
            TVInsertItem hTree, hParent, "Subitem 2"
            hParent = TVInsertItem(hTree, hRoot, "Child #2")
            TVInsertItem hTree, hParent, "Subitem 1"
            TVInsertItem hTree, hParent, "Subitem 2"
          case %WM_NOTIFY
            Local lpNmh As NMHDR Ptr
            lpNmh = CblParam
            Select Case @lpNmh.Code
              Case %TVN_SELCHANGED
                   Local lpTV As NM_TREEVIEW Ptr
                   lpTV = CblParam
                   hTreeItem = @lpTV.ItemNew.hItem
                   tItem.hitem = hTreeItem
                   tItem.mask = %TVIF_TEXT
                   tItem.pszText = VarPtr(szTxt)
                   tItem.cchTextMax = 61&
                   Call TreeView_GetItem(hDlgTree, tItem)
                   CurrentItem = szTxt
              Case %NM_RCLICK
                   Msgbox "Right-clicked on " & CurrentItem
            End Select
        end select
        end function

        ------------------
        -

        Comment


        • #5
          Hi Wayne,
          Did you try Treeview_GetParent?

          Regards,
          Adam

          ------------------

          Comment


          • #6
            Adam,
            I have tried that yes but it returns a dword - FUNCTION TreeView_GetParent (BYVAL hwnd AS DWORD, BYVAL hitem AS DWORD) AS DWORD
            Im assuming that's a handle or ID of some sort for the parent, but how do I get the actual name of it? (string)
            Thanks,
            Wayne


            ------------------
            -

            Comment


            • #7
              All handles are a pointer to a tvitem
              You need to retrieve this tvitem first to find out the pointer to a string.

              It's recommended you have some sort of MSDN or help

              something i use:

              Code:
              Function LISTVIEW_GetSelectedItemText( ByVal hWnd As Long, ByVal nItem As Long, ByVal Column As Long ) As String
              
                  Dim a       As Long
                  Dim lvi     As LV_ITEM
                  Dim Buffer  As String * 2000
              
                  If nItem < 0 Then nItem = LISTVIEW_GetSelectedItem( hWnd, -1 )
              
                  lvi.iSubItem    = Column
                  lvi.cchTextMax  = Len( Buffer )
                  lvi.pszText     = VarPtr( Buffer )
              
                  a = SendMessage( hWnd, %LVM_GETITEMTEXT, nItem, VarPtr( lvi ) )
                  If a > 0 Then Function = Left$( Buffer, a )
              
              End Function
              ------------------
              hellobasic

              Comment


              • #8
                Hi Wayne,

                This returns the text for a tree item having handle hItem -
                Code:
                function TVW_GetText( byval hTree as long, byval hItem as long ) as string
                
                  'Purpose : Returns the item's text ; if the item is not found, returns ""
                  '
                  'Remarks : Allows max of 255 characters for tree item text
                  '
                  local ti      as TV_ITEM
                  local zText   as asciiz * 256
                  local s       as string
                  
                  'Set up the tree item structure for our purpose...
                  ti.hItem      = hItem                              
                  ti.mask       = %TVIF_TEXT                                  
                  zText         = space$( 255 )                       
                  ti.cchTextMax = 256                                
                  ti.pszText    = varptr( zText )                                   
                
                  'Query the tree...
                  if TreeView_GetItem( hTree, ti ) = 0 then        'call failed             
                    function = ""
                  else
                    s = zText
                    function = s
                  end if
                
                end function
                A book I've found really useful is Windows 95 Common Controls & Messages API Bible, by Richard J Simon, ISBN 1-57169-010-7.

                HTH - good luck !

                Paul

                [This message has been edited by Paul Noble (edited April 21, 2001).]
                Zippety Software, Home of the Lynx Project Explorer
                http://www.zippety.net
                My e-mail

                Comment


                • #9
                  wayne --
                  i added some statetements to a sample, which i posted in http://www.powerbasic.com/support/pb...ead.php?t=3328

                  below i search rbuttondown (for popup menu it's more interesting than "click").

                  Code:
                     #compile exe
                     #dim all
                     #register none
                     #include "win32api.inc"
                     #include "commctrl.inc"
                  
                     function tvinsertitem(byval htree as long, byval hparent as long, stxt as string) as long
                        local tv_insert as tv_insertstruct
                        local tv_item as tv_item
                        if hparent then
                           tv_item.mask = %tvif_children or %tvif_handle
                           tv_item.hitem = hparent
                           tv_item.cchildren = 1
                           treeview_setitem htree, tv_item
                        end if
                        tv_insert.hparent = hparent
                        tv_insert.item.item.mask = %tvif_text
                        tv_insert.item.item.psztext = strptr(stxt)
                        tv_insert.item.item.cchtextmax = len(stxt)
                        function = treeview_insertitem(htree, tv_insert)
                     end function
                  
                     callback function dlgproc
                        %id_tree = 100
                        dim hparent as long, hroot as long, htree as static long
                        dim lpnmh as nmhdr ptr, hitem as long, lptv as nm_treeview ptr, pitem as tv_item
                        dim tmpasciiz as asciiz * %max_path, i as long, j as long, answer as string
                        dim ht as tv_hittestinfo
                  
                        select case cbmsg
                           case %wm_initdialog
                              control add "systreeview32", cbhndl, %id_tree, ", 5, 5, 190, 90, _
                                  %ws_child or %ws_visible or %tvs_hasbuttons or %tvs_haslines or _
                                  %tvs_linesatroot or %tvs_showselalways, %ws_ex_clientedge
                              control handle cbhndl, %id_tree to htree
                  
                              hroot = tvinsertitem(htree, 0, "root")
                              for i = 1 to 10
                                 hparent = tvinsertitem(htree, hroot,  "item" + format$(i))
                                 for j = 1 to 10
                                    tvinsertitem htree, hparent, "subitem" + format$(i) + "." + format$(j)
                                 next
                              next
                  
                           case %wm_parentnotify
                              if cbctl = %wm_rbuttondown then
                                  getcursorpos ht.pt
                                  if screentoclient (getdlgitem(cbhndl, %id_tree), ht.pt) then
                                     senddlgitemmessage cbhndl, %id_tree, %tvm_hittest, 0, varptr(ht)
                                     i = ht.hitem
                                     while i
                                        pitem.hitem = i
                                        pitem.mask = %tvif_text
                                        pitem.psztext = varptr(tmpasciiz)
                                        pitem.cchtextmax = sizeof(tmpasciiz)
                                        treeview_getitem htree, pitem
                                        if answer <> " then answer = trim$(tmpasciiz) + "; " + answer else _
                                           answer = trim$(tmpasciiz) + " (rbuttondown)"
                                        i = treeview_getparent (htree, pitem.hitem)
                                     wend
                                     setwindowtext cbhndl, byval strptr(answer)
                                 end if
                           end if
                           
                          case %wm_notify
                              lpnmh = cblparam
                              if (@lpnmh.code = %tvn_selchanged) and (@lpnmh.idfrom = %id_tree) then
                                  lptv = cblparam
                                  i = @lptv.itemnew.hitem
                                  while i
                                     pitem.hitem = i
                                     pitem.mask = %tvif_text
                                     pitem.psztext = varptr(tmpasciiz)
                                     pitem.cchtextmax = sizeof(tmpasciiz)
                                     treeview_getitem htree, pitem
                                     if answer <> " then answer = trim$(tmpasciiz) + "; " + answer else _
                                        answer = trim$(tmpasciiz) + " (selchanged)"
                                     i = treeview_getparent (htree, pitem.hitem)
                                  wend
                                  setwindowtext cbhndl, byval strptr(answer)
                               end if
                        end select
                     end function
                  
                     function pbmain
                        local hdlg as long
                        dialog new 0, "treeview32",,, 200, 100, %ws_sysmenu or %ws_minimizebox or %ws_caption to hdlg
                        dialog show modal hdlg call dlgproc
                     end function
                  ------------------
                  e-mail: [email protected]

                  Comment


                  • #10
                    Thanks very much all! Semen, Edwin, Paul, Im back on the road again
                    Paul, using your function I was able to do what I needed
                    Code:
                          Case %NM_RCLICK
                                Msgbox "Right-click!"
                                DIM xParentID As Long
                                xParentID = Treeview_Getparent(hDlgTree, hTreeItem)
                                Msgbox TVW_GetText(hDlgTree, hTreeItem),, "CURRENT SELECTION"
                                Msgbox TVW_GetText(hDlgTree, xParentID),, "ITS PARENT"
                    Too easy Thanks all!


                    ------------------
                    -

                    Comment


                    • #11
                      Sorry, my earlier post was for the listview..

                      Here is a treeview example wich is 99.9% the same as the earlier tv post.
                      I just build'd it, could not resist to post..

                      Code:
                       
                      Function TV_GetItemStr( ByVal hWnd As Long, ByVal hItem As Long ) As String
                       
                          Dim a       As Long
                          Dim tvItem  As TV_ITEM
                          Dim Buffer  As String * 2000
                       
                          tvItem.Mask         = %TVIF_TEXT
                          tvItem.hItem        = hItem
                          tvItem.pszText      = VarPtr( Buffer )
                          tvItem.cchTextMax   = Len( Buffer )
                       
                          a = SendMessage( hWnd, %TVM_GETITEM, 0, ByVal VarPtr( tvItem ) )
                          If a > 0 Then
                       
                              a = Instr( Buffer, Chr$( 0 ) )
                              If a > 0 Then  Function = Left$( Buffer, a - 1 )
                       
                          End If
                       
                      End Function

                      ------------------
                      hellobasic

                      Comment

                      Working...
                      X
                      😀
                      🥰
                      🤢
                      😎
                      😡
                      👍
                      👎