Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dynamic contect menu from directory structure

Posted on 2009-05-18
2
Medium Priority
?
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

670 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