获取文本框的Windows 8手机的最新录入的单词(Get the latest entered w

2019-10-22 06:43发布

我正在开发一个Windows phone的应用程序。 在我的应用我想在文本框不是最后一个字最近输入的字。 我想改变按下空格键是最近输入的字。 我正在在这样一个关键的事件了最后一个字:

private async void mytxt_KeyUp_1(object sender, KeyRoutedEventArgs e)
{
   if (e.Key == Windows.System.VirtualKey.Space || e.Key == Windows.System.VirtualKey.Enter)
        { 
           if (string.IsNullOrWhiteSpace(textBox_string) == false)
              {
                  string[] last_words = Regex.Split(textBox_string, @"\s+");
                  int i = last_words.Count();
                  last_words = last_words.Where(x => x != last_words[i-1]).ToArray();               last_word = last_words[last_words.Count() - 1];
                  last_word = last_word.TrimStart();
               }
         }
}

我用这种方法获得的最后一个字,但实际上我想通过用户获得最新的录入的单词。 也就是说,如果用户将光标直接移动到文本框和类型,然后我想在空格键按下事件字的任何字的中间; 我想这个词的位置,并可以通过编程方式改变字和更新文本框。 例如,如果用户键入

H!! 我的名字vanani

但随后的用户后直接“名称”和类型移动光标“是sohan”

H!! 我的名字是sohan

然后我想在文本框的按键释放事件让字和“是”位置,同为“sohan”。 我需要的位置用另一个单词和文本框更新用新的替代文本,以取代这个词。

我已经看到了这些问题。 的WinForms -获得最后一个字..和C#如何获取最新焦炭..但他们并没有帮助我。 请帮我。

Answer 1:

像这样:

if (Regex.IsMatch(textBox_string, @"\S*(?=\s?$)"))
{
    Match match = Regex.Match(textBox_string, @"\S*(?=\s?$)");
    string word = match.Value;
    int startingIndex = match.Index;
    int length = word.Length;
}


Answer 2:

我创办回答我的问题。 下面是代码为我工作。

Bool isFirst = false;
int mytempindex;
 private async void mytxt_KeyUp_1(object sender, KeyRoutedEventArgs e)
    {  
      if (e.Key == Windows.System.VirtualKey.Space)
        {
          int i = mytxt.SelectionStart;
          if (i < mytxt.Text.Length)
          {
            if (isfirst == false)
             { 
                mytempindex = mytxt.SelectionStart;
                isfirst = true;
             }
            else
             {
                int mycurrent_index = mytxt.SelectionStart;
                        int templength_index = mycurrent_index - mytempindex;
                string word = mytxt.Text.Substring(mytempindex, templength_index); //It is the latest entered word.
               //work with your last word.
             }
          }
       }
   }

我不认为它在工作的所有情况,但是通过这个你可以得到有关如何从文本框或获得的RichTextbox最近输入字的想法。



文章来源: Get the latest entered word in textbox Windows phone 8