473,804 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

e.Handled = true; Not working?!?

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_KeyAc tion(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
EnterPressed(th is, 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
Nov 16 '05 #1
5 17675
Uchiha Jax <i_************ ************@NO SPAMhotmail.com > wrote:
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_KeyAc tion(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
EnterPressed(th is, 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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
I think you need to use the KeyPress event to intercept input.

ok,
aq

"Uchiha Jax" <i_************ ************@NO SPAMhotmail.com > wrote in message
news:Cs******** *******@newsfe1-win.ntli.net...
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_KeyAc tion(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
EnterPressed(th is, 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

Nov 16 '05 #3
Use OnKeyPress Event instead of OnKeyUp or Down

"Uchiha Jax" <i_************ ************@NO SPAMhotmail.com > wrote in message
news:Cs******** *******@newsfe1-win.ntli.net...
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_KeyAc tion(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
EnterPressed(th is, 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

Nov 16 '05 #4
No problem.

using System;
using System.Drawing;
using System.Collecti ons;
using System.Windows. Forms;
using System.Data;

namespace NewTextBoxExTes t
{
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.A dd(txt);
}

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

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

private void TextBoxEx_KeyAc tion(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....
Nov 16 '05 #5
Ah, yes.
I feel pretty stupid about now.

Thanks people.
Jax

"SerhaD Ýnanlý" <se**********@h otmail.com> wrote in message
news:%2******** **********@tk2m sftngp13.phx.gb l...
Use OnKeyPress Event instead of OnKeyUp or Down

"Uchiha Jax" <i_************ ************@NO SPAMhotmail.com > wrote in message news:Cs******** *******@newsfe1-win.ntli.net...
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_KeyAc tion(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
EnterPressed(th is, 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


Nov 16 '05 #6

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

Similar topics

2
2721
by: Amos Soma | last post by:
In my Win form, I have KeyPreview set to True. In my form KeyUp method, I have the following code: if (e.KeyCode == Keys.F10) { e.Handled = true; MessageBox.Show( "F10 pressed" ); } The message is displayed correctly. However, focus moves off my form and
2
14478
by: Keith Smith | last post by:
Should I always include e.Handled=true at the end of my event handler?
1
3570
by: Keith Smith | last post by:
If a KeyUp event takes place on my form, how can I keep the KeyDown/KeyPress/KeyUp events from being run on the control event itself. I tried putting e.Handled=true; at the end of my KeyUp event, but that doesn't seem to work. Am I missing something? It appears that the KeyUp event on my control still runs. Help!
4
7563
by: Tom | last post by:
I have a ComboBox that I wrote a KeyPress event for. Basically, it looks to make sure that a numeric key has been pressed; if not, then it does a Beep() and sets e.Handled to True. This should, as I understand it, cause the character to 'go away' and not be displayed in the box. I have tried this on a plain old TextBox and it functions as it should. However, with a ComboBox the character stays in the box! The code (via the debugger)...
2
3068
by: **Developer** | last post by:
I have a usercontrol that contains the following. To my surprise the form containing this control get KeyUp events. Help says that for KeyPress setting e.Handled = True suppresses KeyPress events but say very little about KeyUp. What does setting to Handled do?
4
6981
by: **Developer** | last post by:
I have a usercontrol that contains the following. To my surprise the form containing this control get KeyUp events. Help says that for KeyPress setting e.Handled = True suppresses KeyPress events but say very little about KeyUp. What does setting to Handled do?
4
4993
by: Udi | last post by:
Hi All, MyRichEdit is derived from RichTextBox. I'm overriding OnKeyDown. I'm trying to handle the TAB key but without printing it in the edit box: protected override bool IsInputKey(Keys keyData) { switch (keyData) {
0
874
by: DanWeaver | last post by:
Using asp and SQLserver2005 on shared server and with connection string: <connectionStrings> <remove name="LocalSqlServer"/> <add name="LocalSqlServer" connectionString="Data Source=HYDROGEN;Initial Catalog=dbred;Integrated Security=false;User Id=theuser;Password=blahblah;" providerName="System.Data.SqlClient" /> </connectionStrings>
0
1782
by: rajkumar K | last post by:
Hi,I am developing windows application using c#. i have placed the textbox in 6thpanel of my application.when im pressing backspace in keyboard it will delete only one char. when im holding backspace also it was delete only one char. I was written following code in keyup event if (e.KeyData == Keys.Back) { SendKeys.Send("+{BS}"); SendKeys.Send("{BREAK}");
0
9704
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
9571
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
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
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.