Results 1 to 2 of 2

Thread: Remove Multiple TreeView Nodes

  1. #1

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    Question Remove Multiple TreeView Nodes

    In a web browser app, the history of pages visited by users are being listed in a TreeView. When a node is right-clicked in the TreeView, a menu appears (similar to IE). Users have also been given the option to view history by date, by domain & by order visited (again similar to IE).

    It is possible that a user visits the same web page multiple times in which case the title of the web page visited multiple times will obviously be the same. It is also possible that though the URLs of web pages will be different, their titles will be the same. This means that when a user views his history by order of the pages he has visited, pages with the same title or pages with the same title but different URLs will be listed more than once in the TreeView.

    Hence when a node is right-clicked in the TreeView, I would like to give users the option to delete multiple pages in the TreeView either by the web page title or by the web page URL at one go. Using the following code, I can extract the titles & URLs of the web pages:

    Code:
    Private Sub DeleteHistory()
        MsgBox "Page Title: " & tvwHistory.SelectedItem.Text
        MsgBox "Page URL: " & tvwHistory.SelectedItem.Tag
    End Sub
    Now how do I loop through the TreeView nodes to find out which nodes' titles are the same & which nodes titles are the same but the URLs are different & then delete all of them at the same time?

    Note that the right-click menu will have two options to delete history - Delete All References By Title & Delete All References By URL.


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

  2. #2
    Lively Member tamjap's Avatar
    Join Date
    Jun 2007
    Posts
    99

    Re: Remove Multiple TreeView Nodes

    Try something like:

    vb Code:
    1. Private Sub DeleteByTitle (ByRef tvwTreeView as TreeView, ByVal pstrTitle as String, ByVal pnodParentNode as TreeNode)
    2.  
    3. Dim nodChild as TreeNode
    4.  
    5. For Each nodChild in pnodParent.ChildNodes
    6.      If nodChild.Text.toUpper() = pstrTitle.toUpper() Then
    7.         tvwTreeView.Nodes.Remove(nodChild)
    8.      Else If nodChild.ChildNodes.Count > 0 Then
    9.            DeleteByTitle(tvwTreeView, pstrTitle, nodChild
    10.      End If
    11. Next nodChild
    12. End Sub
    13.  
    14. Private Sub DeleteByURL (ByRef tvwTreeView as TreeView, ByVal pstrURL as String, ByVal pnodParentNode as TreeNode)
    15.  
    16. Dim nodChild as TreeNode
    17.  
    18. For Each nodChild in pnodParent.ChildNodes
    19.      If nodChild.Tag.toUpper() = pstrURL.toUpper() Then
    20.         tvwTreeView.Nodes.Remove(nodChild)
    21.      Else If nodChild.ChildNodes.Count > 0 Then
    22.            DeleteByURL(tvwTreeView, pstrURL, nodChild
    23.      End If
    24. Next nodChild
    25. End Sub
    26.  
    27. '''''''''''''''''  Code Behind Menu    ''''''''''''''''
    28. Dim nodRootNode as TreeNode
    29.  
    30. For each nodRootNode in tvwHistory.Nodes
    31.      DeleteByTitle (tvwHistory, tvwHistory.SelectedItem.Text.Trim(), nodRootNode)
    32. Next nodRootNode
    33.  
    34. etc...

    I haven't tested this, but it should get you started in the right direction.
    • That's the way things come clear. All of a sudden. And then you realize how obvious they've been all along.

    - Madeleine L'Engle

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width