954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

excessive space

how to do error trapping in textbox where the user cannot abuse the space bar.
for example:
i want to type hi
h i

trisha0906
Newbie Poster
18 posts since Dec 2011
Reputation Points: 20
Solved Threads: 0
 

Hi,

Do you want to restrict the spaces in text Box?
If so use the below code

Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Space Then
            e.SuppressKeyPress = True
        End If
End Sub

Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
       TextBox1.Text = TextBox1.Text.Replace(" ", "")
End Sub
kothaisaravan
Junior Poster in Training
51 posts since Oct 2011
Reputation Points: 10
Solved Threads: 2
 

If you wanted to limit the number of spaces, you can use Kothaisaravan's method, and incrememnt a counter every time they press the space bar. Then use an if statement with the counter = number and use the replace.

Begginnerdev
Posting Pro in Training
405 posts since Apr 2010
Reputation Points: 69
Solved Threads: 59
 

Hi,

Do you want to restrict the spaces in text Box? If so use the below code

Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Space Then e.SuppressKeyPress = True End If End Sub

Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown TextBox1.Text = TextBox1.Text.Replace(" ", "") End Sub


hi kothaisaravan, well not really, just the excessive using of the space bar, but the user can still use it like "how are you" not like "how are you"

trisha0906
Newbie Poster
18 posts since Dec 2011
Reputation Points: 20
Solved Threads: 0
 

You can't - or to be exact you can use a dictionary and verify the words between spaces against that, but it's suicidal just to think of it so you can't.

You can count the number of spaces and compare it to the length of the whole string, but you can't be certain if it's been used excessively or the input did infact contain that many spaces.

adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
 

You can't - or to be exact you can use a dictionary and verify the words between spaces against that, but it's suicidal just to think of it so you can't.

You can count the number of spaces and compare it to the length of the whole string, but you can't be certain if it's been used excessively or the input did infact contain that many spaces.

oh, that's unfortunate. but thanks for the help anyway =)

trisha0906
Newbie Poster
18 posts since Dec 2011
Reputation Points: 20
Solved Threads: 0
 


You can change it after their done typing.

Use this code in the lost focus or validated event of the textbox. All excessive spaces will be removed.

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
 
    TextBox1.Text = String.Join(" ", TextBox1.Text.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries))

End Sub


So with that "How[space][space][space]Are" will be "How Are"

Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

You can change it after their done typing.

Use this code in the lost focus or validated event of the textbox. All excessive spaces will be removed.

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
 
    TextBox1.Text = String.Join(" ", TextBox1.Text.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries))

End Sub

So with that "How[space][space][space]Are" will be "How Are"

thank you! i tried it and it worked! thank you very much ^_^

trisha0906
Newbie Poster
18 posts since Dec 2011
Reputation Points: 20
Solved Threads: 0
 

You can change it after their done typing.

Use this code in the lost focus or validated event of the textbox. All excessive spaces will be removed.

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
 
    TextBox1.Text = String.Join(" ", TextBox1.Text.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries))

End Sub

So with that "How[space][space][space]Are" will be "How Are"

thank you! i tried it and it worked! thank you for your help ^_^

trisha0906
Newbie Poster
18 posts since Dec 2011
Reputation Points: 20
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: