P.Brian.Mackey
P.Brian.Mackey

Reputation: 44295

Why does Font have to be set after RTF?

In the code below if I set Font before Rtf the font does not change. Font must be set after Rtf. Is this some quirk of RichTextBox?

        [TestMethod]
        public void FontProblemTest()
        {
            Form f = new Form();
            RichTextBox brtb = new RichTextBox();
            brtb.Width = 800;
            brtb.Height = 500;
            brtb.Font = new System.Drawing.Font(new System.Drawing.FontFamily("Courier New"), 8.25F, System.Drawing.FontStyle.Regular);//font set here has no effect
            brtb.Rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil Arial;}{\f1\fnil\fcharset0 Courier New;}}" +
@"{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\trowd\trgaph10\trleft-10\trpaddl10\trpaddr10\trpaddfl3\trpaddfr3" +
@"\cellx1140\cellx3740\cellx7340\pard\intbl\lang1033\f0\fs20\cell Whole Chart, Low to High\cell Most Recent (7/14/2002 10:17 AM)\cell\row" +
@"}";
            //brtb.Font = new System.Drawing.Font(new System.Drawing.FontFamily("Courier New"), 8.25F, System.Drawing.FontStyle.Regular);//font must be set after rtf.
            f.Controls.Add(brtb);
            f.Width = 1000;
            f.Height = 800;
            f.ShowDialog();
        }

Upvotes: 0

Views: 721

Answers (2)

LarsTech
LarsTech

Reputation: 81675

Don't include the font in your RTF string. Your RichTextBox is specifying Courier New but your RTF code overrides that with the Arial.

brtb.Rtf = @"{\rtf1\ansi" +
@"{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\trowd\trgaph10\trleft-10\trpaddl10\trpaddr10\trpaddfl3\trpaddfr3" +
@"\cellx1140\cellx3740\cellx7340\pard\intbl\lang1033\f0\cell Whole Chart, Low to High\cell Most Recent (7/14/2002 10:17 AM)\cell\row" +
@"}";

Also note that I removed \fs20 from the rtf text as well, which was using a larger font.

Once you set the RTF property of the RichTextBox control, it will re-write itself to this:

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}} \viewkind4\uc1\trowd\trgaph10\trleft-10\cellx1140\cellx3740\cellx7340\pard\intbl\lang1033\f0\fs17\cell Whole Chart, Low to High\cell Most Recent (7/14/2002 10:17 AM)\cell\row \pard\fi-10\par }

Upvotes: 1

Picrofo Software
Picrofo Software

Reputation: 5571

I think that the Rtf is what is responsible for Font/Colors/Spacing etc... The font is already changed when you insert

brtb.Font = new System.Drawing.Font(new System.Drawing.FontFamily("Courier New"), 8.25F, System.Drawing.FontStyle.Regular);//font set here has no effect

but because you insert

brtb.Rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0\fnil Arial;}{\f1\fnil\fcharset0 Courier New;}}" +
@"{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\trowd\trgaph10\trleft-10\trpaddl10\trpaddr10\trpaddfl3\trpaddfr3" +
@"\cellx1140\cellx3740\cellx7340\pard\intbl\lang1033\f0\fs20\cell Whole Chart, Low to High\cell Most Recent (7/14/2002 10:17 AM)\cell\row" +
@"}";

it will simply overwrite the font you set before.

REMARK Saving the brtb.Rtf value to a new .rtf document will allow you to view the rows you created in brtb.Rtf

Thanks,

I hope this helps :)

Upvotes: 1

Related Questions