Results 1 to 30 of 30

Thread: "On-the-fly" Treeview

  1. #1

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435

    Question "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.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  2. #2
    Member EagleEye's Avatar
    Join Date
    May 2002
    Location
    South Carolina, USA
    Posts
    43
    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."

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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();
         }
    }

  4. #4

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Originally posted by EagleEye
    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
    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.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  5. #5

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  6. #6

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  7. #7

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    No one can point me in the right direction?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  8. #8
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Ill Convert it today going ot be now will post later

  9. #9

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Excellent 308holes! You're a life saver
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  10. #10

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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.

    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  11. #11
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Here you go
    VB Code:
    1. Imports System
    2. Imports System.Drawing
    3. Imports System.IO
    4. Imports System.Windows.Forms
    5.  
    6. Public Class DirectoryTreeView
    7.     Inherits TreeView
    8.  
    9.     Sub New()
    10.         'Make a little more room for long directory names.
    11.         Me.Width *= 2
    12.  
    13.         'get images for tree
    14.         'You need to put these images from the samples into your
    15.         'project directory for this part to work.
    16.         Me.ImageList = New ImageList()
    17.         Me.ImageList.Images.Add(New Bitmap("35FLOPPY.BMP"))
    18.         Me.ImageList.Images.Add(New Bitmap("CLSDFOLD.BMP"))
    19.         Me.ImageList.Images.Add(New Bitmap("OPENFOLD.BMP"))
    20.  
    21.         'Contruct tree
    22.         Me.RefreshTree()
    23.     End Sub
    24.  
    25.     Public Sub RefreshTree()
    26.         'Turn off visual updating and clear tree.
    27.         Me.BeginUpdate()
    28.         Me.Nodes.Clear()
    29.  
    30.         'Make disk drives the root node
    31.         Dim astrDrives() As String = Directory.GetLogicalDrives()
    32.  
    33.         Dim str As String 'helper for For Each loop
    34.  
    35.         For Each str In astrDrives
    36.             Dim tnDrive As New TreeNode(str, 0, 0)
    37.             Me.Nodes.Add(tnDrive)
    38.             Me.AddDirectories(tnDrive)
    39.  
    40.             If (str = "C:\\") Then
    41.                 Me.SelectedNode = tnDrive
    42.             End If
    43.         Next
    44.         Me.EndUpdate()
    45.     End Sub
    46.  
    47.     Sub AddDirectories(ByVal tn As TreeNode)
    48.         tn.Nodes.Clear()
    49.  
    50.         Dim strPath As String
    51.         Dim dirInfo As New DirectoryInfo(strPath)
    52.         Dim adirInfo() As DirectoryInfo
    53.  
    54.         Try
    55.             adirInfo = dirInfo.GetDirectories()
    56.         Catch
    57.             Return
    58.         End Try
    59.  
    60.         Dim di As DirectoryInfo 'helper for For Each loop
    61.  
    62.         For Each di In adirInfo
    63.             Dim tnDir As New TreeNode(di.Name, 1, 2)
    64.             tn.Nodes.Add(tnDir)
    65.  
    66.             'We could now fill up the whole tree with this statement:
    67.             '         AddDirectories(tnDir);
    68.             'But it would be too slow. Try it!
    69.         Next
    70.     End Sub
    71.  
    72.     Protected Overrides Sub OnBeforeExpand(ByVal tvcea As System.Windows.Forms.TreeViewCancelEventArgs)
    73.         MyBase.OnBeforeExpand(tvcea)
    74.  
    75.         Me.BeginUpdate()
    76.  
    77.         Dim tn As TreeNode 'helper for For Each loop
    78.  
    79.         For Each tn In tvcea.Node.Nodes
    80.             Me.AddDirectories(tn)
    81.         Next
    82.  
    83.         Me.EndUpdate()
    84.     End Sub
    85. End Class
    Dont gain the world and lose your soul

  12. #12

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  13. #13
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  14. #14

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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 )
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  15. #15
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  16. #16

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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:
    1. Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles _
    2. TreeView1.AfterSelect
    3.    Select Case e.Node.Text
    4.  
    5.        'File Handling
    6.         Case "Strip Dir from filepath"
    7.           Dim docPath As String = IO.Path.Combine (Application.StartupPath, "code\filehandling\stripfiledir.rtf")
    8.           rtbcodewindow.LoadFile(docPath, RichTextBoxStreamType.RichText)
    9.           StatusBar1.Text = "Code Section: File Handling - Stripping the dir from a filepath"
    10.  
    11.          Case "Strip filename from filepath"
    12.             Dim docPath As String = IO.Path.Combine (Application.StartupPath, "code\filehandling\stripfilenamer.rtf")
    13.             rtbcodewindow.LoadFile(docPath, RichTextBoxStreamType.RichText)
    14.             StatusBar1.Text = "Code Section: File Handling - Stripping the filename from a filepath"
    15. ....
    16. ....
    17. ....
    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.
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  17. #17
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  18. #18

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  19. #19
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    My advice would to be Learn some more then Make the Code Book and just waite for (ISC) to come out :-)

  20. #20
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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/

  21. #21
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  22. #22
    Junior Member
    Join Date
    Jul 2002
    Posts
    31
    RealNickyDude - Why don't you take a look at this link.
    http://www.msdn.microsoft.com/libra...mwfccontrol.asp
    • Note: The Windows Control Library template is not available in the Standard Edition of Visual Basic and Visual C# .NET.

    Unfortunately, I only have VB Standard

    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/
    Thanks, will do.

    Here ya go. I did a sample to load the files in the Treeview on the fly. Its in C# though. Source is included.
    This is exactly what I wanted except (see above comment) I don't have C# to view the source

    Seems having VB .NET Standard is very limiting, but that's all I could afford
    Last edited by NickyDude; Jan 13th, 2003 at 04:34 AM.

  23. #23

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    Nickydude - RealyNickyDude (one and the same)
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  24. #24

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435

    Talking

    Originally posted by DevGrp
    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 .
    Could someone be a sweetie and convert this to VB so I can have a look at it

    ...pretty please
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  25. #25

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    P.L.E.A.S.E!!!!!!
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  26. #26
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I'll do it when I get home.
    Dont gain the world and lose your soul

  27. #27

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435

    Talking

    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!
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  28. #28
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Dont gain the world and lose your soul

  29. #29

    Thread Starter
    Hyperactive Member RealNickyDude's Avatar
    Join Date
    Nov 2002
    Location
    Watching you through the hidden camera :o
    Posts
    435
    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?
    [vbcode]
    On Error GoTo Hell
    [/vbcode]:¬) Nicky : Why not try VBCodebook.NET.

  30. #30
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    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
  •  



Click Here to Expand Forum to Full Width