1

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.

Community
  • 1
  • 1
sohan vanani
  • 1,537
  • 1
  • 20
  • 37

2 Answers2

0

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;
}
Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28
  • Hey!! it gives the last word I don't want to get last word I want edited word or added word in textbox on any place whether it Is at first , middle or last or anywhere in textbox I just want to replace added or edited word with another word. – sohan vanani Mar 18 '15 at 06:07
  • @sohanvanani never mind ... gotta go to work now, ill figure this out later – Florian Schmidinger Mar 18 '15 at 06:08
0

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.

sohan vanani
  • 1,537
  • 1
  • 20
  • 37