Link to home
Start Free TrialLog in
Avatar of tvtech
tvtechFlag for Australia

asked on

Treeview node select

Greetings,

    The bane of my life ... treeviews. :( In my app, a screen pops up when called and runs through a sequence then closes. I need a Node of a Treeview, on the calling form, to be auto-selected, so that the contents of that Node are updated.

    I have tried various methods, but none have worked successfully. Does anyone have some decent code that will do what I need, please.

Thank you.

Regards,
   Tony
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

To select a node in a treeview you have 2 main options:
* identify all your nodes with a KEY property, and select the node with:
Set YourTreeview.SelectedItem = YourTreeview.Nodes("TheKeyValue")

* loop trough the nodes collection, and if you found the good one, select it:
dim nodLoop as Node
for each nodLoop in YourTreeview.Nodes
  if "nodLoop is the good one" then
    set YourTreeView.SelectedItem = nodLoop
  end if
next

Note that the second one is much slower...

hope this helps
Avatar of ramses
ramses

Hello Tony

You can select any node you'd like by using it's KEY


When you add a node to a treeview, make sure to supply it with a KEY (must be unique).  That way you can select any node with it's KEY, like this:


Dim nodX As Node
  Set nodX = TreeView1.Nodes.Add(,,"R","Root")

The Add method consists of the following:

Add([relative], [relationship], [key], [text], [image], [selectedImage])


Then, when you've filled your Treeview and want to refrence a particular key, go like this:

TreeView1.Nodes("R").Text = "Some new text"

If you don't want to use the Key property to reference the nodes, you can also use it's index.  

So, in the example above, you can also go like this:

TreeView1.Nodes(1).Text = "Some new text"

As you see, this is a 1-based index, not 0 like the Listbox


Should you need additional info, please let me know



Ramses

     
Avatar of tvtech

ASKER

Thank you for the replies.

As usual, the words never come out right ... what I am trying to achieve is essentially a mousedown, or keypress on the Node that I need to refresh. Trying some of the examples above, I have not been able to make this happen. Does that explain it a bit better?

Thank you.

Regards,
   Tony
huh?


You want to trigger a mousedown or keypress event?


Please more info
Avatar of tvtech

ASKER

Thank you for the replies.

As usual, the words never come out right ... what I am trying to achieve is essentially a mousedown, or keypress on the Node that I need to refresh. Trying some of the examples above, I have not been able to make this happen. Does that explain it a bit better?

Thank you.

Regards,
   Tony
Avatar of tvtech

ASKER

Hi ramses,

Essentially yes. (See what I mean about not explaining myself) :) When the called form closes, basically I need to simulate a click event so that the Node update automatically with the new data.

Regards,
   Tony
No sweat!


first declare a variable as ComctlLib.Node, set that node to the selected node, then go like this:

formname.Treeview1_NodeClick yournodevar

this simulates a nodeclick

In the Mouserelated events, you don't need the Node variable, but the X,Y, Shift and other stuff.



Just scream for more ;)
Avatar of tvtech

ASKER

:) I'm screaming... :( Declare ComctlLib.Node?
The Treeview is on the calling form, which means that MailForm.SSTree1_NodeClick "Inbox" is illegal ...?

I'm lost with your example, sorry to say ...

Regards,
   Tony
Avatar of tvtech

ASKER

:) I'm screaming... :( Declare ComctlLib.Node?
The Treeview is on the calling form, which means that MailForm.SSTree1_NodeClick "Inbox" is illegal ...?

I'm lost with your example, sorry to say ...

Regards,
   Tony
Avatar of tvtech

ASKER

:) I'm screaming... :( Declare ComctlLib.Node?
The Treeview is on the calling form, which means that MailForm.SSTree1_NodeClick "Inbox" is illegal ...?

I'm lost with your example, sorry to say ...

Regards,
   Tony
Ok, lets start the discussion from scratch:

