473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 4210
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ticachua | last post by:
Hello all, Does anyone know how to intercept user's keypress or mouseclick in Python? I'd like to write a small tool (Python-based preferrably) running on Windows to capture the user's...
9
by: Dan K. | last post by:
Hi Folks, Problem is, that on one of my controls the DEL key wont work in textboxes. i have different controls of this kind in my app and this is the only one the DEL Key wont do his job....
3
by: Radovan Radic | last post by:
Hello all, I am new to .NET and a i am moving from Delphi (finally!) to C#. Now, i am trying to develop custom TextBox for numeric input. I have done it almost, now i need to prevent paste so...
3
by: Vadim Rapp | last post by:
Hi: On my form, I have textbox and combo box. Form's KeyPreview = True. It does work for the textbox, but not for the combo. I.e. Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As...
9
by: robert_rowe | last post by:
Does anyone know how to detect keypresses in a Datagrid cell? The Keypress event ofthe datagrid doesn't fire if you are in a cell. Neither does KeyUp & KeyDown. I've tried hooking in to the...
0
by: Stefan De Schepper | last post by:
Dear NG, I created a gridlike usercontrol. When clicking on a cell a textbox (or other control, depending on the cell's editortype) is shown. When some other control gets the focus, the textbox...
6
by: MLH | last post by:
Using A97. Want to examine 17-char VIN entered by user. VIN codes are alphanumeric and do not contain Oh's to prevent the confusion that could result if zeros were misread as O's or o's. So, if...
3
by: Simon Verona | last post by:
Sorry for the repost, but this group seems to be more "active" than the group that I posted my question, and it's probably as valid here as there! I have a usercontrol, which contains a textbox...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.