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)))
{
}
更改您的阅读器以读取多行文本框的Text
属性,而不是整个控件:
using (StringReader reader = new StringReader(multilinetbox.Text))
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.
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
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.