468,537 Members | 1,844 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

How to best intercept keypresses in a TextBox?

I'm trying to setup a mechanism whereby I store a formatting code in the Tag
of a textbox. This code might be "UpperCase" or "LowerCase" or "NumericOnly".

I've been trying out various code in KeyPress and KeyDown but haven't been
able to figure out exactly what to do to intercept the keypress and modify
the code accordingly.

So for example, if "UpperCase" is in effect and I receive an "a" then I will
turn it into an "A" on the fly.

Are there any good articles on how to do this?

--
Robert W.
Vancouver, BC
www.mwtech.com

Dec 3 '05 #1
5 4024
This works:

enum KeyTypes
{
UPPERCASE,
LOWERCASE,
NUMERIC
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// Set the tag like this following line:
//this.textBox1.Tag = KeyTypes.UPPERCASE;

// decide what the tag is
switch ((KeyTypes)this.textBox1.Tag)
{
case KeyTypes.NUMERIC:
if (!char.IsDigit(e.KeyChar))
{
// this is not a digit... Handled gets set to true
e.Handled = true;
}
break;
case KeyTypes.UPPERCASE:
if (char.IsLetter(e.KeyChar))
{
e.Handled = true;
// Convert to uppercase
this.textBox1.AppendText(char.ToUpper(e.KeyChar).T oString());
}
break;
case KeyTypes.LOWERCASE:
if (char.IsLetter(e.KeyChar))
{
e.Handled = true;
// Convert to lowercase
this.textBox1.AppendText(char.ToLower(e.KeyChar).T oString());
}
break;
}
}

Robert W. wrote:
I'm trying to setup a mechanism whereby I store a formatting code in the Tag
of a textbox. This code might be "UpperCase" or "LowerCase" or "NumericOnly".

I've been trying out various code in KeyPress and KeyDown but haven't been
able to figure out exactly what to do to intercept the keypress and modify
the code accordingly.

So for example, if "UpperCase" is in effect and I receive an "a" then I will
turn it into an "A" on the fly.

Are there any good articles on how to do this?

Dec 3 '05 #2
Hi Robert,

Trapping the KeyPress event and cancelling unwanted keys, you could resend the character as a new character, but translated to upper/lowercase.

This code will translate everything to uppercase, but I'm not sure how good it is as the translated character will always be sent to the application in focus, which basically means, if after clicking you manage to switch applications or give focus to another control it won't have the desired effect.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// if lowercase character, cancel and send the
// uppercase version back to the application
if(Char.IsLower(e.KeyChar))
{
e.Handled = true;
SendKeys.Send(Char.ToUpper(e.KeyChar).ToString());
}
}

--
Happy coding!
Morten Wennevik [C# MVP]
Dec 3 '05 #3
Benny, Morten,

Thank you for your thoughtful responses. Most interesting indeed. I will
check out both asap.

Benny, I actually can't use AppendText because my solution must also work in
the CF which doesn't have this method. But even if I could, wouldn't it fail
if the user typed in something like "abple" and then used the cursor to go
back to change the "b" to a "p"? I say this because I'm thinking that
AppendText would always add to the end of the text, not necessarily in the
middle.

But once again, thank you both!

--
Robert W.
Vancouver, BC
www.mwtech.com

"Morten Wennevik" wrote:
Hi Robert,

Trapping the KeyPress event and cancelling unwanted keys, you could resend the character as a new character, but translated to upper/lowercase.

This code will translate everything to uppercase, but I'm not sure how good it is as the translated character will always be sent to the application in focus, which basically means, if after clicking you manage to switch applications or give focus to another control it won't have the desired effect.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// if lowercase character, cancel and send the
// uppercase version back to the application
if(Char.IsLower(e.KeyChar))
{
e.Handled = true;
SendKeys.Send(Char.ToUpper(e.KeyChar).ToString());
}
}

--
Happy coding!
Morten Wennevik [C# MVP]

Dec 4 '05 #4
You're right - mine will only work if the user never backsteps which
makes it pretty useless ;)

Robert W. wrote:
Benny, Morten,

Thank you for your thoughtful responses. Most interesting indeed. I will
check out both asap.

Benny, I actually can't use AppendText because my solution must also work in
the CF which doesn't have this method. But even if I could, wouldn't it fail
if the user typed in something like "abple" and then used the cursor to go
back to change the "b" to a "p"? I say this because I'm thinking that
AppendText would always add to the end of the text, not necessarily in the
middle.

But once again, thank you both!

Dec 4 '05 #5
Benny Raymond wrote:
You're right - mine will only work if the user never backsteps which
makes it pretty useless ;)
Same code, but instead of AppendText you could set the "SelectedText"
property. SelectedText always is where the cursor is, and it's supported
by the CF.

hth,
Max

Robert W. wrote:
Benny, Morten,

Thank you for your thoughtful responses. Most interesting indeed. I
will check out both asap.

Benny, I actually can't use AppendText because my solution must also
work in the CF which doesn't have this method. But even if I could,
wouldn't it fail if the user typed in something like "abple" and then
used the cursor to go back to change the "b" to a "p"? I say this
because I'm thinking that AppendText would always add to the end of
the text, not necessarily in the middle.

But once again, thank you both!

Dec 4 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ticachua | last post: by
3 posts views Thread by Radovan Radic | last post: by
9 posts views Thread by robert_rowe | last post: by
reply views Thread by Stefan De Schepper | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.