Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Prevent the ^ character from beeing entered in textbox.

Prevent the ^ character from beeing entered in textbox.

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xkrja
    wrote on last edited by
    #1

    How can I prevent the ^ character from being typed in a textbox? The problem seems to be that the character is not added before another keydown/keyup event. For some other keys I used the following code to prevent them from being typed:

    void textBox_KeyUp(object sender, KeyEventArgs e)
    {
    if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
    {
    if (e.PlatformKeyCode <= 12)
    e.Handled = true;

                switch (e.PlatformKeyCode)
                {
                    case 226:
                        e.Handled = true;
                        break;
                }
            }
            else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
            {
                switch (e.PlatformKeyCode)
                {
                    case 52:
                        e.Handled = true;
                        break;
                    case 186:
                        e.Handled = true;
                        break;
                }
            }
        }
    

    However, this does not work for the ^ character. Thanks for help!

    L OriginalGriffO 2 Replies Last reply
    0
    • X xkrja

      How can I prevent the ^ character from being typed in a textbox? The problem seems to be that the character is not added before another keydown/keyup event. For some other keys I used the following code to prevent them from being typed:

      void textBox_KeyUp(object sender, KeyEventArgs e)
      {
      if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
      {
      if (e.PlatformKeyCode <= 12)
      e.Handled = true;

                  switch (e.PlatformKeyCode)
                  {
                      case 226:
                          e.Handled = true;
                          break;
                  }
              }
              else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
              {
                  switch (e.PlatformKeyCode)
                  {
                      case 52:
                          e.Handled = true;
                          break;
                      case 186:
                          e.Handled = true;
                          break;
                  }
              }
          }
      

      However, this does not work for the ^ character. Thanks for help!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
      if(e.KeyChar == '^')
      {
      e.Handled = true;
      }
      }

      1 Reply Last reply
      0
      • X xkrja

        How can I prevent the ^ character from being typed in a textbox? The problem seems to be that the character is not added before another keydown/keyup event. For some other keys I used the following code to prevent them from being typed:

        void textBox_KeyUp(object sender, KeyEventArgs e)
        {
        if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
        {
        if (e.PlatformKeyCode <= 12)
        e.Handled = true;

                    switch (e.PlatformKeyCode)
                    {
                        case 226:
                            e.Handled = true;
                            break;
                    }
                }
                else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                {
                    switch (e.PlatformKeyCode)
                    {
                        case 52:
                            e.Handled = true;
                            break;
                        case 186:
                            e.Handled = true;
                            break;
                    }
                }
            }
        

        However, this does not work for the ^ character. Thanks for help!

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Couple of things: 1) KeyUp is probably not a good place to disable keys, as KeyPress happens repeatedly before KeyUp occurs. Try handling KeyDown, KeyPress and KeyUp with Console.WriteLine in each to show the flow of events. 2) If you are going to use magic numbers then comment the damn things so the next poor sod stands a chance of understanding what you are doing:

        // Disable '%' key
        case 52:

        is a lot more useful than

        case 52:

        By preference, use '%' instead of magic numbers, or constants with sensible names, or even "Keys." 3) Use KeyPress to disable your code:

            private void textBox\_KeyPress(object sender, KeyPressEventArgs e)
                {
                if (e.KeyChar == '^')
                    {
                    e.Handled = true;
                    }
                }
        

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        X 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Couple of things: 1) KeyUp is probably not a good place to disable keys, as KeyPress happens repeatedly before KeyUp occurs. Try handling KeyDown, KeyPress and KeyUp with Console.WriteLine in each to show the flow of events. 2) If you are going to use magic numbers then comment the damn things so the next poor sod stands a chance of understanding what you are doing:

          // Disable '%' key
          case 52:

          is a lot more useful than

          case 52:

          By preference, use '%' instead of magic numbers, or constants with sensible names, or even "Keys." 3) Use KeyPress to disable your code:

              private void textBox\_KeyPress(object sender, KeyPressEventArgs e)
                  {
                  if (e.KeyChar == '^')
                      {
                      e.Handled = true;
                      }
                  }
          

          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

          X Offline
          X Offline
          xkrja
          wrote on last edited by
          #4

          Thanks for the replies guys. Sorry, but I forgot to mention that I work in Silverlight. I can't get to the KeyPress event. Only KeyDown and KeyUp. Thanks again.

          H 1 Reply Last reply
          0
          • X xkrja

            Thanks for the replies guys. Sorry, but I forgot to mention that I work in Silverlight. I can't get to the KeyPress event. Only KeyDown and KeyUp. Thanks again.

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            In that case you should probably post your question in the SilverLight Forum.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups