System.InvalidCastException未处理错误

时间:2013-11-07 10:22:14

标签: vb.net visual-studio-2010

我收到了标题中所述的错误,并且是vb.net的新用户,试图理解它。如果有人可以解释解决此错误的正确方法,我将不胜感激,因为我没有足够的经验知道从哪种类型的错误开始。

如果有帮助,这就是我想要做的。我在列表框中有一个上下文菜单,当我右键单击一个条目时,显示该单元格中成员的值。消息框显示正确的值,但是当我单击以关闭该框时,会出现此错误。非常感谢

Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.ToolStripItemClickedEventArgs'.

这是我认为我应该展示的代码。

Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs) Handles pnlContextMenuStrip1.ItemClicked, HideToolStripMenuItem.Click
        'Get the text of the item that was clicked on.
        Try
            MessageBox.Show(txtCustomerActive.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Private Sub ContextMenuStrip1_Opening_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles pnlContextMenuStrip1.Opening

    End Sub

更新:我在我的项目中启用了Stirct On,在对代码进行一些细微更改后,我认为这是错误。

Error   1   Method 'Private Sub HideToolStripMenuItem_Click(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs)' cannot handle event 'Public Event Click(sender As Object, e As System.EventArgs)' because they do not have a compatible signature.  C:\Users\domain\Documents\Visual Studio 2010\Projects\Login\btLogin\vb\Form2.vb 153 175 Login

1 个答案:

答案 0 :(得分:4)

ToolStripItem.Click-event有以下参数:

(object sender, EventArgs e)

ToolStripItemClickedEventArgs继承自System.EventArgs,因此隐含于EventArgs类型。一个孩子具有父母的所有能力,但反之亦然。

我假设您只需将该签名更改为:

Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked, HideToolStripMenuItem.Click

所以只需将ToolStripItemClickedEventArgs更改为EventArgs