ghadley
asked on
VB Simple Question (easy 50 pts)
Here it is:
I want the user to type in as string like (I go to VB school) into a textbox. Each time the user clicks a command button, it will capture the location of the space and the string before space. e.g. when the user click a command button for first time, it shows location of space AND character which is "I", when the user click a command button for second time, it shows the location of the space again, and "go".....etc.
It is pretty straight forward, but I would like to know the logic more than anything else.
I want the user to type in as string like (I go to VB school) into a textbox. Each time the user clicks a command button, it will capture the location of the space and the string before space. e.g. when the user click a command button for first time, it shows location of space AND character which is "I", when the user click a command button for second time, it shows the location of the space again, and "go".....etc.
It is pretty straight forward, but I would like to know the logic more than anything else.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry, try this:
Dim strings() As String
Dim counter, x As Integer
Dim iLocation As Integer
Private Sub Command1_Click()
iLocation = 0
For x = 0 To counter
iLocation = iLocation + Len(strings(x)) + 1
Next x
If iLocation < Len(Text1.Text) Then
MsgBox "String: " & strings(counter) & vbCrLf & "Location: " & iLocation
Else
MsgBox "String: " & strings(counter) & vbCrLf & "No space after"
End If
If counter = UBound(strings) Then
counter = 0
Else
counter = counter + 1
End If
End Sub
Private Sub Form_Load()
strings = Split(Text1.Text, " ")
End Sub
Dim strings() As String
Dim counter, x As Integer
Dim iLocation As Integer
Private Sub Command1_Click()
iLocation = 0
For x = 0 To counter
iLocation = iLocation + Len(strings(x)) + 1
Next x
If iLocation < Len(Text1.Text) Then
MsgBox "String: " & strings(counter) & vbCrLf & "Location: " & iLocation
Else
MsgBox "String: " & strings(counter) & vbCrLf & "No space after"
End If
If counter = UBound(strings) Then
counter = 0
Else
counter = counter + 1
End If
End Sub
Private Sub Form_Load()
strings = Split(Text1.Text, " ")
End Sub
Private Sub Command1_Click()
Static lFirstSpacePos As Long
Static lSecondSpacePos As Long
lFirstSpacePos = lSecondSpacePos
If lFirstSpacePos = 0 Then
lFirstSpacePos = 1
End If
lSecondSpacePos = InStr(lFirstSpacePos + 1, Text1.Text, " ", vbTextCompare)
If lSecondSpacePos = 0 Then
lSecondSpacePos = Len(Text1.Text) + 1
Else
lSecondSpacePos = lSecondSpacePos + 1
End If
Text1.SelStart = lFirstSpacePos - 1
Text1.SelLength = lSecondSpacePos - lFirstSpacePos
Call Text1.SetFocus
End Sub
Static lFirstSpacePos As Long
Static lSecondSpacePos As Long
lFirstSpacePos = lSecondSpacePos
If lFirstSpacePos = 0 Then
lFirstSpacePos = 1
End If
lSecondSpacePos = InStr(lFirstSpacePos + 1, Text1.Text, " ", vbTextCompare)
If lSecondSpacePos = 0 Then
lSecondSpacePos = Len(Text1.Text) + 1
Else
lSecondSpacePos = lSecondSpacePos + 1
End If
Text1.SelStart = lFirstSpacePos - 1
Text1.SelLength = lSecondSpacePos - lFirstSpacePos
Call Text1.SetFocus
End Sub
MysticJim, I feel sure that you have been warned previously in respect of comments vs answers. Please reread the guidelines at the bottom of the page and do not post answers in this way in future. Should you continue to do so your account WILL be referred to customer services for possible penalty.
ghadley, are you still with us?
My original code works with one minor issue--it highlights the character after the space. To fix it, subtract one from the SelStart value:
Text1.SelStart = intSpacePosition - 1
Also, you won't see where the selection is if the textbox does not have the focus, so also add this at the end of the Command1_Click procedure:
Text1.SetFocus
The new code for Command1_Click will be:
Private Sub Command1_Click()
Dim intSpacePosition As Integer
m_intSpaceCounter = m_intSpaceCounter + 1
intSpacePosition = FindSpaceInText(Text1.Text , m_intSpaceCounter)
If intSpacePosition = 0 Then
Text1.SelLength = 0
Else
Text1.SelStart = intSpacePosition - 1
Text1.SelLength = 1
End If
Text1.SetFocus
End Sub
....
MysticJim, since you posted your code as a solution, rather than the preferred comment, please explain why/how it is better than the code that I already offered...
(BTW, you defined the variable counter as a variant!)
My original code works with one minor issue--it highlights the character after the space. To fix it, subtract one from the SelStart value:
Text1.SelStart = intSpacePosition - 1
Also, you won't see where the selection is if the textbox does not have the focus, so also add this at the end of the Command1_Click procedure:
Text1.SetFocus
The new code for Command1_Click will be:
Private Sub Command1_Click()
Dim intSpacePosition As Integer
m_intSpaceCounter = m_intSpaceCounter + 1
intSpacePosition = FindSpaceInText(Text1.Text
If intSpacePosition = 0 Then
Text1.SelLength = 0
Else
Text1.SelStart = intSpacePosition - 1
Text1.SelLength = 1
End If
Text1.SetFocus
End Sub
....
MysticJim, since you posted your code as a solution, rather than the preferred comment, please explain why/how it is better than the code that I already offered...
(BTW, you defined the variable counter as a variant!)
ASKER
I do not need to see highlight, i actually ask for msgbox to show the location of space and string.
Can you try again?
Can you try again?
ASKER
I do not need to see highlight, i actually ask for msgbox to show the location of space and string.
Can you try again?
Can you try again?
change the Command1_Click procedure to:
private sub Command1_Click()
dim intSpacePosition as integer
m_intSpaceCounter=m_intSpa ceCounter+ 1
intSpacePosition = FindSpaceInText(Text1.Text , m_intSpaceCounter)
if intSpacePosition=0 then
msgbox "no more spaces", vbokonly or vbexclamation
else
msgbox "space found at position " & (intSpacePosition - 1), vbokonly or vbinformation
endif
end sub
private sub Command1_Click()
dim intSpacePosition as integer
m_intSpaceCounter=m_intSpa
intSpacePosition = FindSpaceInText(Text1.Text
if intSpacePosition=0 then
msgbox "no more spaces", vbokonly or vbexclamation
else
msgbox "space found at position " & (intSpacePosition - 1), vbokonly or vbinformation
endif
end sub
ghadley, look at the second set of code I posted
ghadley,
This is a list of your open questions. Since you became a member here, you have yet to close a single one. Besides being completely unfair to the Experts who have provided assistance, you are shirking your responsibility as a Member of this site.
https://www.experts-exchange.com/questions/20376959/Web-Server-In-Window-2000.html
https://www.experts-exchange.com/questions/20374459/DNS-problems.html
https://www.experts-exchange.com/questions/20347625/New-Linksys-router-with-Window-2000-Server.html
https://www.experts-exchange.com/questions/20344161/Questions-on-dsn-in-Win-2000-Server.html
https://www.experts-exchange.com/questions/20344125/Questions-on-IIS-in-Win-2000-Server-Client.html
https://www.experts-exchange.com/questions/20356366/MS-Words-With-Sql-Server-7-0.html
https://www.experts-exchange.com/questions/20380917/New-Set-Up-Of-SQL-Server-7-0.html
https://www.experts-exchange.com/questions/20343726/Javascript-Function-Running-difference-on-IE-5-0-and-IE-5-5.html
https://www.experts-exchange.com/questions/20303268/VB-Simple-Question-easy-50-pts.html
https://www.experts-exchange.com/questions/20341129/DNS-IN-IIS-5-0.html
https://www.experts-exchange.com/questions/20314846/ASP-UserName-And-Password.html
https://www.experts-exchange.com/questions/20284374/Sending-txt-html-or-xml-file-to-IIS.html
https://www.experts-exchange.com/questions/20258122/Web-Storage-System-in-Exchange-2000.html
https://www.experts-exchange.com/questions/20381906/Window-2000-Server-Virus.html
Please attend to these questions by closing them appropriately as quickly as possible. Admin has been informed of your misbehavior.
Netminder
CS Moderator
This is a list of your open questions. Since you became a member here, you have yet to close a single one. Besides being completely unfair to the Experts who have provided assistance, you are shirking your responsibility as a Member of this site.
https://www.experts-exchange.com/questions/20376959/Web-Server-In-Window-2000.html
https://www.experts-exchange.com/questions/20374459/DNS-problems.html
https://www.experts-exchange.com/questions/20347625/New-Linksys-router-with-Window-2000-Server.html
https://www.experts-exchange.com/questions/20344161/Questions-on-dsn-in-Win-2000-Server.html
https://www.experts-exchange.com/questions/20344125/Questions-on-IIS-in-Win-2000-Server-Client.html
https://www.experts-exchange.com/questions/20356366/MS-Words-With-Sql-Server-7-0.html
https://www.experts-exchange.com/questions/20380917/New-Set-Up-Of-SQL-Server-7-0.html
https://www.experts-exchange.com/questions/20343726/Javascript-Function-Running-difference-on-IE-5-0-and-IE-5-5.html
https://www.experts-exchange.com/questions/20303268/VB-Simple-Question-easy-50-pts.html
https://www.experts-exchange.com/questions/20341129/DNS-IN-IIS-5-0.html
https://www.experts-exchange.com/questions/20314846/ASP-UserName-And-Password.html
https://www.experts-exchange.com/questions/20284374/Sending-txt-html-or-xml-file-to-IIS.html
https://www.experts-exchange.com/questions/20258122/Web-Storage-System-in-Exchange-2000.html
https://www.experts-exchange.com/questions/20381906/Window-2000-Server-Virus.html
Please attend to these questions by closing them appropriately as quickly as possible. Admin has been informed of your misbehavior.
Netminder
CS Moderator
ghadley,
I have made it clear my intentions. Recommend you post in your open questions or your account will be suspended. The initial request was for 72 hours but seeing you have subscribed to E-E since then, consider this your time limit.
Computer101
E-E Admin
I have made it clear my intentions. Recommend you post in your open questions or your account will be suspended. The initial request was for 72 hours but seeing you have subscribed to E-E since then, consider this your time limit.
Computer101
E-E Admin
Question abandoned; force-accepted and Admin notified.
Crash2100: points for you at https://www.experts-exchange.com/questions/20389226/For-Crash2100-re-20303268.html
Netminder
CS Moderator
Crash2100: points for you at https://www.experts-exchange.com/questions/20389226/For-Crash2100-re-20303268.html
Netminder
CS Moderator
Dim counter As Integer
Dim iLocation As Integer
Private Sub Command1_Click()
iLocation = Len(strings(counter)) + 1
MsgBox "String: " & strings(counter) & vbCrLf & "Location: " & iLocation
If counter = UBound(strings) Then
counter = 0
Else
counter = counter + 1
End If
End Sub
Private Sub Form_Load()
strings = Split(Text1.Text, " ")
End Sub