Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I load a rich text file embedded resource into a RichTextBox?

You use the LoadFile method of the RichTextBox, passing it a StreamReader based on the resource. So include your RTF file as an embedded resource in your project, say RTFText.rtf. Then load your RichTextBox with code such as

Stream stream =
  GetType().Assembly.GetManifestResourceStream("MyNameSpace.RTFText.rtf");
if ( stream != null )
{
  StreamReader sr = new StreamReader( stream );
  richTextBox1.LoadFile( stream, RichTextBoxStreamType.RichText );
  sr.Close();
}

Here is a VB snippet. Depending on your default namespace settings, you may not need explicit reference to the namespace in the GetManifestResourceStream argument.

Dim stream As Stream = _
  Me.GetType().Assembly.GetManifestResourceStream("MyNameSpace.RTFText.rtf")
If Not (stream Is Nothing) Then
  Dim sr As New StreamReader(stream)
  richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText)
  sr.Close()
End If

Contributed from George Shepherd's Windows Forms FAQ



Page view counter