I've been trying to figure out the easiest way to populate a menu to allow for easy updating in the future. I would like to add anything to a specific folder and when the program runs it would be populated by the contents of the specific folder.

Is this possible? What should I look into to do this? Not sure of the process to use to do this, that is if it is even possible.

Dani AI

Generated

A compact, reliable pattern is to parse each toolbox INI into a small object model, group entries by section, then build parent menu nodes (categories) and child ToolStripMenuItems (apps). This complements ’s parsing idea and answers ’s desire for editable categories without touching source: store Name, Path and Description in the INI, allow relative paths (combined with the toolbox folder), ignore blank/comment lines, and validate before launching.

A concise parser that is tolerant of comments and missing keys:

Public Class ToolEntry
    Public Property Category As String
    Public Property Name As String
    Public Property ExePath As String
    Public Property Description As String
End Class

Function ParseIni(filePath As String) As List(Of ToolEntry)
    Dim list As New List(Of ToolEntry)
    Dim lines = IO.File.ReadAllLines(filePath)
    Dim currentCategory = "General"
    Dim currentEntry As ToolEntry = Nothing
    For Each ln In lines
        Dim s = ln.Trim()
        If s = "" OrElse s.StartsWith(";") OrElse s.StartsWith("#") Then Continue For
        If s.StartsWith("[") AndAlso s.EndsWith("]") Then
            currentCategory = s.Substring(1, s.Length - 2)
            Continue For
        End If
        Dim pos = s.IndexOf("="c)
        If pos < 0 Then Continue For
        Dim key = s.Substring(0, pos).Trim().ToLowerInvariant()
        Dim val = s.Substring(pos + 1).Trim()
        Select Case key
            Case "application name", "name"
                currentEntry = New ToolEntry With {.Category = currentCategory, .Name = val}
                list.Add(currentEntry)
            Case "application path to exe", "path", "exe"
                If currentEntry IsNot Nothing Then
                    currentEntry.ExePath = If(IO.Path.IsPathRooted(val), val, IO.Path.Combine(Application.StartupPath, "toolbox", val))
                End If
            Case "description"
                If currentEntry IsNot Nothing Then currentEntry.Description = val
        End Select
    Next
    Return list
End Function

Populate the menu and launch safely:

Sub PopulateMenu(root As ToolStripMenuItem, entries As List(Of ToolEntry))
    root.DropDownItems.Clear()
    For Each grp In entries.GroupBy(Function(e) e.Category)
        Dim cat = New ToolStripMenuItem(grp.Key)
        For Each e In grp
            Dim mi = New ToolStripMenuItem(e.Name) With {.Tag = e.ExePath, .ToolTipText = e.Description}
            AddHandler mi.Click, AddressOf LaunchTool
            cat.DropDownItems.Add(mi)
        Next
        root.DropDownItems.Add(cat)
    Next
End Sub

Sub LaunchTool(sender As Object, ea As EventArgs)
    Dim mi = DirectCast(sender, ToolStripMenuItem)
    Dim path = TryCast(mi.Tag, String)
    If String.IsNullOrEmpty(path) OrElse Not IO.File.Exists(path) Then Return
    Try
        Process.Start(path)
    Catch ex As Exception
        MessageBox.Show("Could not start: " & ex.Message)
    End Try
End Sub

Notes and best practices: add a ContextMenuStrip with an "Edit" action (as suggested) that edits the INI and rewrites it, then re-parse. Use a FileSystemWatcher on the toolbox folder to reload menus when INI files change. Validate executable paths before starting (avoid running arbitrary paths from a shared folder), and wrap UI updates from watcher callbacks with Invoke/BeginInvoke.

Recommended Answers

All 8 Replies

See if this helps to add DropDownItems or Items to a menu.
1 MenuStrip(with a "File" menu item)

Public Class Form1
    Private myCoolFolder As String = "C:\"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each myFile In My.Computer.FileSystem.GetFiles(myCoolFolder, FileIO.SearchOption.SearchTopLevelOnly, "*.*") '// get files.
            FileToolStripMenuItem.DropDownItems.Add(myFile) '// add as DropDownItems.
            ' MenuStrip1.Items.Add(myFile)'// add as Category on Menu.
        Next
    End Sub
End Class

