Welcome to MSDN Blogs Sign in | Join | Help

Data Tools and Software Testing

Old model-based testers never die; they just transition to a higher state.

Syndication

News

    These postings are provided "AS IS" with no warranties, and confer no rights.
    Use of included script and code samples are subject to the terms specified here.

TreeView Plus

I was so sure that there just has to be a way to force the plus sign (+) displayed on the TreeView WinForm control - but looks like I was wrong. The TreeView control intelligently decides to show the plus and minus signs depending on whether a node has child nodes or not and whether they are expanded or collapsed. The problem with this design is that it assumes that the control will always know the full tree hierarchy that a user is trying to display. In scenarios where you have a very large list of objects (nodes) to be shown, it might improve performance to not load or retrieve that list till absolutely necessary. So you would want to display a + sign next to a node, irrespective, of whether it has child nodes or not. When the user clicks on the node, the sign can be cleared away if there are no child nodes to be displayed. This is exactly the behavior of the IIS Manager MMC and it seems like it is not possible to do this with the Out-Of-The-Box TreeView control. This is further exacerbated if the data you are retrieving is over a web service (which should become more prevalent as time goes by) – so you would never want to get the full tree details in the very first call that you make.

Am I missing something here? Anyone know of an extended TreeView control which has this option/behavior?

Published Sunday, April 11, 2004 1:52 AM by nihitk

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: TreeView Plus @ Sunday, April 11, 2004 2:38 AM

Hi, Nihit: Couldn't you add a dummy item under each node (which would cause the plus sign to display), then remove it when the user expands the node (and you load the actual subitems)?

Phil Weber

# re: TreeView Plus @ Sunday, April 11, 2004 3:03 AM

Hi Phil,

That thought did cross my mind - but I rejected it because it seemed too "hacky" a solution. I believe a good solution should be simple and elegant. This one just fit the simple bill...:)

Plus - 5 years down the line, if somebody else is mainting your code and can't make sense of why you add and then remove a dummy node, and deletes that code, they will cause a regression which would be hard to catch - suddenly the plus signs stop appearing! Yes - you could add comments - but it will still remain a hack.

Maybe I am just being too particular about something so small...:)

Nihit Kaul

# re: TreeView Plus @ Sunday, April 11, 2004 3:21 AM

Dummy item - only way.

Thomas Tomiczek

# re: TreeView Plus @ Sunday, April 11, 2004 3:25 AM

The dummy item isn't really 'hacky', especially not if the dummy is grey text saying "Loading..." then at least, for that moment it is visible after the user clicks the plus sign, it shows the user why there was a plus and now there isn't.

At least that is what I do :)

Duncan Mackenzie

# re: TreeView Plus @ Sunday, April 11, 2004 4:09 AM


Can you think showing the registry content without using dummy treeitems at each level, so that the next level is only expanded upon request ?

Stephane Rodriguez

# re: TreeView Plus @ Sunday, April 11, 2004 7:23 AM

Agree that dummy items are okay. They were the only way in VB6 as well, so many coders are familiar with it ;)

Dummy

# re: TreeView Plus @ Sunday, April 11, 2004 9:00 AM

If it helps make you feel any better, I know of several Microsoft products that use the dummy item route in their code.

Mike Gunderloy

# re: TreeView Plus @ Sunday, April 11, 2004 1:19 PM

Wow! I didn't know the 'dummy' was this popular...:)

Well - personally the "Loading" idea from Duncan made some sense to me. That way we achieve 2 aims - we provide information to the user, that the list is being loaded dynamically and so they might need to be patient, AND we solve the + sign issue.

Seems like the elegant bill was just met as well. Thanks everyone for all the comments.

And Mike - No - I am not comforted by the fact that there are a lot of MS products which do that. If we reason by that standard, it would be hard to get out of a lot of bad programming practices. IMO.

Stephane - Not sure what your question was, but if you were asking if there was an alternative to the dummy model, well I think there could a property at the control itlsef, which overrides the intelligent behavior and shows the plus/minus signs irrespective of whether the data is loaded or not. Maybe something like "ForcePlusMinusDisplay" in addition to the current "ShowPlusMinus" property.

Nihit Kaul

# re: TreeView Plus @ Monday, April 12, 2004 3:03 PM

But if you show the + irrespective of whether there are child nodes or not, then aren't you showing inaccurate information? The same applies to the "Loading" idea. What if there is a child node actually named "Loading"?

Ideally, there should be a way of indicating when there may or may not be child nodes (maybe a gray +), and a separate way to indicate during list expansion that the program is loading the children (maybe an animation).

Derek

# re: TreeView Plus @ Tuesday, April 13, 2004 12:09 AM

My approach has been to do a lazy load of only the items one level down in the hierarchy and load them on the expand event from a parent level item. That way you avoid loading the whole tree into memory, but you always have one deep already there for the plus sign and for a quick responsive tree expansion.

Brian Noyes

# re: TreeView Plus @ Tuesday, April 13, 2004 11:52 AM

