How to best intercept keypresses in a TextBox?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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).ToString());
}
break;
case KeyTypes.LOWERCASE:
if (char.IsLetter(e.KeyChar))
{
e.Handled = true;
// Convert to lowercase
this.textBox1.AppendText(char.ToLower(e.KeyChar).ToString());
}
break;
}
}
 
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());
}
}
 
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!
 
You're right - mine will only work if the user never backsteps which
makes it pretty useless ;)
 
Benny said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top