If you need to read a multiline TextBox line by line in C#, you need to use the Lines property of the TextBox. This property gives you an array of strings in return. Each element of this array corresponds to a line in the Multiline TextBox. Here is the sample C# example code that will help you to understand in better way.
Assume that you have a multiline TextBox named as ‘txtBox1’.
Reading Lines of Multiline TextBox Line into Array
// Get all lines from the TextBox and store it in array
string[] lines = txtBox1.Lines;
Now we have got all the lines in multiline TextBox into an array of string type. Now we can use foreach loop to iterate each element of array as illustrated below.
foreach (string line in lines)
{
// Here we can process each line as per need
MessageBox.Show(line);
}
Iterating to each element of array means that we are going line by line of TextBox.
Reading Lines of Multiline TextBox Line into List
Below code gets each line of TextBox and appends as list item into a list. Now you can process further to iterate over each list item using foreach loop and get lines of multiline textbox line by line.
List<string> lines = txtBox1.Lines.ToList();
foreach (string line in lines)
{
// Here we can process each line asper need
MessageBox.Show(line);
}