Tuesday 24 September 2013

WPF - How to Read each line from a TextBox

To read each line from a TextBox control in WPF, we can use the below code snippet.
StringCollection type represents a collection of strings. The type is part of the namespace - System.Collections.Specialized. import the namespace in your code file:
using System.Collections.Specialized;
private StringCollection GetTextLines()
{
var lines = new StringCollection();
int lineCount = txtPlainTxt.LineCount;
for (int line = 0; line < lineCount; line++)
lines.Add(txtPlainTxt.GetLineText(line));
return lines;
}

The usage is straightforward as below:
Please note that each item of the StringCollection is a string holding each line of the TextBox.
// Usage
var lines = GetTextLines();
foreach (var line in lines)
{
// line is string with each line.
}

No comments:

Post a Comment