473,799 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "NumericOnl y".

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 4230
This works:

enum KeyTypes
{
UPPERCASE,
LOWERCASE,
NUMERIC
}
private void textBox1_KeyPre ss(object sender, KeyPressEventAr gs e)
{
// Set the tag like this following line:
//this.textBox1.T ag = KeyTypes.UPPERC ASE;

// decide what the tag is
switch ((KeyTypes)this .textBox1.Tag)
{
case KeyTypes.NUMERI C:
if (!char.IsDigit( e.KeyChar))
{
// this is not a digit... Handled gets set to true
e.Handled = true;
}
break;
case KeyTypes.UPPERC ASE:
if (char.IsLetter( e.KeyChar))
{
e.Handled = true;
// Convert to uppercase
this.textBox1.A ppendText(char. ToUpper(e.KeyCh ar).ToString()) ;
}
break;
case KeyTypes.LOWERC ASE:
if (char.IsLetter( e.KeyChar))
{
e.Handled = true;
// Convert to lowercase
this.textBox1.A ppendText(char. ToLower(e.KeyCh ar).ToString()) ;
}
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 "NumericOnl y".

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_KeyPre ss(object sender, KeyPressEventAr gs e)
{
// if lowercase character, cancel and send the
// uppercase version back to the application
if(Char.IsLower (e.KeyChar))
{
e.Handled = true;
SendKeys.Send(C har.ToUpper(e.K eyChar).ToStrin g());
}
}

--
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_KeyPre ss(object sender, KeyPressEventAr gs e)
{
// if lowercase character, cancel and send the
// uppercase version back to the application
if(Char.IsLower (e.KeyChar))
{
e.Handled = true;
SendKeys.Send(C har.ToUpper(e.K eyChar).ToStrin g());
}
}

--
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 "SelectedTe xt"
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
3064
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 keypresses and mouseclicks and write them to a file. Thanks for your help.
9
4354
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. BACKSPACE and rightclick menu delete works either. seems the DEL Key fires no event ... i insert following code in my project to check this. no mehthod call if DEL key hit ... any solution or idea for this problem ? cant say ... users on this...
3
15301
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 someone couldnt paste letters into numeric text box. How this can be done in C#? In Delphi there was method to implement methods wiht message code WM_PASTE, and within that method i could control the contents of the pasted text. Thanks.
3
2358
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 System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress me.Text = e.KeyChar End Sub
9
3809
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 DataGridCell events but it doesn't have the keypress event. Robert
0
1507
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 is hidden. So far, no problem. But when the textbox is visible and, for instance, I click on a toolbarbutton, the textbox' Lostfocus event is not fired. The situation should be that wherever I click, apart from the cell or textbox itself, the...
6
4252
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 a user types a 17-char VIN into the textbox that has an Oh in it (lower or upper case) - I would like to change it to a zero during the BeforeUpdate code. So far, I've not been able to accomplish this. I can examine
3
5521
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 (as well as other controls). The textbox is set for multiline, so it *should* accept an Enter Key to move down to the next line. However, I'm finding that if I put the control onto a form which has a default Accept button set, then pressing...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.