Solved

Treeview node select

Posted on 2001-08-28
19
511 Views
Last Modified: 2007-12-19
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
Comment
Question by:tvtech
  • 9
  • 4
  • 2
  • +3
19 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6431278
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
 
LVL 2

Expert Comment

by:ramses
ID: 6431296
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
 

Author Comment

by:tvtech
ID: 6431326
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
 
LVL 2

Expert Comment

by:ramses
ID: 6431339
huh?


You want to trigger a mousedown or keypress event?


Please more info
0
 

Author Comment

by:tvtech
ID: 6431368
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
 

Author Comment

by:tvtech
ID: 6431371
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
 
LVL 2

Expert Comment

by:ramses
ID: 6431375
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
 

Author Comment

by:tvtech
ID: 6431519
:) 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
 

Author Comment

by:tvtech
ID: 6431532
:) 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:tvtech
ID: 6431541
:) 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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6431542
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
 
LVL 5

Expert Comment

by:Julian_K
ID: 6431543
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
 

Author Comment

by:tvtech
ID: 6431611
  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
 

Expert Comment

by:kuppachi
ID: 6431624
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
 
LVL 2

Accepted Solution

by:
ramses earned 175 total points
ID: 6431632
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
 

Expert Comment

by:kuppachi
ID: 6431861
tony,
have u got it or not ?
0
 

Author Comment

by:tvtech
ID: 6434274
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
 
LVL 12

Expert Comment

by:jgv
ID: 6434836
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
 

Author Comment

by:tvtech
ID: 6719381
Thanks ramses. Sorry for the belated points award. EE has not been notifying me of new answers.

Regards,
   Tony
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now