|
-
Apr 2nd, 2005, 03:20 AM
#1
Thread Starter
Lively Member
Remove double spaces between words(Resolved)
Hi,
I have a textBox for the user to key in their name. Please help me how do remove or avoid the double spaces between words.
Thanks !
Vivian
Last edited by vivian2u; Apr 2nd, 2005 at 03:52 AM.
-
Apr 2nd, 2005, 03:23 AM
#2
Re: Remove double spaces between words
First, add this code to your project...it will remove all instances of 2 or more spaces, so there will only be 1 space.
VB Code:
Function SingleSpaces(ByVal TextString As String) As String
Dim lonLen As Long, lonLoop As Long
Dim strCur As String, strNext As String
Dim strTemp As String, strRet As String
strTemp = TextString
lonLen = Len(strTemp)
If lonLen = 0 Then Exit Function
For lonLoop = 1 To lonLen
strCur = Mid$(strTemp, lonLoop, 1)
If strCur = " " Then
strNext = Mid$(strTemp, lonLoop + 1, 1)
If Not strNext = " " Then
strRet = strRet & strCur
End If
Else
strRet = strRet & strCur
End If
Next lonLoop
SingleSpaces = strRet
End Function
Then, you can remove all spaces by using the code:
VB Code:
TextBox.Text = SingleSpaces(TextBox.Text)
Where TextBox is the name of the textbox the user enters their name into.
-
Apr 2nd, 2005, 03:24 AM
#3
Re: Remove double spaces between words
VB Code:
Option Explicit
Private Sub Command_Click()
If InStr(Text.Text, " ") Then
MsgBox "instr"
Text.Text = Replace(Text.Text, " ", " ")
End If
End Sub
-
Apr 2nd, 2005, 03:39 AM
#4
Thread Starter
Lively Member
Re: Remove double spaces between words
Hi,
Thanks for the help, but the problem is when the user press the space bar twice, the cursor will move back to the beginning of the words. How to solve the problem ? It has to avoid the user to press the spaces bar more than once..
vivian
-
Apr 2nd, 2005, 03:46 AM
#5
Re: Remove double spaces between words
VB Code:
Private Sub txtName_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then
If Len(Text1.Text) > 0 Then
If Right$(Text1.Text, 1) = " " Then
KeyAscii = 0
End If
End If
End If
End Sub
-
Apr 2nd, 2005, 03:51 AM
#6
Thread Starter
Lively Member
Re: Remove double spaces between words
Thank you. Thats exactly what I want.
vivian
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|