473,407 Members | 2,676 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,407 software developers and data experts.

How to trap Delete keypress?

In my WinForms app I wanted to implement a Read-Only textbox. I didn't like
the appearance a textbox takes on when the ReadOnly property is set true so
instead I trapped the KeyPress event with a generic event handler:

private void textBoxMakeReadOnly(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
I *thought* this was working perfectly until I selected some characters and
pressed 'Delete'. It didn't trap the Delete key! I even tried monitoring
the KeyDown event but no luck there either.

So how does one trap the pressing of the Delete key?

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

May 13 '06 #1
3 4972
Robert W. <Ro*****@discussions.microsoft.com> wrote:
In my WinForms app I wanted to implement a Read-Only textbox. I didn't like
the appearance a textbox takes on when the ReadOnly property is set true
By default, it changes the background color to SystemColors.ButtonFace.
You can still change the BackColor property to a different color. If you
set it to SystemColors.Window, then the color will be the same as an
enabled text control.

Be careful what you implement, though - if a cursor is visible and the
background color is SystemColors.Window, users will be surprised to find
that the text is read-only.
I *thought* this was working perfectly until I selected some characters and
pressed 'Delete'. It didn't trap the Delete key! I even tried monitoring
the KeyDown event but no luck there either.


If you handle the KeyDown event as well, it will block the Delete key:

---8<---
using System;
using System.Windows.Forms;
using System.Drawing;

class App
{
static void Main()
{
Form form = new Form();
TextBox box = new TextBox();
box.Location = new Point(10, 10);
box.Size = new Size(100, 20);
box.KeyPress += delegate(object sender, KeyPressEventArgs e)
{
e.Handled = true;
};
box.KeyDown += delegate(object sender, KeyEventArgs e)
{
e.Handled = true;
};
box.Parent = form;
box.Text = "This is test text";
Application.Run(form);
}
}
--->8---

The text control shown by this program will block both new character
input and special keys like delete and arrow keys. In fact, it even
blocks key combinations like Alt+F4.

-- Barry
May 13 '06 #2
On Sat, 13 May 2006 16:03:01 -0700, Robert W. wrote:
So how does one trap the pressing of the Delete key?


As Barry says, don't confuse users, just change the background in a
different way if you want it to be a standard within your app.
You should ideally create your own control too so you can globally change
it if you feel the need later.

e.g.
class ReadonlyTextbox : System.Windows.Forms.TextBox
{
public ReadonlyTextbox()
{
this.ReadOnly = true;
this.BackColor = System.Drawing.Color.Yellow;
}
}

Cheers,
Jason
May 14 '06 #3
Barry, Jason:

Thank you! I feel stupid because I never realized all this before. Thanks
for clearing it up for me!

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

"JasonS" wrote:
On Sat, 13 May 2006 16:03:01 -0700, Robert W. wrote:
So how does one trap the pressing of the Delete key?


As Barry says, don't confuse users, just change the background in a
different way if you want it to be a standard within your app.
You should ideally create your own control too so you can globally change
it if you feel the need later.

e.g.
class ReadonlyTextbox : System.Windows.Forms.TextBox
{
public ReadonlyTextbox()
{
this.ReadOnly = true;
this.BackColor = System.Drawing.Color.Yellow;
}
}

Cheers,
Jason

May 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hi, I'm developing a testing application where i have to trap every keyboard press event. Which windows API can i use to trap all keyboard events. Thanks in advance - Dave
2
by: Fred Sawtelle | last post by:
I'm trying to trap the Enter key in any of the key events (keypress, keydown, keyup) of the Windows .Net combobox control. Try as I might, I cannot get the event to respond to an Enter key press;...
0
by: Mike Smith | last post by:
Can the DEL key be trapped on the datagrid (windows forms version) ? It doesnt seem to respond to the keypress and keydown events of the grid.
3
by: Cystm | last post by:
I am trying to create a custom text box that performs a function on every key press, and it works perfectly, except that the 'delete' key (and the arrow keys) does not fire an event. ...
1
by: bie2 | last post by:
Hi, I have a datagrid with many records, I want to delete all of them. I'm doing CTRL-A and press delete...I take a long time to delete it and the UI is froze during that time. So I would...
9
by: Woody Splawn | last post by:
On a winform, how do I trap for the user pressing Control Delete? I thought perhaps something like the following in the keydown event of the grid, but the code below does not work. If e.KeyCode...
7
by: Lars Netzel | last post by:
Hi How do I in a textbox only allow users to type in numbers and comma and dot ? they should of course be able to delete and backspace the stuff they type in too.. I figured keyDown would...
2
by: David McDivitt | last post by:
Another fellow here must modify an application. On the web page, pressing the enter key causes the form to submit. What users want is to remove enter key functionality from within a textarea and...
2
by: Dom | last post by:
For various reasons, I need to remove the event handlers for a given event. The event can only appear on the left side of "+=" or "-=", so I'm not sure how this is done. I tried this: while...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.