C#里面textbox有多行数。怎么求这多行的数和呢?

如题所述

// 假设textbox名为textBox1,首先对textBox1进行如下属性设置
this.textBox1.Multiline = true;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.WordWrap = false;

// 之后就可以用如下语句来获取textbox行数了
int lnCount = this.textBox1.Lines.Length;
// 取得某一行文本的代码示例如下,此处取得第2行文本内容
string txt = this.textBox1.Lines[1];

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-06-18
private void button1_Click(object sender, System.EventArgs e)
        {
            string str = this.txtBoxMultiLine.Text.Trim();
            if (str.Contains("\r\n"))
            {
                str = str.Replace("\r\n", "@");
                string[] strArray = str.Split('@');
                int sum = 0;
                int i = 0;
                foreach (string item in strArray)
                {
                    if (int.TryParse(item, out i))
                    {
                        sum += i;
                    }
                }
                MessageBox.Show(sum.ToString());
            }
            else
            {
                MessageBox.Show("单行文本框值为:" + this.txtBoxMultiLine.Text);
            }
        }

本回答被提问者采纳
第2个回答  2014-02-25
没有办法做到的,你在获取值的时候就变成一个数值,而不是多个数值。
第3个回答  2014-02-25
有多少行:textBox1.Lines.Length
相似回答