请问如何把多行文本框中的某一行的内容读出来。
谢谢。

解决方案 »

  1.   

            private void button1_Click(object sender, EventArgs e)
            {
                string[] items = textBox1.Text.Split('\n');
                foreach (string s in items)
                {
                    MessageBox.Show(s);
                }
            }注意:空行也算
      

  2.   

    用\n的话必须是用回车换行,如果需要非回车换行的某一行的话用这个方法。         private void button1_Click(object sender, EventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                int lineCode = 2;//第三行
                string text = textBox1.Text;
                for (int i = 0; i < text.Length; i++)
                {
                    if (textBox1.GetLineFromCharIndex(i) > lineCode)
                    {
                        break;
                    }                if (textBox1.GetLineFromCharIndex(i) == lineCode)
                    {
                        sb.Append(text[i].ToString());
                    }
                }            MessageBox.Show(sb.ToString());
            }
      

  3.   


                int lineCode = 2;//第三行
                int startCharIndex = textBox1.GetFirstCharIndexFromLine(lineCode);
                string text = textBox1.Text;
                int endCharIndex = text.Length;
                int tempIndex = textBox1.GetFirstCharIndexFromLine(lineCode + 1);
                if (tempIndex != -1)
                {
                    endCharIndex = tempIndex;
                }            MessageBox.Show(text.Substring(startCharIndex, endCharIndex - startCharIndex));
    修改了一下,不需要循环。
      

  4.   

            private void button3_Click(object sender, EventArgs e)
            {
                foreach (String str1 in textBox1.Lines) 
                {
                    listBox1.Items.Add(str1);//str1为某行的内容
                }
            }