Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following modified code from the method shown above solves the problem. I am still curious however if Microsoft has a bug with the textBox.GetLineText method.</p> <pre><code> public static String AddNewLineCharsToMessage(TextBox textBox) { String message = String.Empty; if (textBox == null) return message; // Just assign the text if we have a single line message if (textBox.LineCount &lt; 2) return textBox.Text; // Find the index for the first line that contains text displayed in the TextBox. // GetLineText(index) will return the text displayed/entered by the user for indices less // than the index of the line that the text is actually displayed on. This seems to be // a bug to me, but I will workaround this Microsoft weirdness. // Find the index of first line that actually displays text by using the length of TextBox.Text Int32 firstTextLineIndex = 0; Int32 textLen = textBox.Text.Length; Int32 textLinesLen = 0; for (Int32 firstTextLine = textBox.LineCount - 1; firstTextLine &gt;= 0; firstTextLine--) { textLinesLen += textBox.GetLineText(firstTextLine).Length; if (textLinesLen &gt;= textLen) { firstTextLineIndex = firstTextLine; break; } } // First strip all the carriage returns and newline characters // so we don't have duplicate newline characters in the message. // Then add back in just the newline characters which is what the car // code uses to parse out the message to be displayed on each line. var textLines = new List&lt;string&gt;(5); int lineCount = Math.Min(textBox.LineCount, textBox.MaxLines); for (Int32 index = 0; index &lt; lineCount; index++) { if (index &lt; firstTextLineIndex) textLines.Add(""); else // if (textBox.GetLineText(index).Length &gt; 0) { textLines.Add(textBox.GetLineText(index)); textLines[index] = textLines[index].Replace("\r", ""); textLines[index] = textLines[index].Replace("\n", ""); } } message = String.Empty; for (Int32 index = 0; index &lt; lineCount; index++) message += textLines[index] + (index &lt; lineCount - 1 ? "\n" : ""); return message; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload