1
votes

I am developing one Windows phone app. In my app I want to get the latest entered word in textbox not the last word. And I want to change that latest entered word on space key pressed. I am getting the last word on a key up event like this:

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();
               }
         }
}

I am getting the last word by this method but actually I want to get latest entered word by user. Meaning, if the user moves the cursor directly to the middle of textbox and types any word then I want to get that word on space key pressed event; I want the position of that word and can change that word programmatically and update textbox. For example, if the user types

H!! my name vanani

but then the user moves the cursor directly after 'name' and types 'is sohan'

H!! my name is sohan

then I want to get word and position of 'is' and same for 'sohan' in key up event of textbox. I need the position to replace that word with another word and update textbox with the new replaced text.

I have seen these questions. winforms - get last word.. and C# how to get latest char.. but they didn't help me. Please help me.

2
Try position = Regex.Match(text, @"\S*(?=\s$)").IndexFlorian Schmidinger
after checking Regex.IsMatch(text, same pattern)Florian Schmidinger
i cant understand. did you mean index by SelectionStart of textbox???sohan vanani
You could get the word itself by Regex.Match(..).value tooFlorian Schmidinger
yeah the starting index...Florian Schmidinger

2 Answers

0
votes

Like this:

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;
}
0
votes

I founded answer to my question. Here is code worked for me.

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.
             }
          }
       }
   }

I don't think it work in all situation but through this you can get idea about how to get latest entered word from Textbox or RichTextbox.