I cannot get the space to show up in between the 5 letters what am I missing? He
ID: 3889652 • Letter: I
Question
I cannot get the space to show up in between the 5 letters what am I missing?
Here is the code:
Imports System.Reflection
Public Class MainForm
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub lettersTextBox_Enter(sender As Object, e As EventArgs) Handles lettersTextBox.Enter
lettersTextBox.SelectAll()
End Sub
Private Sub lettersTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles lettersTextBox.KeyPress
' accepts only letters and the Backspace key
If (e.KeyChar.ToString.ToUpper < "A" OrElse e.KeyChar.ToString.ToUpper > "Z") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub lettersTextBox_TextChanged(sender As Object, e As EventArgs) Handles lettersTextBox.TextChanged
lettersWithSpacesLabel.Text = String.Empty
End Sub
Private Sub insertButton_Click(sender As Object, e As EventArgs) Handles insertButton.Click
' inserts a space after each letter
Dim letters As String
letters = lettersTextBox.Text
If letters.Length = 5 Then
lettersWithSpacesLabel.Text = letters.Substring(0, 5)
For index As Integer = 0 To letters.Length - 5
lettersWithSpacesLabel.Text = lettersWithSpacesLabel.Text & " " & letters.Substring(index, 0)
Next index
Else
MessageBox.Show("Please enter 5 letters", "Debugging Exercise", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
lettersTextBox.Focus()
End Sub
End Class
Explanation / Answer
Because of below code it was not allowed to add spaces in text box
Private Sub lettersTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles lettersTextBox.KeyPress
' accepts only letters and the Backspace key
If (e.KeyChar.ToString.ToUpper < "A" OrElse e.KeyChar.ToString.ToUpper > "Z") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
you have to modify the code like below to take spaces like below or you can also remove the if condition in the above code whatever you like:
Private Sub lettersTextBox_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles lettersTextBox.KeyPress
' accepts only letters and the Backspace key
If (e.KeyChar.ToString.ToUpper < "A" OrElse e.KeyChar.ToString.ToUpper > "Z") Then
e.Handled = True
End If
If (e.KeyChar.ToString.Contains(" ")) Then
e.Handled = False
End If
End Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.