2010-12-03 19 views
0

这里是我的代码不能RichTextBox的中数行数

private void Allocation_Matrix_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == 13) 
     { 
      int Total_Row_Check; 
      Total_Row_Check = getRow(); 
      Textbox1.Text = Convert.ToString(Total_Row_Check); 
      if (Total_Row_Check >= Convert.ToInt32(Total_Processes.Text)) 
      { 
       MessageBox.Show("rows cannot exceed from total number of processess"); 
      } 
     } 
    } 

    public int getRow() 
    { 
     int Row = 0; 
     string[] arLines; 
     int i; 
     arLines = Allocation_Matrix.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); 
     for (i = 0; i < arLines.Length; i++) 
     { 
      Row++; 
     } 
     return Row; 
    } 

我想更新TextBox1的我敲击回车键从键盘的RichTextBox ...但文本框只保留显示FIRSTROW并显示一( 1)

回答

0

Is Allocation_Matrix your RichTextBox?

请注意,RichTextBox.Text使用换行符(“\ n”)返回换行符,其中大多数其他所有Windows控件都使用回车符,换行符(“\ r \ n”)。

因此,如果您对由Text属性返回的字符串调用Split,它将不包含任何Evironment.NewLine。

+0

公众诠释GetRows的() { 串[] arLines; int i; arLines = Allocation_Matrix.Text.Split(new string [] {Environment.NewLine},StringSplitOptions.None);对于(i = 0; i 2010-12-03 06:00:09

+0

上面的代码正在工作.....然后你错了:( – 2010-12-03 06:02:06

0

尝试Allocation_Matrix.Text.Lines.Count()方法

+0

没有这样的方法可用:::::: – 2010-12-03 05:59:07

0

尝试这种

private void richTextBox1_TextChanged(object sender, EventArgs e) 
{ 
var lineCount = richTextBox.Lines.Count(); 
numberLabel.Text = lineCount.ToString(); 
}