Microsoft KB Archive/814312

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

Article ID: 814312

Article Last Modified on 12/3/2007

-

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 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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 = &quot;&quot; For idx = 0 To myStrArr.Length - 1 RichTextBox1.Text += myStrArr(idx) & &quot; &quot; 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 = &quot;&quot;; for(idx = 0; idx < myStrArr.Length ;idx++) {          richTextBox1.Text += myStrArr[idx]+ &quot; &quot;; }



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a Windows application by using Visual Basic 2005, Visual Basic .NET, Visual C# 2005, or Visual C# .NET.

By default, Form1 is created. Drag a RichTextBox control to Form1. Drag a Button control to Form1.  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; </li> On the Build menu, click Build Solution.</li> Press the F5 key to run the project.</li> Type two or three lines of text in the RichTextBox, and then click Button1.</li></ol>

<div class="references_section">