e.Handled = true; Not working?!?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Uchiha Jax

    #1

    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


  • Jon Skeet [C# MVP]

    #2
    Re: e.Handled = true; Not working?!?

    Uchiha Jax <i_am_anti_ever ythingNOSPAM@NO SPAMhotmail.com > wrote:[color=blue]
    > 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.....[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Ahmed Qurashi

      #3
      Re: e.Handled = true; Not working?!?

      I think you need to use the KeyPress event to intercept input.

      ok,
      aq

      "Uchiha Jax" <i_am_anti_ever ythingNOSPAM@NO SPAMhotmail.com > wrote in message
      news:CsxLd.294$ UT4.130@newsfe1-win.ntli.net...[color=blue]
      > 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[/color]
      this[color=blue]
      > 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
      >
      >[/color]


      Comment

      • SerhaD Ýnanlý

        #4
        Re: e.Handled = true; Not working?!?

        Use OnKeyPress Event instead of OnKeyUp or Down

        "Uchiha Jax" <i_am_anti_ever ythingNOSPAM@NO SPAMhotmail.com > wrote in message
        news:CsxLd.294$ UT4.130@newsfe1-win.ntli.net...[color=blue]
        > 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[/color]
        this[color=blue]
        > 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
        >
        >[/color]


        Comment

        • Uchiha Jax

          #5
          Re: e.Handled = true; Not working?!?

          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....


          Comment

          • Uchiha Jax

            #6
            Re: e.Handled = true; Not working?!?

            Ah, yes.
            I feel pretty stupid about now.

            Thanks people.
            Jax

            "SerhaD Ýnanlý" <serhadinanli@h otmail.com> wrote in message
            news:%234EOaC%2 3BFHA.3976@tk2m sftngp13.phx.gb l...[color=blue]
            > Use OnKeyPress Event instead of OnKeyUp or Down
            >
            > "Uchiha Jax" <i_am_anti_ever ythingNOSPAM@NO SPAMhotmail.com > wrote in[/color]
            message[color=blue]
            > news:CsxLd.294$ UT4.130@newsfe1-win.ntli.net...[color=green]
            > > 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[/color][/color]
            or[color=blue][color=green]
            > > 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[/color]
            > this[color=green]
            > > line but the char still goes through to the textbox?!?
            > > Does anyone know of any conditions where e.Handled = true; will NOT[/color][/color]
            handle[color=blue][color=green]
            > > the event as I am very confused by all of this.....
            > >
            > > Kind Regards
            > > Jax
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...