VB.Net Menuitem动态添加不触发点击事件

时间:2016-12-14 09:36:29

标签: vb.net winforms onclick menuitem

我完全迷失在这里。我动态创建了一个menuitem。我已经添加了一个onclick-event处理程序,但这个代码似乎永远不会触发。我记得它在几个月前工作,并且不记得做出任何改变,但它可能是我做过的愚蠢。

请参阅下面的代码:

frmMain._mnuSep1_0.Visible = True
Dim tlRecentApp As New ToolStripMenuItem(strMenuCaption)
tlRecentApp.Text = "Test"
tlRecentApp.Name = "AddApp"
tlRecentApp.Tag = strMenuID
RecentAppID = strMenuID
AddHandler tlRecentApp.Click, AddressOf Test
frmMain.mnuApplicantS.DropDownItems.Add(tlRecentApp.ToString)

活动代码:

Public Sub MnuRecentApp(ByVal sender As Object, ByVal e As EventArgs)
   ' MsgBox(sender.tag.ToString)
    ApplicantID = sender.tag.ToString
    frmApplicantEdit.Show()
End Sub

它被创建但是当我点击它时没有任何反应:

3 个答案:

答案 0 :(得分:0)

如果事件处理程序的代码是

Public Sub MnuRecentApp(ByVal sender As Object, ByVal e As EventArgs)
   ' MsgBox(sender.tag.ToString)
    ApplicantID = sender.tag.ToString
    frmApplicantEdit.Show()
End Sub

然后这一行

AddHandler tlRecentApp.Click, AddressOf Test

应该是

AddHandler tlRecentApp.Click, AddressOf MnuRecentApp

答案 1 :(得分:0)

在不尝试更改过多代码的情况下,我已成功测试了以下内容:

<强>控制

Dim tlRecentApp As New ToolStripMenuItem(strMenuCaption)
'tlRecentApp.Text = "Test"  This isn't needed as it's done on the above line when declared
tlRecentApp.Name = "AddApp"
tlRecentApp.Tag = strMenuID
RecentAppID = strMenuID
AddHandler tlRecentApp.Click, AddressOf MnuRecentApp
frmMain.mnuApplicantS.Items.Add(tlRecentApp)

方式

Public Sub MnuRecentApp(ByVal sender As Object, ByVal e As EventArgs)
    ApplicantID = CType(sender, ToolStripMenuItem).Tag.ToString
    frmApplicantEdit.Show()
End Sub
在我的示例中,

mnuApplicantSToolStrip控件。如果您可以澄清应用程序中mnuApplicatS的内容,我可以提供更好的解决方案。

答案 2 :(得分:0)

我发现了问题。这一行:

frmMain.mnuApplicantS.DropDownItems.Add(tlRecentApp.ToString)

应阅读:

frmMain.mnuApplicantS.DropDownItems.Add(tlRecentApp)