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

Unhnd_Exception commented: Just[space][space][space]Because -2
codeorder commented: just.because, w/out [space] +12

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

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.

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"

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.

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 =)

Member Avatar for Unhnd_Exception

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"

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 ^_^

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 ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.