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



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

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 = "Temp"

      ' 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 = "Temp";

      // 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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET, create a new Windows application by using Visual Basic .NET or Visual C# .NET.

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

    By default, RichTextBox1 is created.
  3. 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.
  4. From the toolbox, drag a Button control onto Form1.
  5. 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 ;
    
  6. From the toolbox, drag a Button control onto Form1.
  7. 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 ;
    
  8. On the Debug menu, click Start.
  9. Copy www.msdn.microsoft.com to the RichTextBox.

    Note The URL must be in bold and italic format.
  10. Click Button1 or Button2.

    The rich text is changed to plain text.

REFERENCES

For more information, see your .NET Framework SDK documentation or visit the following MSDN Web sites:

RightToLeft Enumeration
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsRightToLeftClassTopic.asp

RichTextBox.DetectUrls Property
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsRichTextBoxClassDetectUrlsTopic.asp

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbWindowsForms kbProperties kbRichEdit kbbug KB814310 kbAudDeveloper