winforms 为什么访问RichTextBox的RTF时会出现此错误?

ars1skjm  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(161)

我在我的应用程序上有许多富文本框,可以打印从数字调制解调器类型设备接收的数据。有时需要将一个窗口复制到另一个窗口,因为RTF窗口中的一些文本以某种方式着色和格式化,我认为最好的方法是将窗口的RTF复制到另一个窗口。但是当我运行程序并尝试复制文本时,我得到了相同的结果。错误无论是在运行模式r,如果我打破了代码,并试图手动读取RTF。

?Hostform.rtfRX(0).TextRTF

'Hostform.rtfRX(0).TextRTF' threw an exception of type 'System.MissingMemberException'
    ClassName: Nothing
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233070
    HelpLink: Nothing
    InnerException: Nothing
    MemberName: Nothing
    Message: "Public member 'TextRTF' on type 'RichTextBox' not found."
    Signature: Nothing
    Source: "Microsoft.VisualBasic"
    StackTrace: "   at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& MemberName, Boolean ReportErrors)" & vbCrLf & "   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)" & vbCrLf & "   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)"
    TargetSite: {System.Reflection.MemberInfo[] GetMembers(System.String ByRef, Boolean)}

字符串
同样的错误显示它我使用.RTF或.TextRTF我不明白为什么RTF无法找到当我使用一个富文本框.我使用WinForm并确保正确的RTF类型被使用.
是否有什么特殊的东西我需要打开RTF输出?

zf9nrax1

zf9nrax11#

看起来你所做的是通过Option Strict OffObject引用访问RichTextBox。这是一个糟糕的开始。你应该经常使用Option Strict On并相应地编写代码。我在表单中添加了一个RichTextBox,并通过即时窗口中生成的字段直接访问它,我得到了这样的结果:

?RichTextBox1.Rtf
"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang2057{\fonttbl{\f0\fnil Segoe UI;}}" & vbCrLf & "{\*\generator Riched20 10.0.22621}\viewkind4\uc1 " & vbCrLf & "\pard\f0\fs18 Hello\par" & vbCrLf & "World\par" & vbCrLf & "}" & vbCrLf
?RichTextBox1.TextRTF
Nothing

字符串
然后我这样做:

Option Strict Off

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim x As Object = RichTextBox1
    End Sub

End Class


然后在即时窗口中使用x变量,得到如下结果:

?x.Rtf
"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang2057{\fonttbl{\f0\fnil Segoe UI;}}" & vbCrLf & "{\*\generator Riched20 10.0.22621}\viewkind4\uc1 " & vbCrLf & "\pard\f0\fs18 Hello\par" & vbCrLf & "World\par" & vbCrLf & "}" & vbCrLf
?x.RTF
"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang2057{\fonttbl{\f0\fnil Segoe UI;}}" & vbCrLf & "{\*\generator Riched20 10.0.22621}\viewkind4\uc1 " & vbCrLf & "\pard\f0\fs18 Hello\par" & vbCrLf & "World\par" & vbCrLf & "}" & vbCrLf
?x.TextRTF
'x.TextRTF' threw an exception of type 'System.MissingMemberException'
    ClassName: Nothing
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233070
    HelpLink: Nothing
    InnerException: Nothing
    MemberName: Nothing
    Message: "Public member 'TextRTF' on type 'RichTextBox' not found."
    Signature: Nothing
    Source: "Microsoft.VisualBasic.Core"
    StackTrace: "   at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& memberName, Boolean reportErrors)" & vbCrLf & "   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateGet(Object instance, Type type, String memberName, Object[] arguments, String[] argumentNames, Type[] typeArguments, Boolean[] copyBack)" & vbCrLf & "   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)"
    TargetSite: {System.Reflection.MemberInfo[] GetMembers(System.String ByRef, Boolean)}


你应该从Option Strict On开始,所以你的代码不应该编译。不管你是否必须,你应该把Object引用转换为对象的实际类型,然后你可以直接访问该类型的成员,而不需要后期绑定。使用我的例子:

?DirectCast(x, RichTextBox).TextRTF
Nothing

相关问题