Text after the first line is lost when you modify the Multiline property of the RichTextBox control from true to false (814312)



The information in this article applies to:

  • Microsoft .NET Framework 2.0
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

Beta Information

This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.

SYMPTOMS

When you type multiple lines of text in a RichTextBox control, and then modify the Multiline property from True to False, you lose the text in the RichTextBox after the first line. When you modify the Multiline property, only the first line exists in the RichTextBox. However, when you modify the Multiline property from True to False for a RichTextBox, all the text of the control is appended to the first line.

WORKAROUND

To work around this problem, save the text in the RichTextBox before you toggle the Multiline property. After you modify the Multiline property, append all the text lines into the first line, and then assign the text back to the RichTextBox. To do this, use the following code:

Visual Basic .NET or Visual Basic 2005 Code
      ' Create an Array
      Dim myStrArr() As String
      myStrArr = RichTextBox1.Text.Split(vbLf.ToString)

      ' Set Multiline Property value to False
      RichTextBox1.Multiline = False

      Dim idx As Integer
      RichTextBox1.Text = ""
      For idx = 0 To myStrArr.Length - 1
         RichTextBox1.Text += myStrArr(idx) & " "
      Next idx
Visual C# .NET or Visual C# 2005 Code
     // Create a string Array.
     string []myStrArr;
     myStrArr = richTextBox1.Text.Split('\n');

     // Set multiline property value to false.
     richTextBox1.Multiline = false;
     int idx;
     richTextBox1.Text = "";
     for(idx = 0; idx < myStrArr.Length ;idx++)
     {
	       richTextBox1.Text += myStrArr[idx]+ " ";
     }

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a Windows application by using Visual Basic 2005, Visual Basic .NET, Visual C# 2005, or Visual C# .NET.

    By default, Form1 is created.
  3. Drag a RichTextBox control to Form1.
  4. Drag a Button control to Form1.
  5. Double-click Button1, and then add the following code to the Button1_Click event handler:
    Visual Basic .NET or Visual Basic 2005 Code
    RichTextBox1.Multiline = False
    Visual C# .NET or Visual C# 2005 Code
    richTextBox1.Multiline = false;
  6. On the Build menu, click Build Solution.
  7. Press the F5 key to run the project.
  8. Type two or three lines of text in the RichTextBox, and then click Button1.

REFERENCES

For more information, visit the following MSDN Web site:

Introduction to the Windows Forms RichTextBox Control
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconrichtextboxcontroloverview.asp

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbCtrl kbWindowsForms kbRichEdit kbForms kbControl kbprb KB814312 kbAudDeveloper