实现结果如下:
代码如下:
private void button1_Click(object sender, EventArgs e)
{
Int32 lines = richTextBox1.Lines.Length;
richTextBox2.Text = "行数:" + lines.ToString();
List<string> values = new List<string>();
for (int i = 0; i < lines; i++)
{
string value = string.Empty;
if (i == lines - 1)
{
int startIndex = this.richTextBox1.GetFirstCharIndexFromLine(i);
value = this.richTextBox1.Text.Substring(startIndex, this.richTextBox1.Text.Length - startIndex);
}
else
{
int startIndex = this.richTextBox1.GetFirstCharIndexFromLine(i);
int endIndex = this.richTextBox1.GetFirstCharIndexFromLine(i + 1) - 1;
value = this.richTextBox1.Text.Substring(startIndex, endIndex - startIndex + 1);
}
values.Add(value);
}
richTextBox2.AppendText("\r\n 内容如下 :\r\n");
foreach (string str in values)
{
richTextBox2.AppendText(str);
}
Int32 he = 0;
foreach( string str in values)
{
Int32 zhi;
if( int.TryParse(str, out zhi))
{
he += zhi;
}
}
richTextBox2.AppendText("\r\n 加和结果如下 :\r\n");
richTextBox2.AppendText(he.ToString());
}
参考链接
(45条消息) C# 获取richtextbox每一行数据_小仙女&&的博客-CSDN博客_richtextbox按行读取
特此记录
anlog 2022年7月17日