c# replace a keystroke character with other key

How do you replace a character with other while “say” you are typing in a textbox or text area.

There are couple of ways to do it.

Lets consider an example.

So say

While I am typing a browser URL I always want people to enter ‘/’ instead of ‘\’

I will create an Event for Keydown for your textbox or textarea

So lets say your textbox’ ID is txtText



In the sample above most important bit is to Suppress the Key press thus we are using e.SuppressKeyPress = true

Other way is pretty much the same but you will see a little flicker when replacement occurs so I prefer the above technique always but its worth to show you any way because the technique below will actually replace all source replaceable characters with the new one

[geshi lang=”csharp” nums=”1″ target=”_self” ]

So this is how you can manipulate Keystokes and do whatever with them. Even though I consider it as not so user friendly but there can be many other use cases where this is acceptable, say for example only allow numerics in a text field etc

I hope this helps

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.