Keypress event

  • Thread starter Thread starter Kay
  • Start date Start date
K

Kay

Hi all,

I want to create a small function to monitor the keystroke. For example, if
user key in a comma, I want to replace it with a single quote(etc)... In
vb6 I an simply check the KeyAscii in the keypress event and re-assign the
value to it, however how do I achieve this result in .net? Thanks~

Kay
 
Assuming you are intested in the Textbox control, it still supports KeyPress
event. The KeyPressEventArgs paramter exposes the KeyChar (of type Char),
which can be set at runtime.

Hi all,

I want to create a small function to monitor the keystroke. For example, if
user key in a comma, I want to replace it with a single quote(etc)... In
vb6 I an simply check the KeyAscii in the keypress event and re-assign the
value to it, however how do I achieve this result in .net? Thanks~

Kay
 
Hi Siva and everyone,

Thanks for your info~ However I still can't work out how to replace an
invalid character
i.e. replace comma with ":", can you please help?

T.I.A

Kay
 
Kay,

I think that it should be enough if I tell you not to use the KeyPress, than
the used key is not yet known.

Use the KeyUp, you get than in the event (e) all kind of types of
information about the used key.

I hope this helps,

Cor
 
In the KeyPress event of the textbox, have this:

If (e.KeyChar = ",") Then e.KeyChar = ":"

This will replace commas with colons. That is, even if the user presses
comma key, they will see colon instead.

Hope this is what you are looking for.

Hi Siva and everyone,

Thanks for your info~ However I still can't work out how to replace an
invalid character
i.e. replace comma with ":", can you please help?

T.I.A

Kay
 
Hi Siva & Cor,

Happy new year to you guys~~

Siva, when I try to assign a value to e.KeyChar, I got an error message say
that "KeyChar" is read only....?_?

Yup Cor, you're right, there're more event like KeyData, KeyValue etc,
however if I use the same syntax as Siva suggested, I got the same error
message too... how to fix it?

Thanks~
Kay :)
 
Kay,

Yes you have to replace the last keyed part by deleting it and setting the
replacer in it.

Something as by instance (not tested)
\\\
Textbox1.Text = TextBox1.Text.Replace("x","y")
TextBox1.SelectionStart = TextBox1.Text.Length
TextBox1.SelectionLength = 1
///
I hope this helps,

Cor
 
Back
Top