Udostępnij za pośrednictwem


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?

Comments

  • Anonymous
    April 10, 2004
    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)?
  • Anonymous
    April 10, 2004
    The comment has been removed
  • Anonymous
    April 10, 2004
    Dummy item - only way.
  • Anonymous
    April 10, 2004
    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 :)
  • Anonymous
    April 10, 2004

    Can you think showing the registry content without using dummy treeitems at each level, so that the next level is only expanded upon request ?
  • Anonymous
    April 10, 2004
    Agree that dummy items are okay. They were the only way in VB6 as well, so many coders are familiar with it ;)
  • Anonymous
    April 11, 2004
    If it helps make you feel any better, I know of several Microsoft products that use the dummy item route in their code.
  • Anonymous
    April 11, 2004
    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.
  • Anonymous
    April 12, 2004
    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).
  • Anonymous
    April 12, 2004
    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.
  • Anonymous
    April 13, 2004
    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.
  • Anonymous
    July 16, 2004
    How can I remove a selected node from treeview without clearing the whole treeview.
  • Anonymous
    July 16, 2004
    The comment has been removed
  • Anonymous
    July 19, 2004
    The comment has been removed
  • Anonymous
    July 19, 2004
    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.
  • Anonymous
    July 22, 2004
    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:impactmonday97.mdb")
    'Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

    'Set db = OpenDatabase("c:impactmonday97.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
  • Anonymous
    July 22, 2004
    Dorothy - I would suggest you post this to some VB forum. Very likely that someone will be able to help you there immediately.
  • Anonymous
    August 14, 2007
    The comment has been removed