Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

going line-by-line through a Multiline textbox

Status
Not open for further replies.

nickmollberg

Programmer
Aug 2, 2004
29
0
0
US
I have a big textbox (100 lines) that I need to read through, one line at a time.

Basically, I need to pass it, one line at a time to a parsing method. I should just loop through it until I reach the end, I suppose.

What would be the syntax to loop, line by line, through a texbox?
 
Use the Lines property.

Show All
Gets or sets the lines of text in a text box control.

[Visual Basic]
Public Property Lines As String ()

[C#]
public string[] Lines {get; set;}
Property Value
An array of strings that contains the text in a text box control.

Remarks
Each element in the array becomes a line of text in the text box control. If the Multiline property of the text box control is set to true and a newline character appears in the text, the text following the newline character is added to a new element in the array and displayed on a separate line.

Example


Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
if this is going to be a lot of lines, can you store each line of a list box, instead of a text box? At least the index of the list box could provide a nice number to loop with.....though if John has a better way, definitely listen to him, he's a bad man.

Good luck,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
nickmollberg,

OK, here's what you do (and in C# even!) ...

you've created your form with a multiline textbox, right? If so, you can loop through each line this way (on a button click event):

Code:
private void button1_Click(object sender, System.EventArgs e)
{
    string[] lines = textBox1.Lines;

    for(int i = 0; i < lines.GetUpperBound(0); i++)
    {
        MessageBox.Show(lines[i]);
    }
}

and, as you probably know, "lines" is equal to a string, so at that point do whatever processing you have to do.

Hope that helps.

______________________________________________
When told, "goto hell", a programmer finds the method, not the destination as harmful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top