This is my first time posting question on this amazing service, since today it has helped me a lot by just reading it.
Currently, I'm making small C# application where I need to use a lot of TextBoxes. In TextBox Properties I have checked MultiLine and Word-Wrap functions. So when user enters text, it is displayed correctly in multiple lines.
My problem is how can I get those lines, which are displayed on form, in list of strings, instead of one big string.
I haven yet distinguished weather Word-Wrap Function makes new lines or adds "\n\r" at the end of every line. I tried to get lines from TextBox.Lines , but it has only TextBox.Lines[0], which contains the whole string form TextBox
I have already tried many things and researched many resources, but I still haven't found right solution for this problem.
You can pull out the single
string
and then do whatever you want with it.Splitting a string into a list of strings based on a separator is straightforward:
https://stackoverflow.com/a/1814568/13895
Lots of corner cases here. The core method you want use is
TextBox.GetFirstCharIndexFromLine()
, that lets you iterate the lines after TextBox has applied the WordWrap property. Boilerplate code:Beware that line-endings are included in lines.
It is possible to get the line count and build an array of lines directly. This is inclusive of lines generated due to WordWrap.