Populate TreeView Again Without Reloading
The history of the web pages visited by users in a WebBrowser app is displayed in a TreeView. Like IE, when a CommandButton is clicked, the TreeView becomes visible. Clicking the CommandButton again hides the TreeView.
Each node in the TreeView has an unique key which is why if the user wants to view his history more than once, on subsequent views, all the nodes in the TreeView are first removed & then the TreeView is again populated with the same nodes. If I don't remove the nodes first, then it generates an error saying key is not unique.
Is there a way by which I can avoid the extra work of deleting all the nodes & re-populating the TreeView with the same nodes on subsequent views (maybe saving all the nodes somewhere so that the uniqueness of the keys of the nodes can be maintained)?
Re: Populate TreeView Again Without Reloading
Can someone please help me out with this?
Re: Populate TreeView Again Without Reloading
First of all, please don't bump your threads.
I'm a little confused. You say you have a button that hides/unhides the treeview. Why does the unique key requirement have anything to do with that?
Re: Populate TreeView Again Without Reloading
The reason unique key comes into the picture is this - all the URLs visited by a user on a particular date are populated in a text file. This text file resides in a folder whose name is the same as the date. For e.g. if I surf the Net today, a folder named 20080117 will get created. A text file named History.txt will be created in this folder. All web pages visited today will be populated in this text file. Similarly, if I surf the Net tomorrow, a folder named 20080118 will get created. A text file named History.txt will be created in this folder. All web pages I visit tomorrow will be populated in this text file.
The parent node is each of the dates & the child nodes are the URLs visited by the user on that date. The Key of each parent node is the folder name followed by History.txt. So the Key of the parent node 17.01.2008 will be 20080117/History.txt. Similarly, the Key of the parent node 18.01.2008 will be 20080118/History.txt.
A user clicks the button; the history TreeView becomes visible & the TreeView is populated with the URLs. He again clicks the button which hides the TreeView. Again he clicks the button to make the TreeView visible. Here I populate the TreeView again with the dates as the parent node & the URLs as the child nodes.
Now if I don't have a unique key for each parent node, when the user makes the TreeView visible for the 2nd time, it will generate the Key is not unique in collection error. This is the reason I am first deleting all the nodes in the TreeView & then again populating the TreeView.
What I want is if there is a way to save the TreeView nodes somewhere after it is loaded for the 1st time so that when the TreeView becomes visible subsequently, I don't have to delete all the nodes & then re-populate the TreeView.
Re: Populate TreeView Again Without Reloading
Quote:
Originally Posted by arpan_de
...A user clicks the button; the history TreeView becomes visible & the TreeView is populated with the URLs. He again clicks the button which hides the TreeView. Again he clicks the button to make the TreeView visible. Here I populate the TreeView again with the dates as the parent node & the URLs as the child nodes....
How about this instead.
...A user clicks the button; the history TreeView becomes visible & if the bFirstTime boolean is True then the TreeView is populated with the URLs and the bFirstTime boolean is set to False. He again clicks the button which hides the TreeView. Again he clicks the button to make the TreeView visible. Here I populate the TreeView again with the dates as the parent node & the URLs as the child nodes only if the bFirstTime boolean is True....
Re: Populate TreeView Again Without Reloading
BTW, how about using simply On Error Resume Next instead of introducing a boolean variable?
Re: Populate TreeView Again Without Reloading
Usually a bad idea since it can hide errors that you want to know about. What's wrong with a simple boolean?
Re: Populate TreeView Again Without Reloading
OK....this is what I finally did:
Code:
Dim blnFirstTime As Boolean
Private Sub cmdHistory_Click()
If (blnFirstTime = False) Then
blnFirstTime = True
Else
blnFirstTime = False
End If
If (TreeView1.Visible = False) Then
TreeView1.Visible = True
'populate the TreeView
Else
TreeView1.Visible = False
blnFirstTime = True
End If
End Sub
Re: Populate TreeView Again Without Reloading
If that's the way you actually implemented my suggestion and assuming that you actually have code where you now just show the "populate the TreeView" comment, I don't see how it helps you since every time the treeview becomes visible you (re)populate the treeview. Don't your really want to do this instead?
Code:
Option Explicit
Dim blnFirstTime As Boolean
Private Sub Form_Load()
blnFirstTime = True
End Sub
Private Sub cmdHistory_Click()
TreeView1.Visible = Not TreeView1.Visible
If blnFirstTime Then
'populate the TreeView
blnFirstTime = False
End If
End Sub