e.Handled = true; Not working?!?

  • Thread starter Thread starter Uchiha Jax
  • Start date Start date
U

Uchiha Jax

I have created a custom textbox which contains an arraylist of permitted
chars.
Any chars that are not in the list are not permitted.
One of the events looks like this and is attached to either this.KeyUp or
this.KeyDown:

private void TextBoxEx_KeyAction(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
EnterPressed(this, e);
}
if(!_allowAll)
{
if(al.IndexOf(e.KeyCode)== -1)
{
e.Handled = true;
}
}
}

As you can see I if the keycode does not exist in the ArrayList named al
e.Handled = true, I check with the debugger and sure enough it DOES run this
line but the char still goes through to the textbox?!?
Does anyone know of any conditions where e.Handled = true; will NOT handle
the event as I am very confused by all of this.....

Kind Regards
Jax
 
Uchiha Jax said:
I have created a custom textbox which contains an arraylist of permitted
chars.
Any chars that are not in the list are not permitted.
One of the events looks like this and is attached to either this.KeyUp or
this.KeyDown:

private void TextBoxEx_KeyAction(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
EnterPressed(this, e);
}
if(!_allowAll)
{
if(al.IndexOf(e.KeyCode)== -1)
{
e.Handled = true;
}
}
}

As you can see I if the keycode does not exist in the ArrayList named al
e.Handled = true, I check with the debugger and sure enough it DOES run this
line but the char still goes through to the textbox?!?
Does anyone know of any conditions where e.Handled = true; will NOT handle
the event as I am very confused by all of this.....

It could be that something else is "unhandling" the event for you...

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
No problem.

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace NewTextBoxExTest
{
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
TextBoxEx txt = new TextBoxEx(true);
txt.Size = new Size(100, 20);
txt.Location = new Point(10,10);
this.Controls.Add(txt);
}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public class TextBoxEx:System.Windows.Forms.TextBox
{
private bool _up;

public TextBoxEx(bool up)
{
_up = up;
if(up)
{
this.KeyUp+=new KeyEventHandler(TextBoxEx_KeyAction);
}
}

private void TextBoxEx_KeyAction(object sender, KeyEventArgs e)
{
e.Handled = true;
}
}
}
}

I'm guessing i've either somehow missed something when I add it to the form
or Visual Studio is doing something to prevent it from happening alothough
both of these are unlikely.
It's probably me, somehow, although i've done code like this loads of times
in the past which has always worked so I cant figure it out....
 
Back
Top