Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

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
0
tvtech
Asked:
tvtech
  • 9
  • 4
  • 2
  • +3
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
0
 
ramsesCommented:
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

     
0
 
tvtechAuthor Commented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ramsesCommented:
huh?


You want to trigger a mousedown or keypress event?


Please more info
0
 
tvtechAuthor Commented:
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
0
 
tvtechAuthor Commented:
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
0
 
ramsesCommented:
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 ;)
0
 
tvtechAuthor Commented:
:) 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
0
 
tvtechAuthor Commented:
:) 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
0
 
tvtechAuthor Commented:
:) 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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
 
Julian_KCommented:
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}
0
 
tvtechAuthor Commented:
  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
0
 
kuppachiCommented:
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.
0
 
ramsesCommented:
Just use the Selected Property of the node you want

Let's say, for example, that the Inbox Folder is the first node added, then

TreeView1.Nodes(1).Selected=True

or, if you made the node with key "InBox",

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

Will do just the same


Ramses
0
 
kuppachiCommented:
tony,
have u got it or not ?
0
 
tvtechAuthor Commented:
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
0
 
jgvCommented:
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
0
 
tvtechAuthor Commented:
Thanks ramses. Sorry for the belated points award. EE has not been notifying me of new answers.

Regards,
   Tony
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now