|
-
Apr 17th, 2008, 10:39 PM
#1
Thread Starter
Lively Member
[RESOLVED] How to detect two uppercase letters.
Hey. I wanna do something so when someone is typing in a textbox it will detect two capital letters and change it to one, like for example, "I bought a NEw phone yesterday." It would detect that the word, "new" had two capital letters and it changes it back to "New."
Anyone know how to do this?
-
Apr 17th, 2008, 10:45 PM
#2
Re: How to detect two uppercase letters.
Will StrConv() suffice for your needs?
MsgBox ("I bought a NEw phone yesterday.", vbProperCase)
EDIT: Ooops... forgot that it capitalizes start of all words. You'll have to scan string two characters at a time from position 1 to Len(string) - 1. Personally I would go for byte array implementation, but you can also use Mid().
Last edited by leinad31; Apr 17th, 2008 at 10:48 PM.
-
Apr 17th, 2008, 10:51 PM
#3
Thread Starter
Lively Member
Re: How to detect two uppercase letters.
Ok. Thanks. I'll try find something like that
-
Apr 17th, 2008, 10:58 PM
#4
Re: How to detect two uppercase letters.
Code:
Option Explicit
Private Sub Form_Load()
Dim pos As Long
Dim strTemp As String
Dim lngCharA As Long
Dim lngCharB As Long
strTemp = "I bought a NEw phone yesterday."
For pos = 1 To Len(strTemp) - 1
lngCharA = Asc(Mid(strTemp, pos, 1))
lngCharB = Asc(Mid(strTemp, pos + 1, 1))
If (lngCharA >= 65 And lngCharA <= 90) And (lngCharB >= 65 And lngCharB <= 90) Then
Mid(strTemp, pos + 1, 1) = LCase(Chr(lngCharB))
End If
Next
MsgBox strTemp
End Sub
-
Apr 17th, 2008, 11:06 PM
#5
Re: How to detect two uppercase letters.
StrConv(x,vbProperCase) won't help as it will capitalize the first letter of all the words.
Instead you must loop thru the string, set a flag when a capital letter is found. So when you get the second capital letter and you see that the flag is true you know you need to replace it.
This code might help (not tested).
Note that this is not the most efficient code and there could be better methods for doing this too.
vb Code:
Function ReplaceTwoCapitalsWithOne(ByVal sourceString as String) Dim found as Boolean For i = 0 To Len(sourceString) -1 If Mid(sourceString, i, 1) like "[A-Z]" Then If found Then Mid(sourceString, i, 1) = Lcase(Mid(sourceString, i, 1)) found = True Else found = False End If Next ReplaceTwoCapitalsWithOne = sourceString End Sub
Pradeep
Last edited by Pradeep1210; Apr 17th, 2008 at 11:13 PM.
-
Apr 17th, 2008, 11:09 PM
#6
Re: How to detect two uppercase letters.
Try this, wrote it really quick:
Code:
Private Sub Text1_Change()
On Error Resume Next
Dim bytCur As Integer, lonLen As Long
Dim lonSel As Long, lonPrevSpace As Long
Dim strText As String
strText = Text1.Text
lonLen = Len(strText)
lonSel = Text1.SelStart
If lonLen > 0 Then
If lonSel < 2 Then
bytCur = CByte(Asc(Mid$(strText, 1, 1)))
Else
bytCur = CByte(Asc(Mid$(strText, lonSel - 1, 1)))
lonPrevSpace = InStrRev(strText, " ", lonSel)
End If
If lonSel > 0 Then
If lonPrevSpace + 1 <> (lonSel - 1) Then
If bytCur >= 65 And bytCur <= 90 Then
Mid$(strText, lonSel - 1, 1) = Chr$(bytCur + 32)
Text1.Text = strText
Text1.SelStart = lonSel
End If
End If
End If
End If
End Sub
-
Apr 17th, 2008, 11:17 PM
#7
Thread Starter
Lively Member
Re: How to detect two uppercase letters.
Thanks, I used DigiRev's and it worked .
Resolved
-
Apr 18th, 2008, 05:12 PM
#8
Re: [RESOLVED] How to detect two uppercase letters.
Another option:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 65 To 90 ' A to Z
With Text1
If .SelStart > 0 Then
Select Case Asc(Mid$(.Text, .SelStart, 1))
Case 65 To 90: KeyAscii = KeyAscii + 32
End Select
End If
End With
End Select
End Sub
-
Apr 18th, 2008, 05:45 PM
#9
Re: [RESOLVED] How to detect two uppercase letters.
 Originally Posted by Logophobic
Another option:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 65 To 90 ' A to Z
With Text1
If .SelStart > 0 Then
Select Case Asc(Mid$(.Text, .SelStart, 1))
Case 65 To 90: KeyAscii = KeyAscii + 32
End Select
End If
End With
End Select
End Sub
Much better. I tend to write sloppy and sometimes redundantly when I'm tired or rushing through it...
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
|