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

backspace

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 4 Posters 1 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.
  • S Offline
    S Offline
    sarojkumarjena
    wrote on last edited by
    #1

    When I will press the backspace it shouldnt delete at a paricular position.How to do it

    N C L 3 Replies Last reply
    0
    • S sarojkumarjena

      When I will press the backspace it shouldnt delete at a paricular position.How to do it

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello There are several approaches. This is the one I use:

      bool ToChange = true;
      string OldText = "";

      private void TextBox_KeyDownHandler(object sender, KeyDownArgs e)
      {
      If(e.KeyChar == "\b")
      {
      OldText = TextBox.Text;
      ToChange = false;
      }
      else
      ToChange = true;
      }

      private void TextBox_TextChanged((object sender, KeyDownArgs e)
      {
      If(!ToChange)
      {
      TextBox.Text = OldText;
      TextBox.SelectionStart = TextBox.TextLength;
      }
      }

      You see. The text hasn't changed when the keydown event was fired. So if we don't want to accept thechanges we store the original text in a string. Once the text has changed, the TextChanged event will be fired. It will check to see if we want to accept the changes. If not, revert to the old text. If anyone got a better approach, please post.:) Regards:rose:

      1 Reply Last reply
      0
      • S sarojkumarjena

        When I will press the backspace it shouldnt delete at a paricular position.How to do it

        C Offline
        C Offline
        coolestCoder
        wrote on last edited by
        #3

        Hi, You can also set the KeyChar = 0 in the KeyPress event ! I think you will have to cast 0 in to char for such assignment. Try it ! Hope it works !


        "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


        Anant Y. Kulkarni

        1 Reply Last reply
        0
        • S sarojkumarjena

          When I will press the backspace it shouldnt delete at a paricular position.How to do it

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

          private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
          {
          if(e.KeyChar == (char)Keys.Back)
          e.Handled = true;
          }

          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