如何在运行时动态加载菜单条的子项? [英] How would I dynamically load the subitems of a menustrip at run time?

查看:27
本文介绍了如何在运行时动态加载菜单条的子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我会再试一次这个地方的答案.我有这段代码加载 C:\Quiz

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

我不确定如何为 menustrip.item 创建 toolstripmenuitem 对象并将每个文件夹中的测验问题加载到它们的透视 menustrip.item 名称中

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)

这不会填充每个菜单子菜单,整个想法是在运行时使用 C:\Quiz 中的文件夹名称和每个文件夹中的文本文件填充菜单和子菜单:C:\Quiz\Genesis\Genesis1.txt, 2.txt....C:\Quiz\Exodus\Exodus1.txt, 2.txt .... 等等.我很感激这方面的帮助,因为我仍在学习并且已经为此工作了很长时间.谢谢

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

感谢 IronRazer 和 CharlieMay!

Thanks to IronRazer and CharlieMay!

这篇关于如何在运行时动态加载菜单条的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