问题描述:

I thought Id try this place for an answer one more time.

I have this code that loads the names of folders in C:\Quiz

For Each dir As String In Directory.GetDirectories("C:\Quiz")

Dim dirinfo As New DirectoryInfo(dir)

MenuStrip1.Items.Add(dirinfo.Name) ' loads the folder names

For Each fn As String In Directory.GetFiles(dir) ' get the txt files in the directory Quiz's subfolders

Next

Im not sure on how to create the toolstripmenuitem object for the menustrip.item and load the quiz questions that are in each folder into their perspective menustrip.item names

the submenus would get loaded at run time. This is what I have tried but to no avail:

'iterate through each file in the current directory (dir)

For Each fn As String In Directory.GetFiles(dir)

'add the file name without the extension to a new ToolStripMenuItem

Dim menuItem As New ToolStripMenuItem(IO.Path.GetFileNameWithoutExtension(fn))

'set the Tag of the new ToolStripMenuItem to the full path and name of the file

menuItem.Tag = fn

'add the new ToolStripMenuItem to your existing (DropDownButton) in the ToolStrip or your (MenuItem) in your MenuStrip

MenuStrip1.Add(menuItem)

This does not populate each menus submenu, the whole idea is to populate the menu and submenu at run time with the folder names in C:\Quiz and the text files which are in each of the folders:

C:\Quiz\Genesis\Genesis1.txt, 2.txt....

C:\Quiz\Exodus\Exodus1.txt, 2.txt .... and so on and so forth. I'd appreciate so help on this as Im still learning and have been working on it for a great while. Thank you

网友答案:
Private Sub LoadFile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'iterate through each directory in the C:\Quiz folder
    For Each dir As String In Directory.GetDirectories("C:\Quiz")

        'add the current folder name to a new ToolStripMenuItem
        Dim mainMenuItem As New ToolStripMenuItem(IO.Path.GetFileName(dir).Substring(2))

        'add the new ToolStripMenuItem to the ToolStrip
        MenuStrip1.Items.Add(mainMenuItem)

        'iterate through each file in the current directory (dir)
        For Each fn As String In Directory.GetFiles(dir)

            'add the current file name without the extension to a new ToolStripMenuItem
            Dim subMenuItem As New ToolStripMenuItem(IO.Path.GetFileNameWithoutExtension(fn))

            'set the Tag of the new ToolStripMenuItem to the full path and name of the file
            subMenuItem.Tag = fn

            'add the new ToolStripMenuItem to the DropDownItems of the (folder) ToolStripMenuItem
            mainMenuItem.DropDownItems.Add(subMenuItem)

            'add the QuizItem_Click handler sub to to the new ToolStripMenuItem
            AddHandler subMenuItem.Click, AddressOf QuizItem_Click
        Next
    Next

End Sub

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

    'cast the "sender" to a ToolStripMenuItem
    Dim tsmi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)

    'convert the Tag (Object) back to a string to get the full file path name from the ToolStripMenuItem that was clicked
    Dim FileToLoad As String = tsmi.Tag.ToString

    'just to show you the full file path and name from the ToolStripMenuItem Tag
    MessageBox.Show("You want to load this file" & vbNewLine & FileToLoad)
End Sub

Thanks to IronRazer and CharlieMay!

相关阅读:
Top