Search code examples
vb.netwinformsrichtextbox

Why does this error show up accessing the RTF of a RichTextBox?


I have a number of rich text boxes on my application that print data received from a digital modem type device. At times it is necessary to copy a window to another window because some of the text in the RTF window is colored and formatted a certain way I thought the best way was to copy the RTF of the window to the other window. But when I run the program and I attempt to copy the text I get this same error either in run mode r if I break the code and try to manually read the 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)}

The same error is shown it I use .RTF or .TextRTF I do not understand why the RTF can't be found when I'm using a Rich Text Box. I'm using a WinForm and made sure the correct type of RTF is being used.

Is there something special I need to turn on the RTF output?


Solution

  • It appears that what you have done is accessed your RichTextBox via an Object reference with Option Strict Off. That's bad to start with. You should have Option Strict On pretty much all the time and write your code accordingly. I added a RichTextBox to my form and accessed it directly via the generated field in the Immediate window and I got this result:

    ?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
    

    I then did this:

    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
    

    and used that x variable in the Immediate window and got this:

    ?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)}
    

    You should have Option Strict On to start with, so the code you have should not compile. Whether you have to or not, you should be casting that Object reference as the actual type of the object, then you can access members of that type directly, without late binding. Using my example:

    ?DirectCast(x, RichTextBox).TextRTF
    Nothing