Question

I am trying to get the current index of the letter that typed in during OnKeyDown event.

For example, if I have a text in RichEdit Control as "MOVS,21"

I would like know the index of "(" when I change the string to "MOV(S,21" in the event "OnKeyDown".

Is there any way I can get this index? Appreciate your help.

regards, Balan Sinniah

Was it helpful?

Solution

The general behavior of a richedit when it receives a KeyDown event, is to insert the corresponding char in the current carret position.

You should be able to get this position using the selectionStart and selectionEnd properties of the richedit.

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        MessageBox.Show(richTextBox1.SelectionStart.ToString());
    }

There are some edge cases that you will need to handle, for example, when the pressed key is a delete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top