Page 1 of 1

How to know which item was checked on tree?

Posted: Mon Sep 14, 2009 5:13 pm
by dempty
Hi Guys,

I`ve started to work under tree with checboxes, but i can`t find an event that returns to me the item was checked.

I already tried oTree:bChanged and oTree:bChange but any of them works. When I tried oTree:OnClick its works just in a away, because i can`t get the item which was just checked.

Can some one give a hand?


Thanks,
Diego Imenes

Re: How to know which item was checked on tree?

Posted: Mon Sep 14, 2009 7:18 pm
by Antonio Linares
Diego,

Could you please provide an example PRG that shows the way you are doing it ? thanks

Re: How to know which item was checked on tree?

Posted: Mon Sep 14, 2009 8:08 pm
by dempty
Antonio Linares wrote:Diego,

Could you please provide an example PRG that shows the way you are doing it ? thanks


Here it goes

Code: Select all | Expand


#include "FiveWin.ch"

function Main()

   local oDlg, oTree

   DEFINE DIALOG oDlg

      oTree = TTreeView():New( 0, 0, oDlg,,,,,200,200,,.t.)

      oTree:OnClick = {|| msginfo(  oTree:GetSelected():cPrompt  , 1) }

   ACTIVATE DIALOG oDlg CENTERED ON INIT BuildTree( oTree )

   MsgInfo( oTree:aItems[ 1 ]:GetCheck() )

return nil

function BuildTree( oTree )

   local oMenu := Array( 2 ), oSubMenu := Array( 3 )

   oMenu[ 1 ]:= oTree:Add( "Principal" )
      oSubMenu[ 1 ]:= oMenu[ 1 ]:Add( "Imprimir..." )
      oSubMenu[ 1 ]:SetCheck( .T. )

   oMenu[ 2 ]:= oTree:Add( "Proyectos" )
      oSubMenu[ 2 ]:= oMenu[ 2 ]:Add( "Definir Proyectos" )
      oSubmenu[ 3 ]:= oMenu[ 2 ]:Add( "Actualización datos" )

   oTree:Expand()

return nil
 


Like going with the mouse or with the space bar to check a item i would like to SHOW which item was checked. I tried the getselected() but when you check a item, it doesn't become selected.


Thanks,
Diego

Re: How to know which item was checked on tree?

Posted: Mon Sep 14, 2009 8:42 pm
by Antonio Linares
Diego,

Here you have your example modified:

Code: Select all | Expand


#include "FiveWin.ch"

function Main()

   local oDlg, oTree

   DEFINE DIALOG oDlg

      oTree = TTreeView():New( 0, 0, oDlg,,,,,200,200,,.t.)

      oTree:OnClick = { || CheckStatus( oTree, oTree:aItems ) }

   ACTIVATE DIALOG oDlg CENTERED ON INIT BuildTree( oTree )

return nil

function BuildTree( oTree )

   local oMenu := Array( 2 ), oSubMenu := Array( 3 )

   oMenu[ 1 ]:= oTree:Add( "Principal" )
      oSubMenu[ 1 ]:= oMenu[ 1 ]:Add( "Imprimir..." )
      oSubMenu[ 1 ]:SetCheck( .T. )

   oMenu[ 2 ]:= oTree:Add( "Proyectos" )
      oSubMenu[ 2 ]:= oMenu[ 2 ]:Add( "Definir Proyectos" )
      oSubmenu[ 3 ]:= oMenu[ 2 ]:Add( "Actualización datos" )

   oTree:Expand()

return nil

function CheckStatus( oTree, aItems )

   local n
   
   for n = 1 to Len( aItems )
      MsgInfo( oTree:GetCheck( aItems[ n ] ) )
      CheckStatus( oTree, aItems[ n ]:aItems )
   next
   
return nil      
 

Re: How to know which item was checked on tree?

Posted: Mon Sep 14, 2009 8:50 pm
by dempty
Antonio,

In this way i have done before. I really want to pick up that exactly item in the moment its checked.

When I check "Imprimir..." i want the message shows "Imprimir...". Just the item was checked and not all of them.

Is that possible?

Re: How to know which item was checked on tree?

Posted: Mon Sep 14, 2009 9:02 pm
by Antonio Linares
Diego,

Please check if this code is fine for what you want:

Code: Select all | Expand


function CheckStatus( oTree, aItems )

   local n
   
   for n = 1 to Len( aItems )
      if oTree:GetCheck( aItems[ n ] )
         MsgInfo( aItems[ n ]:cPrompt )
      endif  
      CheckStatus( oTree, aItems[ n ]:aItems )
   next
   
return nil
 

Re: How to know which item was checked on tree?

Posted: Tue Sep 15, 2009 11:04 am
by dempty
Antonio,

Almost this! Because its shows all items that are checked and i wanna show just the item was checked in each moment.

I don`t know if I express myself well. So being more exactly:

The tree is there with all items, so i check "Imprimir..." and the function shows for me just "Imprimir..."
So i check "Definir Proyectos" and the function shows for me just "Definir Proyectos"


______________________________________


Antonio I`m reading the overview of TreeView classe at MSDN:

http://msdn.microsoft.com/pt-br/library ... view(VS.80).aspx

So I found this part that I think is what I`m looking for....

Setting the TreeNode.Checked property from within the BeforeCheck or AfterCheck event causes the event to be raised multiple times and can result in unexpected behavior.For example, you might set the Checked property in the event handler when you are recursively updating the child nodes, so the user does not have to expand and check each one individually.To prevent the event from being raised multiple times, add logic to your event handler that only executes your recursive code if the Action property of the TreeViewEventArgs is not set to TreeViewAction.Unknown.For an example of how to do this, see the Example section of the AfterCheck or BeforeCheck events.

Re: How to know which item was checked on tree?

Posted: Thu Sep 17, 2009 11:25 am
by dempty
Antonio?

Re: How to know which item was checked on tree?

Posted: Thu Apr 07, 2011 8:48 pm
by Gale FORd
Has this ever been resolved. I find myself needing to know which node the user changed.
Wwen the user checks or unchecks the node it does not select the node so I cant tell which one was changed.

Re: How to know which item was checked on tree?

Posted: Thu Apr 07, 2011 9:00 pm
by Daniel Garcia-Gil

Re: How to know which item was checked on tree?

Posted: Thu Apr 07, 2011 10:08 pm
by Gale FORd
The problem is different. When you use the check boxes and you use the mouse to click on the checkbox it does not make that node selected. So you cannot tell which node was actually checked. I can tell which node is selected but that is a different issue.