473,249 Members | 1,846 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,249 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 4207
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: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
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...

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.