2013-08-19 131 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
相关问题