8

I want to know how to get the latest char written in TextBox. It does not mean the last of the string. For instance, I write this :

This i a test.

But I forgot 's', so I move my finger to the 'i' and I add the 's' :

This is a test.

So, how can I get the 's' ?

I want to get it in char or string, but I don't know how to do...

I hope it is clear.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
TDK
  • 161
  • 2
  • 13
  • Every time a character is entered, convert the string to an array of characters, keep the old string in an array of characters and find the first index where the characters do not match. For example, with the strings you provided, the old string at index 6 would be ' ' while the new string at index 6 would be 's' so you know this was the character that was inserted. – Jonathan Feb 18 '13 at 15:05
  • _Hmm_, sounds like to me this is a artificial intelligence rather than a programming problem. – Soner Gönül Feb 18 '13 at 15:07
  • If windows phone 7 or 8 still support copy pasting (6 did), what would you like for your application to show, the last character entered with a key or the pasted data? – C.Evenhuis Feb 18 '13 at 15:15

4 Answers4

8

If you want to get the last written character, then subscribe TextBox to the KeyDown event:

C#:

textBox.KeyDown += textBox_KeyDown;

XAML:

<TextBox x:Name="textBox" KeyDown="textBox_KeyDown" />

Then:

private void textBox_KeyDown(object sender, KeyEventArgs e)
{
    /* e.Key contains the keyboard key associated with the event. */
}

If you want to get the index of the last written character, then this is more complicated. One of the solution could be tracking the mouse position and cursor in the TextBox.

Ryszard Dżegan
  • 24,366
  • 6
  • 38
  • 56
  • 1
    What's the difference between appending `new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress)` and `this.textBox1_KeyPress` ? –  Feb 18 '13 at 15:12
  • In c# there is no difference. I wrote the code of designer, which is using fully qualified names. – Ryszard Dżegan Feb 18 '13 at 15:14
  • Thanks ! And, indeed, I need the index too. How can we track the cursor position in WP ? – TDK Feb 18 '13 at 15:52
  • 1
    I found ! Just : Textbox.SelectionStart, and I get the cursor position ! – TDK Feb 18 '13 at 16:01
  • @TDK: You could try to observe keyboard left and right arrows in order to determine the caret position by using `KeyDown` event. Unfortunately I have no idea how to achieve the exact character position by using mouse in `TextBox` since particular characters don't rise any events. You are only cable of calculating approximate character position. If it is short text, you can make a copy of current content of the `TextBox` and after `TextChanged` event you will be able to find the difference between stored copy and current text. – Ryszard Dżegan Feb 18 '13 at 16:17
  • @TDK: I am pleased with your success. I didn't know, that you are looking for selection :) – Ryszard Dżegan Feb 18 '13 at 16:19
  • @TDK if you have any idea about this question. please help me it's like as your question. http://stackoverflow.com/questions/29114870/get-the-latest-entered-word-in-textbox-windows-phone-8 – sohan vanani Mar 18 '15 at 19:33
4

The yBee's answer is almost correct, with the exception that event is called KeyDown, not KeyPress, at least on Windows Phone 8.

Haspemulator
  • 11,050
  • 9
  • 49
  • 76
  • 1
    They are both valid events. `KeyPress` works better for this, since it's not raised for noncharacter keys. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx –  Feb 18 '13 at 15:16
  • 2
    The question was asked for Windows phone and not Windows forms. Keypress is not present in case of Windows Phone. The available event is KeyDown. – Manish Feb 18 '13 at 15:20
0

using this code to get last character inserted in textbox

 private void Textbox1_KeyDown(object sender, KeyEventArgs e)
            {
                lastchar_inserted = e.KeyValue.ToString(); // global string variable
            }
Moory Pc
  • 860
  • 15
  • 16
0

Have a look, it might help. To get the last character of a Text another suggestion is to append a

"\n" (nameTextbox.Append("\n"))

and then, use

int lineCount = nameTextbox.Lines.Count();

and finally, use the method

nameTextbox.GetFirstCharIndexFromLine(lineCount).

There you may have (index-1) (or Whatever, be careful and test it with a smaller text) as your last character's index.

arkoak
  • 2,437
  • 21
  • 35
Nayebifar
  • 1
  • 1