0

我正在尝试将一个菜单项转换为富文本框,它抛出了一个空引用异常。

代码是

' ensure above it true and find parent
Dim mnuItem As MenuItem = TryCast(sender, MenuItem)
If mnuItem IsNot Nothing Then
  Dim menu As Menu = TryCast(mnuItem.Parent, Menu)
  If menu IsNot Nothing Then
    Dim rtb As RichTextBox = TryCast(menu.Container, RichTextBox)
    If rtb IsNot Nothing Then
      rtb.Copy()
    End If
  End If
End If

代码在 rtb.Copy() 行抛出异常。谁能指导我如何解决这个问题?

编辑:当我单步执行代码时,它显示 rbt 为“无”。

4

2 回答 2

1

menu.containerinDim rtb As RichTextBox = TryCast(menu.Container, RichTextBox)可能不是 RichTextBox。使用调试器,停在该行,突出显示“menu.container”,然后按 Shift-F9 以查看它的真正含义。

于 2013-01-10T21:51:01.710 回答
0

它说“IsNothing”,你的意思是测试不是什么都没有。

If rtb Is Nothing Then
    rtb.Copy()
End If

应该:

If rtb IsNot Nothing Then
    rtb.Copy()
End If
于 2013-01-10T21:29:52.287 回答