1

This one has me awfully confused...

I am trying to display kerned RTF text in a Visual Studio Visual Basic RichTextBox control (having so far tried under VS2010 and VS2012). Simply, I create a Windows Form project, add two RichTextBox's (RichTextBox1 and RichTextBox2) to the form, no change to default properties, and include the following VB code:

    Public Class Form1

        Private Sub Initialise(sender As System.Object, e As System.EventArgs) Handles Me.Load
            Dim txtRTF As String = "{\rtf1\ansi" & _
                               "{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" & _
                               "\f0\pard" & _
                               "\expndtw-60 a" & _
                               "\expndtw200 b" & _
                               "\expndtw-20 c}"

            RichTextBox1.Rtf = txtRTF
            RichTextBox2.Paste() ' RichTextBox2 formats properly iff clipboard holds ANY valid rtf content
            RichTextBox2.Rtf = txtRTF

        End Sub

    End Class

The txtRTF String contains, as best as I can tell, minimal valid RTF markup and text.

Here's the confusing bit: text displayed in RichTextBox1 is not kerned, despite \expndtw (expand twips) RTF markup, BUT text displayed in RichTextBox2 is properly kerned, if and only if the clipboard holds ANY valid RTF content (e.g., any text has first been copied into the clipboard from an MS Word document). Text displayed in RichTextBox2 is not properly kerned if clipboard contents is not RTF format.

Result of running if the clipboard does not contain RTF-formatted data (or if the RichTextBox2.Paste() code is removed or commented out):

Improper <code>RichTextBox</code> kerning

Result of running if the clipboard holds any random RTF-formatted text:

Proper <code>RichTextBox</code> kerning

Questions:

  1. Why on earth should it matter that I have previously pasted RTF-format (and not non-RTF format) into the RichTextBox2 control before setting the RichTextBox2.Rtf field?
  2. More importantly, how can I (in VB) programmatically display properly kerned text in a RichTextBox control without the absurdity of first pasting random RTF format text into it?

1 Answer 1

1

Well, that took a lot of work! Nevertheless, problem now solved.

It turns out that, although the RTF spec notes that \ltrch (left-to-right character run) is the default state, it seems that RichTextBox objects don't necessarily agree. Including a \ltrch (or even, oddly, a \rtlch) control sequence in the RTF markup stream completely solves the failure-to-kern problem. RTF text kerning via \expndtwN and \expndN now works perfectly well. No need for silly Paste() commands to pre-configure the RichTextBox control into its proper state!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.