2010-12-03 21 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(); 
}