|
-
Jan 6th, 2003, 09:11 AM
#1
Thread Starter
Hyperactive Member
"On-the-fly" Treeview
I want my program to create a Treeview by reading a directory, any directories in that, and then any files inside those directories.
Basically, I'd like to program to create it's own treeview. The Main Directory is set (it's called Code) but then i'd like to program to look inside the Code directory and make a treeview of what's in there.
This is way above my head but there might be a kind soul out there 
This is desparetly needed to contine with my VBCodebook .NET
Last edited by RealNickyDude; Jan 17th, 2003 at 11:08 AM.
-
Jan 6th, 2003, 12:38 PM
#2
Member
Don't know if this could help at all but I was surfing and saw it.
http://www.devcity.net/forums/topic.asp?tid=17308
Eagle Eye
"Programming is easy ... when you are done."
-
Jan 6th, 2003, 10:04 PM
#3
PowerPoster
This is in C#, but it is exactly what you need. It is from a book I have. I am not going to convert it right now to VB (don't feel like it right now, may do it tomorrow for you), but you can just take the code, compile it into a control and use if from VB without having to convert it. Otherwise, you or someone else is going to have to convert it to VB.
Code:
//---------------------------------------------
// DirectoryTreeView.cs © 2001 by Charles Petzold
//---------------------------------------------
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
class DirectoryTreeView: TreeView
{
public DirectoryTreeView()
{
// Make a little more room for long directory names.
Width *= 2;
// Get images for tree.
// You need to put these images from the samples into your
// project directory for this part to work.
ImageList = new ImageList();
ImageList.Images.Add(new Bitmap(GetType(), "35FLOPPY.BMP"));
ImageList.Images.Add(new Bitmap(GetType(), "CLSDFOLD.BMP"));
ImageList.Images.Add(new Bitmap(GetType(), "OPENFOLD.BMP"));
// Construct tree.
RefreshTree();
}
public void RefreshTree()
{
// Turn off visual updating and clear tree.
BeginUpdate();
Nodes.Clear();
// Make disk drives the root nodes.
string[] astrDrives = Directory.GetLogicalDrives();
foreach (string str in astrDrives)
{
TreeNode tnDrive = new TreeNode(str, 0, 0);
Nodes.Add(tnDrive);
AddDirectories(tnDrive);
if (str == "C:\\")
SelectedNode = tnDrive;
}
EndUpdate();
}
void AddDirectories(TreeNode tn)
{
tn.Nodes.Clear();
string strPath = tn.FullPath;
DirectoryInfo dirinfo = new DirectoryInfo(strPath);
DirectoryInfo[] adirinfo;
try
{
adirinfo = dirinfo.GetDirectories();
}
catch
{
return;
}
foreach (DirectoryInfo di in adirinfo)
{
TreeNode tnDir = new TreeNode(di.Name, 1, 2);
tn.Nodes.Add(tnDir);
// We could now fill up the whole tree with this statement:
// AddDirectories(tnDir);
// But it would be too slow. Try it!
}
}
protected override void OnBeforeExpand(TreeViewCancelEventArgs tvcea)
{
base.OnBeforeExpand(tvcea);
BeginUpdate();
foreach (TreeNode tn in tvcea.Node.Nodes)
AddDirectories(tn);
EndUpdate();
}
}
-
Jan 7th, 2003, 04:53 AM
#4
Thread Starter
Hyperactive Member
Thanks EagleEye, but it's dealing with database's (I think!) I just need to read what's in a directory and make a treeview out of it.
-
Jan 7th, 2003, 04:56 AM
#5
Thread Starter
Hyperactive Member
This is in C#, but it is exactly what you need. It is from a book I have. I am not going to convert it right now to VB (don't feel like it right now, may do it tomorrow for you), but you can just take the code, compile it into a control and use if from VB without having to convert it. Otherwise, you or someone else is going to have to convert it to VB.
I would be most greatful if anyone could convert it to VB
-
Jan 7th, 2003, 02:11 PM
#6
Thread Starter
Hyperactive Member
-
Jan 8th, 2003, 03:45 AM
#7
Thread Starter
Hyperactive Member
No one can point me in the right direction?
-
Jan 8th, 2003, 04:14 AM
#8
Hyperactive Member
Ill Convert it today going ot be now will post later
-
Jan 8th, 2003, 06:47 AM
#9
Thread Starter
Hyperactive Member
Excellent 308holes! You're a life saver
-
Jan 9th, 2003, 05:50 PM
#10
Thread Starter
Hyperactive Member
Still no one has converted this The VBCodebook .NET will have to be hard coded then, which means you can't add your own code.
-
Jan 9th, 2003, 07:33 PM
#11
Frenzied Member
Here you go
VB Code:
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Public Class DirectoryTreeView
Inherits TreeView
Sub New()
'Make a little more room for long directory names.
Me.Width *= 2
'get images for tree
'You need to put these images from the samples into your
'project directory for this part to work.
Me.ImageList = New ImageList()
Me.ImageList.Images.Add(New Bitmap("35FLOPPY.BMP"))
Me.ImageList.Images.Add(New Bitmap("CLSDFOLD.BMP"))
Me.ImageList.Images.Add(New Bitmap("OPENFOLD.BMP"))
'Contruct tree
Me.RefreshTree()
End Sub
Public Sub RefreshTree()
'Turn off visual updating and clear tree.
Me.BeginUpdate()
Me.Nodes.Clear()
'Make disk drives the root node
Dim astrDrives() As String = Directory.GetLogicalDrives()
Dim str As String 'helper for For Each loop
For Each str In astrDrives
Dim tnDrive As New TreeNode(str, 0, 0)
Me.Nodes.Add(tnDrive)
Me.AddDirectories(tnDrive)
If (str = "C:\\") Then
Me.SelectedNode = tnDrive
End If
Next
Me.EndUpdate()
End Sub
Sub AddDirectories(ByVal tn As TreeNode)
tn.Nodes.Clear()
Dim strPath As String
Dim dirInfo As New DirectoryInfo(strPath)
Dim adirInfo() As DirectoryInfo
Try
adirInfo = dirInfo.GetDirectories()
Catch
Return
End Try
Dim di As DirectoryInfo 'helper for For Each loop
For Each di In adirInfo
Dim tnDir As New TreeNode(di.Name, 1, 2)
tn.Nodes.Add(tnDir)
'We could now fill up the whole tree with this statement:
' AddDirectories(tnDir);
'But it would be too slow. Try it!
Next
End Sub
Protected Overrides Sub OnBeforeExpand(ByVal tvcea As System.Windows.Forms.TreeViewCancelEventArgs)
MyBase.OnBeforeExpand(tvcea)
Me.BeginUpdate()
Dim tn As TreeNode 'helper for For Each loop
For Each tn In tvcea.Node.Nodes
Me.AddDirectories(tn)
Next
Me.EndUpdate()
End Sub
End Class
Dont gain the world and lose your soul
-
Jan 10th, 2003, 04:27 AM
#12
Thread Starter
Hyperactive Member
Thanks DevGrp, really appreciate it 
How would I use this? Do I just call DirectoryTreeView?
How do I set the root directory? I don't want the entire drive, I just want to look into a directory called code and make a treeview out of what's there.
Also, how to I skip certain files that I don't want shown but are in the directory I'm scanning?
-
Jan 10th, 2003, 06:10 AM
#13
Frenzied Member
That should go in a class file. Then you can go like this
Dim dtv as new DirectoryTreeView()
Dont gain the world and lose your soul
-
Jan 10th, 2003, 06:40 AM
#14
Thread Starter
Hyperactive Member
Thanks again DevGrp but now i'm going to sound like a total newbie dweeb-head and you have my full permission to stick sharp things up my nose, but....
Exactly, step-by-step, how would I use it? I can create the class but do I put the name of the directory I wish to start from, like this:
DirectoryTreeView(directoryname)?
I'm not sure how to use it, or how to tell it i want to start from a certain directory.
(...I'll dunk my own head down the toilet )
-
Jan 10th, 2003, 07:43 AM
#15
PowerPoster
Create a control project. Stick the code into it. Then compile it, and add it to your project. That code is actually a control that inherits from the treeview control. You can create an instance by code and such, but it would be easier to just use it like a normal control and let the IDE help you out.
As far as only letting you browse the directories you want, you are going to have to modify it somewhat to fit your needs.
-
Jan 11th, 2003, 07:17 AM
#16
Thread Starter
Hyperactive Member
How do I create a control project? Do I add a 'user control' or 'inherited control' or something else?
Do I then call it with Dim dtv as new DirectoryTreeView() as DevGrp mentioned? from within my program?
I'm sorry if I seem thicker than Bill Gate's wallet (is that possible!) but I know diddlysquat about classes, inheritence, user controls and the like.
This is how I read the Treeview:
VB Code:
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles _
TreeView1.AfterSelect
Select Case e.Node.Text
'File Handling
Case "Strip Dir from filepath"
Dim docPath As String = IO.Path.Combine (Application.StartupPath, "code\filehandling\stripfiledir.rtf")
rtbcodewindow.LoadFile(docPath, RichTextBoxStreamType.RichText)
StatusBar1.Text = "Code Section: File Handling - Stripping the dir from a filepath"
Case "Strip filename from filepath"
Dim docPath As String = IO.Path.Combine (Application.StartupPath, "code\filehandling\stripfilenamer.rtf")
rtbcodewindow.LoadFile(docPath, RichTextBoxStreamType.RichText)
StatusBar1.Text = "Code Section: File Handling - Stripping the filename from a filepath"
....
....
....
the more nodes i add, the more I have to hardcode. I basically want the program to make a treeview out of what's in a directory every time it's ran, hence code snippets could be added without having a hardcode check for each one.
Thanks for your help...
... and your patience
Last edited by RealNickyDude; Jan 11th, 2003 at 07:23 AM.
-
Jan 11th, 2003, 08:04 AM
#17
Frenzied Member
If someone does'nt come up with something by the end of the day, I'll code something up when I get home later.
Dont gain the world and lose your soul
-
Jan 11th, 2003, 08:44 AM
#18
Thread Starter
Hyperactive Member
-
Jan 12th, 2003, 06:25 PM
#19
Hyperactive Member
My advice would to be Learn some more then Make the Code Book and just waite for (ISC) to come out :-)
-
Jan 12th, 2003, 06:34 PM
#20
PowerPoster
308holes - What is ISC?
RealNickyDude - Why don't you take a look at this link.
http://www.msdn.microsoft.com/librar...wfccontrol.asp
It will get you started on creating controls and such. Also if you need some place to learn about .Net and what you can do, you should visit http://www.msdn.microsoft.com/library/
-
Jan 12th, 2003, 07:30 PM
#21
Frenzied Member
Here ya go. I did a sample to load the files in the Treeview on the fly. Its in C# though. Source is included.
Link
I think ISC is a program Edneesis is working on.
Have fun .
Dont gain the world and lose your soul
-
Jan 13th, 2003, 04:30 AM
#22
Junior Member
Last edited by NickyDude; Jan 13th, 2003 at 04:34 AM.
-
Jan 13th, 2003, 04:56 AM
#23
Thread Starter
Hyperactive Member
Nickydude - RealyNickyDude (one and the same)
-
Jan 13th, 2003, 08:21 PM
#24
Thread Starter
Hyperactive Member
-
Jan 16th, 2003, 12:32 PM
#25
Thread Starter
Hyperactive Member
P.L.E.A.S.E!!!!!!
-
Jan 16th, 2003, 01:38 PM
#26
Frenzied Member
I'll do it when I get home.
Dont gain the world and lose your soul
-
Jan 16th, 2003, 01:48 PM
#27
Thread Starter
Hyperactive Member
Please could you, this is the one thing i'm desparate for, this is the reason I'm not going full out on the VBCodebook .NET. Once this is sorted, it will all be go, go, go!
If you do, I can't thank you enough!
-
Jan 16th, 2003, 08:12 PM
#28
Frenzied Member
Dont gain the world and lose your soul
-
Jan 17th, 2003, 11:07 AM
#29
Thread Starter
Hyperactive Member
Brilliant DevGrp, exactly what I was looking for.
Just one, unimportant question, how do I change the icon for the Treeview headings, (File Handling, Graphics, Text, those ones) to different ones in an imagelist?
-
Jan 18th, 2003, 02:32 AM
#30
Hyperactive Member
Talk to Edneeis about ISC
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|