Solved

dynamic contect menu from directory structure

Posted on 2009-05-18
2
454 Views
Last Modified: 2013-12-26
Should be simple, but I am drawing a blank. I would like to build a winform context menu off of traget folder from my system. So say I have folder [C:\something\] and within it there is the following:
C:\something\subfolder\
C:\something\subfolder\something1.txt
C:\something\subfolder\something3.txt
C:\something\subfolder\subfolder2\xxx.txt
C:\something\123.txt

I like to build the context menu to mimic the above showing just the file names or folder names. whenever it hits a sub folder i need a new node on the menu with more sub items.

Any ideas?

VB 2K5 or C# - either would be fine.

thanks in advance.
0
Comment
Question by:TheRoyalFalcon
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24417295
Here is a simple example...
Public Class Form1
 

    Private WithEvents cms As New ContextMenuStrip

    Private MyDocumentsSubFolder As String = "ContextMenuTest"

    Private SourceFolder As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, MyDocumentsSubFolder)
 

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

        Me.ContextMenuStrip = cms

    End Sub
 

    Private Sub cms_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cms.Opening

        cms.Items.Clear()

        BuildMenu(New System.IO.DirectoryInfo(SourceFolder))

        e.Cancel = False ' <-- YES...this was necessary!  Without it, the menu would not appear on the FIRST Right Click.

    End Sub
 

    Private Sub BuildMenu(ByVal di As System.IO.DirectoryInfo, Optional ByVal tsmi As ToolStripMenuItem = Nothing)

        Try

            For Each fi As System.IO.FileInfo In di.GetFiles

                Dim mi As New ToolStripMenuItem(fi.Name)

                mi.Tag = fi.FullName

                AddHandler mi.Click, AddressOf tsmi_Click

                If IsNothing(tsmi) Then

                    cms.Items.Add(mi)

                Else

                    tsmi.DropDownItems.Add(mi)

                End If

                Application.DoEvents()

            Next

        Catch ex As Exception

        End Try
 

        Try

            For Each subDi As System.IO.DirectoryInfo In di.GetDirectories

                Dim mi As New ToolStripMenuItem(subDi.Name)

                If IsNothing(tsmi) Then

                    cms.Items.Add(mi)

                Else

                    tsmi.DropDownItems.Add(mi)

                End If

                BuildMenu(subDi, mi)

                Application.DoEvents()

            Next

        Catch ex As Exception

        End Try

    End Sub
 

    Private Sub tsmi_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim tsmi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)

        If Not IsNothing(tsmi.Tag) AndAlso TypeOf tsmi.Tag Is String Then

            Process.Start(tsmi.Tag)

        End If

    End Sub
 

End Class

Open in new window

DynamicFileContextMenu.jpg
0
 
LVL 1

Author Closing Comment

by:TheRoyalFalcon
ID: 31582738
This worked like a charm. Thank you SO much. It is just what I needed. Thanks again!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now