1

我正在尝试使用以下链接中的代码:

VB- Helper在运行时使用 Visual Basic .NET 中的图像、快捷键和事件处理程序创建菜单项

唯一的区别是我想要一个本地图像,而不是来自 my.Recources

我所拥有的是以下内容:

    ''Tool 2 displays a string and image.
    Dim tool2 As New ToolStripMenuItem("Tool 2", (Image.FromFile("C:\test\icon.jpg")))
    tool2.Name = "mnuToolsTool2"
    tool2.ShortcutKeys = (Keys.D2 Or Keys.Control) ' Ctrl+2
    AddHandler tool2.Click, AddressOf mnuTool2_Click
    ToolStripMenuItem1.DropDownItems.Add(tool2)
4

1 回答 1

0

我无法重现这个“错误”。但是,根据给定的文本、代码和链接,我的最佳猜测如下:

  1. 您使用的是 64 位机器。
  2. 您在Form.Load事件中运行代码。
  3. 此方法中某处发生错误。

Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Code...
    Throw New Exception("ooops..")
    'Code...
End Sub

您可能知道,Form.Load在 64 位机器上抛出的错误会被系统“吞下”。

有关更多信息,请阅读此 SO 帖子:为什么表单加载无法捕获异常?

您应该在构造函数中移动代码:

Public Sub New()
    Me.InitializeComponent()
    'Code goes here...
End Sub

或更改为Form.Shown事件:

Private Sub _Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
    Try
        'Code goes here...
    Catch ex As Exception
        MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
于 2014-02-19T16:53:17.163 回答