14 Replies - 2500 Views - Last Post: 22 April 2007 - 08:54 PM Rate Topic: -----

#1 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Treeview Glitch

Posted 18 April 2007 - 06:12 PM

My treeview is designed to display files added to the directory by the user.
It works, but you have to click on the directory to make it show the file. AND if you click a different directory then back to the first, it duplicates the files listed.

Any Ideas ?

	Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		Dim Tnode As TreeNode = TreeView1.Nodes.Add("Additional Files")
		AddAllFolders(Tnode, ".\wiikeysource\bootdisc\root\")
		TreeView1.ExpandAll()
	End Sub

	Public Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
		Try
			For Each FolderNode As String In Directory.GetDirectories(FolderPath)
				Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))

				SubFolderNode.Tag = FolderNode

				SubFolderNode.Nodes.Add("Loading...")
			Next
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Sub

	Public Sub Treeview1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
		If TreeView1.SelectedNode.Nodes.Count = 1 AndAlso TreeView1.SelectedNode.Nodes(0).Text = "Loading..." Then

			TreeView1.SelectedNode.Nodes.Clear()

			AddAllFolders(TreeView1.SelectedNode, CStr(TreeView1.SelectedNode.Tag))

		End If

		Dim folder As String = CStr(e.Node.Tag)

		If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
			Try
				For Each file As String In IO.Directory.GetFiles(folder)
					e.Node.Nodes.Add(file.Substring(file.LastIndexOf("\"c) + 1))

				Next
			Catch ex As Exception
				MsgBox(ex.Message)

			End Try
		End If
	End Sub

	Public Sub Treeview1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand

		If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then

			e.Node.Nodes.Clear()
			AddAllFolders(e.Node, CStr(e.Node.Tag))

		End If
	End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Treeview Glitch

#2 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Treeview Glitch

Posted 19 April 2007 - 11:50 AM

I've been playing with this for hours and still can't get the glitch to go away.

Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Treeview Glitch

Posted 19 April 2007 - 12:43 PM

Can you upload a screen shot of what you are describing?
Was This Post Helpful? 0
  • +
  • -

#4 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Treeview Glitch

Posted 19 April 2007 - 01:34 PM

Here's what its doing:

You select a file using the button on the right, after the file select dialog is done the file doesnt show up under the list untill you click the folder. (thats the first porblem, i want it to show the new file right away)
Posted Image

You click on a different folder "media".
Posted Image

You click the other folder again and it duplicates the listing (this is the MAIN problem). Even though there is actually only 1 of the files in the directory.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Treeview Glitch

Posted 21 April 2007 - 09:05 AM

bump
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Treeview Glitch

Posted 21 April 2007 - 01:19 PM

By default when you add to the tree you need to Expand at that time to see your file automatically. I would suggest inside the click event of your Add button to use the ExpandAll method as the last statement.
TreeView.ExpandAll


Not sure what you are trying to do here. But this may be where your duplicate problem is occurring. Try clearing the selected Node before adding to it.
		If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then

			TreeView1.Nodes.Clear()

			Try
				For Each file As String In IO.Directory.GetFiles(folder)
					e.Node.Nodes.Add(file.Substring(file.LastIndexOf("\"c) + 1))

				Next
			Catch ex As Exception
				MsgBox(ex.Message)

			End Try


If these don't work, then if you can, zip up your project and upload to this topic and I can take a closer look.
Was This Post Helpful? 0
  • +
  • -

#7 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Treeview Glitch

Posted 21 April 2007 - 03:56 PM

I used this to fix the duplication problem: (THANKS!)

Public Sub Treeview1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
		If TreeView1.SelectedNode.Nodes.Count = 1 AndAlso TreeView1.SelectedNode.Nodes(0).Text = "Loading..." Then

			TreeView1.SelectedNode.Nodes.Clear()

			AddAllFolders(TreeView1.SelectedNode, CStr(TreeView1.SelectedNode.Tag))

		End If

		Dim folder As String = CStr(e.Node.Tag)

		If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then


Added: TreeView1.SelectedNode.Nodes.Clear()

			Try
				For Each file As String In IO.Directory.GetFiles(folder)
					e.Node.Nodes.Add(file.Substring(file.LastIndexOf("\"c) + 1))

				Next
			Catch ex As Exception
				MsgBox(ex.Message)

			End Try
		End If
	End Sub


But I still can't get the list to update after button click event. I'm pretty sure that because this function is written as a sub for a "afterselect" event, that with what I have it will never update unless I click the child in the treeview.

So, how would I transfer this action to a last event in a button click event?

This post has been edited by unholyfire: 21 April 2007 - 03:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Treeview Glitch

Posted 21 April 2007 - 05:42 PM

I don't know why I didn't see this right off the bat, but the problem is that you never actually load the files into your tree from the folders. You load all the folders and then thats it.

To fix this problem you need another For/Each loop to traverse the contents of each folder adding the items to the treeview as you add folders to the tree. Also, I removed the Loading message, otherwise you will always have it at the end of your childnode.

	Public Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
		Try
			For Each FolderNode As String In Directory.GetDirectories(FolderPath)
				Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))

				SubFolderNode.Tag = FolderNode

				'add the files inside each folder to the TreeView
				For Each childNode As String In Directory.GetFiles(FolderNode)
					Dim subChildNode As TreeNode = SubFolderNode.Nodes.Add(childNode.Substring(childNode.LastIndexOf("\"c) + 1))
				Next childNode

			Next FolderNode
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Sub

Was This Post Helpful? 0
  • +
  • -

#9 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Treeview Glitch

Posted 22 April 2007 - 03:43 PM

I got this code from a snippet and adapted it to my app.
It came with the "treeview1_afterselect" sub and thats the event that triggers the listing. It works, but you have to click on the folder before it displays the new items.

Can I simply move this action to a new event?
Plainly I just want the list to update after the user selects the files to add. Tryed the "treeview1.expandall()" as the last statement in the button click event, and the openfiledialog event.. neither makes it do what clicking on the folder does.

Seems like this really shouldn't be that hard. But I just dont have the knowledge and experience.

Is there a way to force the treeview1_afterselect event at the end of the button click event? If there is, that would seem to be the quickest way to resolve this.
Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Treeview Glitch

Posted 22 April 2007 - 05:01 PM

I guess at this point I'm a little confused as to what result you are trying to achieve.

Is the user manually adding files to the Treeview?

I have removed the AfterSelect event from the code in your project and the result is a TreeView of the directory I am pointing to in my code, which lists the folders and files in the directories. The AfterSelect event is intended to show the files only after you select the folder in the TreeView.

Is that your intention?

The following screenshot shows the folders and files immediately after the application loads without me having to do anything.
Don't mind the extra controls on the form, I just use this one to quickly trouble shoot other peoples code.

Here is the code I am currently using:
Imports System.IO

Public Class Form1

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		Dim Tnode As TreeNode = TreeView1.Nodes.Add("Additional Files")
		AddAllFolders(Tnode, "C:\Downloads")
		TreeView1.ExpandAll()


	End Sub

	Public Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
		Try
			For Each FolderNode As String In Directory.GetDirectories(FolderPath)
				Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))

				SubFolderNode.Tag = FolderNode

				'add the files inside each folder to the TreeView
				For Each childNode As String In Directory.GetFiles(FolderNode)
					Dim subChildNode As TreeNode = SubFolderNode.Nodes.Add(childNode.Substring(childNode.LastIndexOf("\"c) + 1))
				Next childNode

			Next FolderNode
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Sub

	Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
		If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then

			e.Node.Nodes.Clear()
			AddAllFolders(e.Node, CStr(e.Node.Tag))

		End If
	End Sub


End Class

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#11 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Treeview Glitch

Posted 22 April 2007 - 07:57 PM

Thats perfect, but now I need that list to update after the user clicks the button (starts the openfiledialog, user selects a file and clicks ok, selected file gets copied to its desinated directory -??and is now needs to be added in the treeview??)

I've tried every "treeview1.*" posible at the end of the button click routine (update, refresh, expandall... ... ...) but none will make the treeview display the file newly added file.


Posibly it's becuase the routine is copying a file from one location to another, and it simply hasn't caught up? (some files are gonna take some time).

I'm running out of ideas but that last one seems the most likely.
So now, I guess I will investigate how to tell the action to wait till the file is actually pressent, then move to the next step "treeview1.expandall".

Does that sound logical?
Was This Post Helpful? 0
  • +
  • -

#12 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Treeview Glitch

Posted 22 April 2007 - 08:14 PM

Just thought about adding a timer that continuosly rechecks the folder contents and updates the treeview accordingly. Think this would solve my problem?
Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Treeview Glitch

Posted 22 April 2007 - 08:36 PM

Ok, I understand what you want.

See my next post for what I was trying to explain, which I did poorly in this post.
Was This Post Helpful? 0
  • +
  • -

#14 unholyfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 10-April 07

Re: Treeview Glitch

Posted 22 April 2007 - 08:44 PM

Well it's taking alot of extra code per subroutine (each add file/remove file button).
But I added this to the end of each one and its working PERFECTLY!

TreeView1.Nodes.Clear()
			Try
				Dim folderpath As String = ".\wiikeysource\bootdisc\root\"
				For Each FolderNode As String In Directory.GetDirectories(folderpath)
					Dim SubFolderNode As TreeNode = TreeView1.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))

					SubFolderNode.Tag = FolderNode

					'add the files inside each folder to the TreeView
					For Each childNode As String In Directory.GetFiles(FolderNode)
						Dim subChildNode As TreeNode = SubFolderNode.Nodes.Add(childNode.Substring(childNode.LastIndexOf("\"c) + 1))
					Next childNode

				Next FolderNode
			Catch ex As Exception
				MsgBox(ex.Message)
			Finally
				TreeView1.ExpandAll()
			End Try



I wouldn't have realised how to do this without your guidance (and snippets) so thanks so much.
If you feal this isn't a good way to do this and wanna suggest an alternative, by all meens please do LOL.

Anyhow.. Thanks for all your help!
Was This Post Helpful? 0
  • +
  • -

#15 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Treeview Glitch

Posted 22 April 2007 - 08:54 PM

Sorry I was actually editing my post when you viewed it to tell you the following.

If you are going to be duplicating this code in multiple click events and your load event then you might want to create a procedure put all the code inside there and then just call the procedure from your Load event and all of your click events. This will save you from duplicating the code several times.

Something like this:
	Private Sub LoadNodes()
		TreeView1.Nodes.Clear()
		Dim Tnode As TreeNode = TreeView1.Nodes.Add("Additional Files")
		AddAllFolders(Tnode, ".\wiikeysource\bootdisc\root\")
		TreeView1.ExpandAll()
	End Sub



Now all you need to do is call LoadNodes() anyplace you need to update or create the nodes.

Anytime you need to duplicate code it is always best to create a procedure or function and then just make a call to the procedure/function.

You want to eliminate any redundancy in your code. This way if you ever need to modify your code then you only need to go to one place to modify.

Here is how I would recommend you do it.
Imports System.IO

Public Class Form1

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		LoadNodes()

	End Sub

	Public Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)


		Try
			For Each FolderNode As String In Directory.GetDirectories(FolderPath)
				Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))

				SubFolderNode.Tag = FolderNode

				'add the files inside each folder to the TreeView
				For Each childNode As String In Directory.GetFiles(FolderNode)
					Dim subChildNode As TreeNode = SubFolderNode.Nodes.Add(childNode.Substring(childNode.LastIndexOf("\"c) + 1))
				Next childNode

			Next FolderNode
		Catch ex As Exception
			MsgBox(ex.Message)
		End Try
	End Sub

	Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
		If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then

			e.Node.Nodes.Clear()
			AddAllFolders(e.Node, CStr(e.Node.Tag))

		End If
	End Sub


	Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

		LoadNodes()

	End Sub

	Private Sub LoadNodes()
		TreeView1.Nodes.Clear()
		Dim Tnode As TreeNode = TreeView1.Nodes.Add("Additional Files")
		AddAllFolders(Tnode, "C:\Downloads")
		TreeView1.ExpandAll()
	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1