2011-10-25 15 views
0

我嘗試下面的代碼:如何獲取用戶正在寫入的RichTextBox?

public RichTextBox GetCurrentlyInUse() 
     { 
      if (p.Focused) 
      { 
       return p; 
      } 
      else if (r.Focused) 
      { 
       return r; 
      } 
      else 
      { 
       return null; 
      } 
     } 

但以往任何時候都返回null,我該怎麼做呢? 在此先感謝。

+0

是你試圖讓富文本框中的文本..? – Hadi

+0

不,我需要獲取當前正在使用的豐富的盒子來處理.SelectionXX方法。 –

+1

你是否執行這個按下按鈕? – gbianchi

回答

0

添加一個新的領域你Form類:

public partial class MyForm : Form { 

    private RichTextBox currentRichTextBox = null; 

    //... 
} 

這兩個事件添加到您的每個richTextBoxes的:

private void richTextBox1_Enter(object sender, EventArgs e) { 
    currentRichTextBox = (RichTextBox) sender; 
} 

private void richTextBox1_Leave(object sender, EventArgs e) { 
    currentRichTextBox = null; 
} 

// Add those events to richTextBox2, richTextBox3... 

這樣,你將永遠有在currentRichTextBox對象的值當前使用的richTextBoxnull如果沒有richTextBox處於活動狀態。

相關問題