我有一个ToolStripMenuItem,当我把鼠标悬停时,它会显示一个txt文件列表。当我点击其中一个项目时,我需要它用记事本(或默认文本编辑器)打开该文件。
Private Sub CFGMenu_DropDownOpening() Handles CFGMenu.MouseHover
CFGMenu.DropDownItems.Clear()
Dim cfgfolderpath As String
cfgfolderpath = SrcdsExePathTextBox.Text & "\" & GameMod & "\cfg"
If System.IO.Directory.Exists(cfgfolderpath) Then
'Create new submenu for each cfg file
For Each CfgFile As String In My.Computer.FileSystem.GetFiles _
(cfgfolderpath, FileIO.SearchOption.SearchTopLevelOnly, "*.cfg")
CFGMenu.DropDownItems.Add(System.IO.Path.GetFileNameWithoutExtension(CfgFile))
Next
Else
Status.Text = "The 'cfg' folder is empty or doesn't exist!"
End If
End Sub
答案 0 :(得分:0)
动态添加项目时,无法使用句柄关键字。您必须使用AddHandler语句。只需对所有Click事件使用公共事件处理程序,如下所示:
Dim text = System.IO.Path.GetFileNameWithoutExtension(CfgFile)
Dim item As ToolStripItem = CfgMenu.DropDownItems.Add(text)
item.Tag = CfgFile '' We'll need this later for Notepad
AddHandler item.Click, AddressOf CfgMenuItems_Click
在Click事件处理程序中,它需要恢复从发件人中单击的项目:
Private Sub CfgMenuItems_Click(sender As Object, e As EventArgs)
Dim item = CType(sender, ToolStripItem)
Dim path = CStr(item.Tag)
System.Diagnostics.Process.Start("notepad.exe", path)
End Sub