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. Visual Basic
  4. Remove double spaces in a textbox on keypress

Remove double spaces in a textbox on keypress

Scheduled Pinned Locked Moved Visual Basic
question
4 Posts 4 Posters 6 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.
  • B Offline
    B Offline
    Benniiit
    wrote on last edited by
    #1

    I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces. I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once? Thank you, all.

    H B M 3 Replies Last reply
    0
    • B Benniiit

      I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces. I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once? Thank you, all.

      H Offline
      H Offline
      Hemant Singh Rautela
      wrote on last edited by
      #2

      I write a code in c#.. hope will you get help from this (You can use its logic in VB)... http://hemantrautela.blogspot.com/2012/09/trimming-string-cnet-aspnet.html[^]

      1 Reply Last reply
      0
      • B Benniiit

        I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces. I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once? Thank you, all.

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Member 11078565 wrote:

        I used the
        replace function but it does not work well.

        What does that mean? Did you use it correctly?

        yourString = yourString.Replace(" ", " ")

        or with Regular Expressions:

        yourString = System.Text.RegularExpressions.Regex.Replace(yourString,"\\s+"," ")

        1 Reply Last reply
        0
        • B Benniiit

          I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces. I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once? Thank you, all.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Trap the keydown/up event, inspect the current and previous characters in the string, if they are both a space then deal with it in the event.

          Never underestimate the power of human stupidity RAH

          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