Question

I am using a StringReader to read input from a multi-line textbox. However, I am experiencing strange behaviour.
My Code:

string x = reader.ReadLine();
int y = int.Parse(x);

x is always an int.
My problem is that since x is the first line of the multiline textbox, it doesn't contain just the int, but System.Windows.Forms.Textbox, Text:10
Any help here?

I create the StringReader as following:

using (StringReader reader = new StringReader(Convert.ToString(multilinetbox)))
{

}
Was it helpful?

Solution

Change your reader to read the Text property of the multiline textbox, instead of the entire control:

using (StringReader reader = new StringReader(multilinetbox.Text))

OTHER TIPS

I know you are not asking this but still I thought to post it.

Why not try

foreach (string line in multilinetbox.Lines)
{
    int y = int.Parse(line);
}

Hope it helps.

This should work:

    private void button1_Click(object sender, EventArgs e)
    {
        StringReader rdr = new StringReader(textBox1.Text);
        int y = int.Parse(rdr.ReadLine());
    }

I think you made a mistake at your StringReader declaration.

Because Convert.ToString(multilinebox) first converts the type information, and then get the text content.

You should change the multilinebox to multilinebox.Text.

Like

using (StringReader reader = new StringReader(Convert.ToString(multilinetbox.Text)))
{    
}

You have normal results for your code, because Convert.ToString(multilinetbox) converts it to text representation.

Try to user 'Lines' property instead:

foreach (string ln in textBox1.Lines)
{
    // some work here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top