更具textbox中的参数 控制richtextbox中文本复制的的行数。例如text中填写3 从richtextbox复制出3行文本。
更具textbox中的参数 控制richtextbox中文本复制的的行数。例如text中填写3 从richtextbox复制出3行文本。
[解决办法]
B/S
- C# code
public Form1() { InitializeComponent(); //初始化 this.richTextBox1.Text = "起始行\r\n第二行\r\n第三行\r\n第四行\r\n第五行\r\n第六行\r\n第七行"; } private void button1_Click(object sender, EventArgs e) { //********************** //在前面 //Form中添加richTextBox1,richTextBox2,textBox1三控件 //剪,可以用select(int 取字符始位置,int 取字符的度) //********************** //清空 this.richTextBox2.ResetText(); if (this.textBox1.Text.Trim() == "") { MessageBox.Show("入一整"); return; } //可以判方法 try { Convert.ToInt32(this.textBox1.Text.Trim()); } catch (Exception) { MessageBox.Show("入一整"); return; } int n = Convert.ToInt32(this.textBox1.Text.Trim()); //置取的始行,0第一行 int startRow = 0; //richTextBox1文本行 int totalRow = this.richTextBox1.Lines.Length; int[] strLineLen = new int[totalRow]; if (totalRow > 0) { for (int i = 0; i < totalRow; i++) { strLineLen[i] = this.richTextBox1.Lines[i].Length; } } if (startRow > totalRow) { MessageBox.Show("richTextBox中取不到此行"); return; } if (startRow + n < totalRow) { //取字符度 int strTotalLen = 0; for (int i = startRow; i < startRow + n; i++) { string s = ""; s = this.richTextBox1.Lines[i].ToString(); strTotalLen = strTotalLen + s.Length + 1; } //取始字符位置 int startStrIndex = 0; for (int i = 0; i < startRow; i++) { startStrIndex = startStrIndex + strLineLen[i] + 2;//2="\r\n".string.length; } if (startStrIndex > 1) { //中richTextBox1指定行 richTextBox1.Select(startStrIndex - 1, strTotalLen);//因是0始算,要一 //剪至richTextBox2中 richTextBox1.Cut(); if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true) { richTextBox2.Paste(); } } else { //起始行0的情 //中richTextBox1指定行 richTextBox1.Select(startStrIndex, strTotalLen);//起始行0 //剪至richTextBox2中 richTextBox1.Cut(); if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true) { richTextBox2.Paste(); } } } else { //取字符度 int strTotalLen = 0; for (int i = startRow; i < totalRow; i++) { string s = ""; s = this.richTextBox1.Lines[i].ToString(); if (i == totalRow - 1) { strTotalLen = strTotalLen + s.Length;//最後一行不需要加了 } else { strTotalLen = strTotalLen + s.Length + 1; } } //取始字符位置 int startStrIndex = 0; for (int i = 0; i < startRow; i++) { startStrIndex = startStrIndex + strLineLen[i] + 2;//2="\r\n".string.length; } if (startStrIndex > 1) { //中richTextBox1指定行 richTextBox1.Select(startStrIndex - 1, strTotalLen);//因是0始算,要一 //剪至richTextBox2中 richTextBox1.Cut(); if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true) { richTextBox2.Paste(); } } else { //起始行0的情 //中richTextBox1指定行 richTextBox1.Select(startStrIndex, strTotalLen);//起始行0 //剪至richTextBox2中 richTextBox1.Cut(); if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true) { richTextBox2.Paste(); } } } }