Derek - You are right in that, you are still showing inaccurate information if there are no child nodes - but if the 80% case (more in my scenario) has child nodes, then it probably makes sense as compared to not loading any sign at all (which is also useless to the user, since it doesn't tell anything - whether it has child nodes or not - you have to click to see).
Your ideal case is indeed that - Ideal..:)
If I undertake creating an extended TreeView control, I shall keep this in mind.

Brian - Your suggestion is also another good one - though one problem with it might be that, it might not be easy to filter the data in such a way. In my case, thedata is retrieved from various tables, doing joins etc. in different ways, so filtering out on the basis of "level" is not easy as it is not part of the design. But if creating a product from scratch, it would be great to have this option.

Nihit Kaul [MSFT]

# re: TreeView Plus @ Friday, July 16, 2004 11:17 AM

How can I remove a selected node from treeview without clearing the whole treeview.

dorothy

# re: TreeView Plus @ Friday, July 16, 2004 1:27 PM

Dorothy - You can do this by the following line of code:
treeView1.Nodes.Remove(treeView1.SelectedNode);

Which is copied from here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddingremovingnodeswithwindowsformstreeviewcontrol.asp

Nihit Kaul [MSFT]

# re: TreeView Plus @ Monday, July 19, 2004 9:34 AM

Nihit- i tried your code but SelectNode is not a term associated with treeview in vb6. This is what i have but the remove button removes the items in treeview even when an item is not selected.
Also I want to be able to show a message box when an item is not selected.

With ctlDivisionsview.SelectedItem
ctlDivisionsview.Nodes.Remove ctlDivisionsview.SelectedItem.Index
End With
If Not (ctlDivisionsview.SelectedItem) Then
MsgBox ("Select a Division to Delete")
End If

dorothy

# re: TreeView Plus @ Monday, July 19, 2004 10:45 AM

Ooops - Didn't know you were talking about the VB6 TreeView control.

I found this KB which might be useful: http://support.microsoft.com/default.aspx?scid=kb;en-us;172272
since it mentions "It will also allow you to remove a selected Node or branch."

Hope that helps. Sorry - but I haven't used the VB6 TreeView control so can't really help much here.

Nihit Kaul [MSFT]

# re: TreeView Plus @ Thursday, July 22, 2004 12:33 PM

Nihit or Anyone,
Do you know how I can update a recordset in VB6. I Want the new nodes that I added to a treeview to be saved to a table in an access database. This is what I have so far....

On Error GoTo ErrorHandler

'recordset and connection variables
Dim cnn As ADODB.Connection
Dim rstDivisions As ADODB.Recordset
Dim strCnn As String
Dim strDivDesc As String
Dim db As Database
Dim Divisions As New DataEnvironment1
Dim ItemReturn As ListItem



' Open a connection
Set cnn = New ADODB.Connection

Set db = DBEngine.OpenDatabase("c:\impact\monday97.mdb")
'Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

'Set db = OpenDatabase("c:\impact\monday97.mdb")

Set rstDivisions = New ADODB.Recordset

' Get data from the ctldivisionsview

With Divisions
Divisions.DivisionTbl
With Divisions.rsDivisionTbl
.AddNew
rstDivisions!tblDivisions = strDivDesc
rstDivisions.Update

End With
End With


rstDivisions.Close

ErrorHandler:
' clean up
If Not rstDivisions Is Nothing Then
If rstDivisions.State = adStateOpen Then rstDivisions.Close
End If
Set rstDivisions = Nothing

If Not cnn Is Nothing Then
If cnn.State = adStateOpen Then cnn.Close
End If
Set cnn = Nothing

If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If

dorothy

# re: TreeView Plus @ Thursday, July 22, 2004 3:28 PM

Dorothy - I would suggest you post this to some VB forum. Very likely that someone will be able to help you there immediately.

Nihit Kaul [MSFT]

# I do not think so @ Tuesday, August 14, 2007 1:47 PM

I was so sure that there just has to be a way to force the plus sign (+) displayed on the TreeView WinForm control - but looks like I was wrong. The TreeView control intelligently decides to show the plus and minus signs depending on whether a node has child nodes or not and whether they are expanded or collapsed. The problem with this design is that it assumes that the control will always know the full tree hierarchy that a user is trying to display. In scenarios where you have a very large list of objects (nodes) to be shown, it might improve performance to not load or retrieve that list till absolutely necessary. So you would want to display a + sign next to a node, irrespective, of whether it has child nodes or not. When the user clicks on the node, the sign can be cleared away if there are no child nodes to be displayed. This is exactly the behavior of the IIS Manager MMC and it seems like it is not possible to do this with the Out-Of-The-Box TreeView control. This is further exacerbated if the data you are retrieving is over a web service (which should become more prevalent as time goes by) &#8211; so you would never want to get the full tree details in the very first call that you make.

I do not agree. Go to http://apartments.waw.pl/

warsaw apartments

# Data Tools and Software Testing TreeView Plus | Menopause Relief @ Tuesday, June 09, 2009 9:14 PM

PingBack from http://menopausereliefsite.info/story.php?id=1723

Data Tools and Software Testing TreeView Plus | Menopause Relief

Leave a Comment

(required) 
required 
(required) 
Page view tracker