codeorder,
Thanks for the pointing in the general direction. I'm going to post my code below for anyone else that might need help with this also. The files are going to be .ini's that I'm scanning. Is there a way to put comments in the .ini's that the reader will ignore?

'LOAD Toolbox Menu
    Private Sub LoadToolboxMenu()
        'Load Initial Folders
        'Looks for toolboxCats.ini
        Dim File_Name As String = Application.StartupPath & "\toolbox\toolboxcats.ini"

        If System.IO.File.Exists(File_Name) = True Then
            Dim objReader As New System.IO.StreamReader(File_Name)
            Do While objReader.Peek() <> -1
                ToolStripDropDownButton1.DropDownItems.Add(objReader.ReadLine())
            Loop
        End If

        'Load Base Folders
        For Each myFile In My.Computer.FileSystem.GetFiles(ToolboxScanFolder, FileIO.SearchOption.SearchAllSubDirectories, "toolbox.ini")

        Next
    End Sub

I want to be able to have the following information stored in my .ini files and then pull it as needed.

- Documentation
- Application Name
- Application Path to EXE
- Description

>>Is there a way to put comments in the .ini's that the reader will ignore?
I believe that comment lines for a .ini file start with ";".

[category title 1]
;Documentation=kewl
Application Name=VS Professional
Application Path to EXE=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe
Description=Software developing product

[category title 2]
;Documentation=kewl also
Application Name=Firefox
Application Path to EXE=C:\Program Files (x86)\Mozilla Firefox\firefox.exe
Description=Web browser of choice

I would load the file in a String Array and loop thru lines.

See if this helps for adding items with .Click Event and ToolTipText to a ToolStripDropDownButton.
1 ToolStripDropDownButton

Public Class Form1
    Private myIniFile As String = "C:\test.ini" '// your File.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IO.File.Exists(myIniFile) Then
            Dim arTemp() As String = IO.File.ReadAllLines(myIniFile) '// load lines as arrays.
            For i As Integer = 0 To arTemp.Length - 1 '// loop thru lines.
                '// check for lines like: [category title 1]
                If arTemp(i).StartsWith("[") Then '// once located, you know the next 4 lines are for that category.
                    '// line 2 in category.
                    Dim mItem As New ToolStripMenuItem(arTemp(i + 2).Substring(arTemp(i + 2).IndexOf("=") + 1)) '// add .Text of item.
                    '// line 3 in category.
                    mItem.Tag = arTemp(i + 3).Substring(arTemp(i + 3).IndexOf("=") + 1) '// add FullPath to .Tag.
                    '// line 4 in category.
                    mItem.ToolTipText = arTemp(i + 4).Substring(arTemp(i + 4).IndexOf("=") + 1) '// add Description as ToolTipText.
                    AddHandler mItem.Click, AddressOf myCoolDropDownItems_Click '// give the DropDownItem an Event to handle.
                    ToolStripDropDownButton1.DropDownItems.Add(mItem) '// add item to DropDownButton.
                End If
            Next
        End If
    End Sub

    Private Sub myCoolDropDownItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Process.Start(CType(sender, ToolStripMenuItem).Tag.ToString) '// use FullPath from .Tag to load app..
    End Sub
End Class

Okay, so I'm finally understanding how the Form1_Load(). I would like to categorize the items in the list. I would like to make it to where anyone can change the categories in the list without having to have access to the source code. Is this a dumb idea?? I know I would have to add another look to the above code to add the mItem to the correct section.

>> I would like to categorize the items in the list. I would like to make it to where anyone can change the categories in the list without having to have access to the source code.

What about adding a ContextMenu to the item with a "Edit Item" option that loads a Form and allows you to edit the item.Text, .Tag, .etc.?

I think I'm a little loss with the ContextMenu.

I currently have this code that is loading a category list to my ProgramsToolStripMenuItem based off of a .ini file that anyone can modify.

'Load Initial Folders
        'Looks for toolboxCats.ini
        Dim File_Name As String = Application.StartupPath & "\toolbox\Programs\toolboxcats.ini"

        If System.IO.File.Exists(File_Name) = True Then
            Dim objReader As New System.IO.StreamReader(File_Name)
            Do While objReader.Peek() <> -1
                ToolStripMenuItem1.DropDownItems.Add(objReader.ReadLine())
            Loop
        End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.