Accessing a RichTextBox on the ActiveMdiChild

jlmacdonald

Member
Joined
Apr 16, 2007
Messages
7
Programming Experience
1-3
I've been playing around with an MDI app and am looking at building something similar to Wordpad. I have a child form with only one richtextbox on it. In my parent form, I want to be able to open up an RTF file and have the contents show up in the Active Child's richtextbox. I managed to get it to do this but don't think it is very efficient and would like to know if there is a better way. This is the snippet I was using:

VB.NET:
        Dim filename As String = "c:\test.rtf"
        For Each c As RichTextBox In Me.ActiveMdiChild.Controls
            If TypeOf c Is RichTextBox Then
                c.LoadFile(filename)
            End If
        Next

This will work only is there are no other controls on the child form. Is there an easier way to do this? I would love to do something like Me.ActiveMdiChild.RichTextBox1.load(filename) but it doesn't recognize the richtextbox. I was thinking maybe of using something like Me.ActiveMdiChild.Controls.Item("RichTextBox1").GetType.GetMember("loadfile") = filename but that doesn't work (I've never really used GetType so I'm not entirely sure how it's supposed to be used)

I have been able to use
VB.NET:
Using sr As StreamReader = File.OpenText("c:\test.rtf")
	Me.ActiveMdiChild.Controls.Item("RichTextBox1").Text = sr.ReadToEnd()
End Using
but this will read the rtf as a text file with all the rtf formatting code and dump that into the richtextbox.

Thanks for any help!
 
Last edited by a moderator:
You can cast. DirectCast keyword will cast a boxed object to the type you specify (if compatible).
DirectCast(acontrolinstance, RichTextBox).LoadFile...
DirectCast(aforminstance, ChildForm).theRichTextBox.LoadFile...
 
Back
Top