Cant Intercept DEL Key .. not recognized ...

  • Thread starter Thread starter Dan K.
  • Start date Start date
D

Dan K.

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 tabcontrol you cant use DEL if it works in
all other parts :(

private void OnKeyDown(object sender,KeyEventArgs AArgs)

{

base.OnKeyDown(AArgs);

System.Windows.Forms.Keys LKey =
System.Windows.Forms.Keys.KeyCode &
AArgs.KeyCode;

switch (LKey)

{

case System.Windows.Forms.Keys.Delete :

MessageBox.Show("DEL Key!");

break;

}

}
 
Well, First I would call base.OnKeyDown... after my code. Not that I
actually call it at all, it gets called anyway.

Second, using the bitmask is for extracting KeyValue to obtain the
KeyCode. You already have the KeyCode in AArgs.KeyCode so there is no
need to use the bitmask.

Third, the code is valid and a textbox does fire an event on the delete
key so somehow your code isn't tied to it.
 
Dan,

Your code you posted should be intercepting the delete key.

One idea my be that the KeyUp event was added incorrectly. Make sure
the following line of code was added to you InitializeComponent()
method.

this.textBox1.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.OnKeyDown);

If this doesn't help, if you could post or email me your source for
the form I would be willing to help figure out the problem.

Glen Jones MCSD
 
sorry for the late answer ... was on holiday the last weeks :)

problem still active ...

at my view its not the implemententation of the of the keydown event
cause this implementation was only a test to check if DEL Key fires an
event.
the naturally DEL Function (delete content) dont do his job on this
control. the test should only show why.
but its still not clear for me. any further idea ?

glen can i still send you the source of this control to check ?
 
I may be out of topic. I do not have the history of this message. However,
If you are playing with a form, what about this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData==Keys.Delete)
{
MessageBox.Show("Delete pressed!");
return true;
}
return base.ProcessCmdKey (ref msg, keyData);
}



José
 
Dan,

I would be happy to help.

glen lee jones AT comcast.net ( no spaces )

I'll try to respond as quick as I can.
 
i´ll give it a try tomorrow ... today im to tired :)
i will reporting whats happen ... strange is also that the DEL key
with his standard delete function works if i inialize the control new
... without content ... if i load data into the txtboxes so the arent
"new" the DEL key does nothing.
 
seems that José´s method is a good start to solve this problem ...
with his code snippet the box reacts to the DEL key. with my code
posted above there was no event / no msgbox. so i must only find a way
to check via the msg.HwND which box fires this event and than i should
delete content .. or ?

@glen

only the control source enough ? cause project is to big to send all
 
now i have this code and it works

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData==Keys.Delete)
{
SetWindowText(msg.HWnd.ToInt64( ), String.Empty);
return true;
} return base.ProcessCmdKey (ref msg, keyData);
}


content of the focused box will be deleted if i hit the DEL Key. Not a
solution but a first workaround ... maybe Glen could say more :)
 
Dan K.

I went back and looked at your original post. I can also understand having
to much code to post. So let me see if I can help.

First, the ProcessCmdKey method example that was posted will work but
becareful. That method is form level not just for a text box.

Ok, the first thing to check is if the Form.KeyPreview property is true and
you intercept the delete key and then set the KeyEventArgs.Handled property
to true, the event will never get called.

The Next thing to check is that the Initialization of the Textbox includes
the following line:
this.textBox1.KeyDown += new KeyEventHandler( this.textBox1_KeyDown );

If that is there every thing should work.

I have posted the code that works at the end of this post.

Hope this helps.
--
Glen Jones MCSD

Sample Code
**************************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace CWinTest
{
public class Form1 : System.Windows.Forms.Form
{
private TextBox textBox1;
private Container components = null;

public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
if (components != null)
components.Dispose();

base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.textBox1 = new TextBox();
this.SuspendLayout();
// textBox1
this.textBox1.Location = new Point(24, 56);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new Size(192, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
this.textBox1.KeyDown += new KeyEventHandler(
this.textBox1_KeyDown );
// Form1
this.AutoScaleBaseSize = new Size(5, 13);
this.ClientSize = new Size(292, 273);
this.Controls.AddRange(new Control[] { this.textBox1 } );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
switch( e.KeyCode )
{
case Keys.Delete :
MessageBox.Show("DEL Key!");

e.Handled = true;
break;
}
}
static void Main()
{
Application.Run(new Form1());
}
}
}
 
Back
Top