2013-08-19 65 views
0

如何在RichTextBox中獲取最後輸入的單詞及其索引位置(在兩個空格之間的單詞,只要按空格,我需要獲取單詞)。如果單詞在RichTextBox文檔的末尾,我使用下面的代碼來獲取最後輸入的單詞及其索引位置。使用c獲取RichTextBox的最後輸入的單詞#

private void richTextBox_KeyPress(object sender, KeyPressEventArgs e){ 
     if(e.KeyChar == ' '){ 
     int i = richTextBox.Text.TrimEnd().LastIndexOf(' '); 
     if(i != -1) MessageBox.Show(richTextBox.Text.Substring(i+1).TrimEnd()); 
       } 
     } 

但是我如何才能最後輸入的單詞,其索引位置,如果我在一個句子中的RTB(例如,「快速狐狸」中等類型是句子,如果我寫「跳躍「後」 狐狸「,然後使用上面的代碼,我可以得到最後輸入的單詞。但是,如果我定位光標後「後」 快速」我怎麼快速「寫」 棕色最後輸入的單詞(即棕色)a當我按下空間時

請幫

+0

可能的重複[如何提取richtextbox中的最後一個單詞在c#](http://stackoverflow.com/questions/12431607/how-to-extract-last-word-in-richtextbox-in-c-sharp ) – gunr2171

+0

讓我看看那個鏈接,如果它在我的問題 – jeff

回答

0

如果您在框中尋找最後輸入的字,而不僅僅是最後一個字,你將需要在對話框來創建一組單詞,然後比較有什麼變化時,新單詞被添加。

,就在我的頭頂

private string oldwords = ""; 
    private string newwords = ""; 
    private string lastword; 
    private int index; 

    private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar != ' ') return; 
     findLastword(); 
    } 

    private void findLastword() 
    { 
     newwords = richTextBox1.Text; 
     lastword = newwords.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) 
          .Except(oldwords.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) 
          .ToList().FirstOrDefault() ?? ""; 

     oldwords = newwords; 
     index = newwords.IndexOf(lastword.FirstOrDefault()); 
    } 
+0

如何得到這個詞的索引位置呢? – jeff

+0

看到我的編輯 - 但如果你知道新的單詞indexOf在文本框中找到它的位置。還是有沒有理由不適合你? – trashrobber

1

你可以嘗試這樣的事情

RichTextBox richTextBox = new RichTextBox(); 
    string lastWord; 
    private void KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == ' ') 
     { 
      lastWord = richTextBox.Text.Split(' ')[richTextBox.Text.Split(' ').Count() - 2]; 
     } 
    } 

我不能確切地測試它的權利,但它應該給你一個強烈的念頭怎麼做你想要的。

編輯:::對此深感抱歉,試試這個,而不是

string lastword; 
    private void KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (e.KeyChar == ' ') 
     { 
      lastword = ReadBack(rtb.SelectionStart); 
     } 
    } 
    private string ReadBack(int start) 
    { 
     string builder = ""; 
     for (int x = start; x >= 0; x--) 
     { 
      Char c = rtb.Text[x]; 
      if (c == ' ') 
       break; 
      builder += c; 
     } 
     Array.Reverse(builder.ToArray()); 
     return builder; 
    } 
+0

你碰到了我遇到的同樣的問題。這不是整個文本中的最後一個單詞,但他需要最後一個單詞_that是最後一個typed_。這意味着如果你開始在字符串的中間輸入,它會在那裏接收,而不是結束。我假設你必須做一些與光標位置有關的工作。 – gunr2171

+0

這正是這裏所需要的 – jeff

+0

是的,這是更好的答案 – jeff

5

好螺絲我的第一個答案,那應該會更好:

private void richTextBox_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == ' ') 
    { 
     int wordEndPosition = richTextBox1.SelectionStart; 
     int currentPosition = wordEndPosition; 

     while (currentPosition > 0 && richTextBox1.Text[currentPosition - 1] != ' ') 
     { 
      currentPosition--; 
     } 

     string word = richTextBox1.Text.Substring(currentPosition, wordEndPosition - currentPosition); 
    } 
} 

SelectionStart獲取當前插入符位置。 SelectionStart通常用於知道用戶的文本選擇('藍色突出顯示的文本')開始和結束的位置。但是,即使沒有選擇,它仍然可以獲得當前的插入位置。

然後,爲了得到輸入的單詞,它會倒退,直到它找到一個空格(或者第一個單詞的索引爲0)。

所以你得到這個詞有一點Substring,currentPosition擁有你的詞的索引。

適用於任何地方的文字,但它有一個弱點:如果不是在'quick'之後放入插入符號並進入「brown SPACE」,用戶將插入符號設置爲'quick'並且鍵入'SPACE brown'目前可能檢測到它。

+0

必須有更好的方法。有什麼辦法獲得當前光標位置並開始遍歷到左側,直到找到空格爲止? – jeff

+0

@jeff事實上,在RichTextBox稍作研究之後,終於找到了如何獲得插入符的位置,並閱讀了我的更新答案。 –

+0

想到了,我想我現在可以做到 – jeff

1

您可以從一些Key事件和flags受益:

bool newStart; 
int startIndex; 
//SelectionChanged event handler for richTextBox1 
private void richTextBox1_SelectionChanged(object sender, EventArgs e) 
{ 
    //record the new SelectionStart 
    if (newStart) startIndex = richTextBox1.SelectionStart;       
} 
//KeyDown event handler for richTextBox1 
private void richTextBox1_KeyDown(object sender, KeyEventArgs e) 
{ 
    newStart = char.IsControl((char)e.KeyCode) || 
         ((int)e.KeyCode < 41 && (int)e.KeyCode > 36); 
} 
//KeyUp event handler for richTextBox1 
private void richTextBox1_KeyUp(object sender, KeyEventArgs e) 
{ 
    newStart = true;//This makes sure if we change the Selection by mouse 
    //the new SelectionStart will be recorded. 
} 
//KeyPress event handler for richTextBox1 
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar == ' '){ 
     MessageBox.Show(richTextBox1.Text.Substring(startIndex, richTextBox1.SelectionStart - startIndex));   
     newStart = true; 
    }     
} 

這裏要注意的重要一點是事件的順序:

  1. 的KeyDown
  2. 按鍵響應
  3. 的SelectionChanged
  4. KeyUp
相關問題