Microsoft KB Archive/814310

= BUG: The RichTextBox control loses formatting when you change the DetectUrls property or the RightToLeft property at run time =

Article ID: 814310

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



SYMPTOMS
When you change the DetectUrls property or the RightToLeft property of the RichTextBox control at run time, you lose the rich text formatting. The rich text is converted to plain text.



CAUSE
When you change the RightToLeft or DetectUrls property of the RichTextBox at run time, Microsoft .NET Framework cannot preserve the format. Therefore, you lose the rich text formatting.



WORKAROUND
To work around this bug, copy the rich text to the Clipboard, and then set the RightToLeft property. Next, copy the text from Clipboard back to rich text. To do this, use the following code:

Visual Basic .NET Code ' Select the Text in TextBox RichTextBox1.SelectAll

' Add the Text to the Clipboard RichTextBox1.Cut

' Because RightToLeft cannot be set for Empty RichTextBox ' Add Temporary text to RichTextBox RichTextBox1.Text = &quot;Temp&quot;

' Set RightToLeft to Yes RichTextBox1.RightToLeft = RightToLeft.Yes

' Paste the Text back to RichTextBox RichTextBox1.SelectAll RichTextBox1.Paste Visual C# .NET Code // Select the Text in TextBox richTextBox1.SelectAll;

// Add the Text to the Clipboard richTextBox1.Cut;

// Because RightToLeft cannot be set for Empty RichTextBox // Add Temporary text to RichTextBox richTextBox1.Text = &quot;Temp&quot;;

// Set RightToLeft to Yes richTextBox1.RightToLeft = RightToLeft.Yes;

// Paste the Text back to RichTextBox richTextBox1.SelectAll; richTextBox1.Paste;



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 In Microsoft Visual Studio .NET, create a new Windows application by using Visual Basic .NET or Visual C# .NET.

By default, Form1 is created. From the toolbox, drag a RichTextBox control onto Form1.

By default, RichTextBox1 is created. Right-click RichTextBox1, and then click Properties.

By default, the DetectUrls property is set to True and the RightToLeft property is set to No in the Properties window. From the toolbox, drag a Button control onto Form1.  Double-click Button1, and then add the following code in the Click event procedure:

Visual Basic .NET Code RichTextBox1.DetectUrls = Not RichTextBox1.DetectUrls Visual C# .NET Code richTextBox1.DetectUrls =!richTextBox1.DetectUrls ;  From the toolbox, drag a Button control onto Form1.</li>  Double-click Button2, and then add the following code in the Click event procedure:

Visual Basic .NET Code If RichTextBox1.RightToLeft = RightToLeft.Yes Then RichTextBox1.RightToLeft = RightToLeft.No       Else RichTextBox1.RightToLeft = RightToLeft.Yes End If Visual C# .NET Code if (richTextBox1.RightToLeft ==RightToLeft.Yes) richTextBox1.RightToLeft =RightToLeft.No ; else richTextBox1.RightToLeft =RightToLeft.Yes ; </li> On the Debug menu, click Start.</li> Copy www.msdn.microsoft.com to the RichTextBox.

Note The URL must be in bold and italic format.</li> Click Button1 or Button2.

The rich text is changed to plain text.</li></ol>

<div class="references_section">