you have a form, which you name CallingForm ...
on this form, you have a treeview, and in this treeview a node.
let us call this node ActiveNode.
I guess that this node is selected when the second form (let us call it SubForm) is opened, in order to edit data that should go to the selected node (maybe you give more info how the node needs to be updated to reflect those changes...)
Is the SubForm Modal? if yes, then the answer should be simple:
  SubForm.Show vbModal
  TreeView1_NodeClick ActiveNode
if not, things become more tricky, and some workarounds are possible.
Here is one:
  in the SubForm, define an event that is raised whenever the SubForm is closed. This event needs to be catched by the CallingForm:

--- SubForm Code ---  
Public Event ClosingForm()

Private Sub Form_Unload(...)
  RaiseEvent ClosingForm()
End Sub


--- CallingForm Code ---
Private withevents sf as SubForm


Private Sub sf_ClosingForm()
  'call the NodeClick or MouseDown event procedure, for example
  Treeview1_NodeClick Treeview1.SelectedItem
End Sub

Hope this helps

Hi. I'm not sure I understood you perfect, but If I am, this might help:

on the Calling Form:

private sub Something()

dim myNode as ComctlLib.Node

..... {your code}

set myNode = treeview1.nodes("NeededKey") '<-- the key of the needed node
set treeview1.SelectedItem = mynode
treeview1.SelectedItem.EnsureVisible
set myNode = nothing

..... {your code}
Avatar of tvtech

ASKER

  Points raised, as I am not explaining myself correctly. Here's the whole drum of the code ..

   I have developed a mail client which looks very simlar to Outlook Express. As you will be aware, as mail comes in (POP), the 'message grid' (From, Subject, Date etc) updates with each email as it arrives. That is where my problem is. Due to the fact that mail can be sent to whatever Treeview folder the user wants ie Inbox, MyMail etc, I need the ability to default click (mousedown) on the 'Inbox', which is a preset folder within the Treeview. This needs to occur when all mail has arrived. The called form (shows mail arriving, like Outlook) is modeless.

   Hoping that's a better description of my problem ...

Regards,
   Tony
try this code:-

TreeView1.Nodes.Add , , "K1", "Root"
TreeView1.Nodes("K1").Expanded = True
TreeView1.Nodes.Add "K1", tvwChild, "K2", "Child1"
TreeView1.Nodes.Add "K1", tvwChild, "K3", "Child2"

TreeView1.Nodes.Add "K2", tvwChild, "K4", "Child3"

TreeView1.Nodes("K1").Expanded = True
TreeView1.Nodes("K2").Expanded = True

TreeView1.Nodes("K4").Selected = True

If u know the key of the node you have to select, just look at the last line which helps in highlighting the node.
ASKER CERTIFIED SOLUTION
Avatar of ramses
ramses

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tony,
have u got it or not ?
Avatar of tvtech

ASKER

Hi ramses,

   Sorry to say that none of the code samples, or any variants that I have written will work here. Due to the fact that the called form is modeless, I have tried introducing the code at Form Unload, but still no go. The Treeview doesn't even get focus, let alone click. :( Wonder if API is the go ...

Regards,
   Tony
Everything you need to know is in this thread, you just have to piece it together.

This will demonstrate how you would select a node and then force a NodeClick from the "show arriving" form:

1) On the main form (with the treeview) you must make the NodeClick event Public (it is Private by default). This will allow you to call it from another form. It should look something like this:

Public Sub SSTree1_NodeClick(ByVal Node As MSComctlLib.Node)
    'your code
End Sub

Note that the parameter will be "Node As ComctlLib.Node" if you are using M$ Windows Common Controls 5.0

2) In the "show arriving" form you first need to select the appropriate treeview node. I will assume that you are using the Key property to identify certain nodes ("Inbox" for the Inbox node). After that you call the NodeClick event on the main form and pass the selected node as the parameter. It should look something like this:

Private Sub cmdActivateInbox_Click()
    MailForm.SSTree1.Nodes("Inbox").Selected = True
    Call MailForm.SSTree1_NodeClick(MailForm.SSTree1.SelectedItem)
End Sub
Avatar of tvtech

ASKER

Thanks ramses. Sorry for the belated points award. EE has not been notifying me of new answers.

Regards,
   Tony