Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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.

0
ghadley
Asked:
ghadley
  • 3
  • 3
  • 2
  • +4
1 Solution
 
rspahitzCommented:
Here's some logic:

* set a module-level counter to keep track of the iteration number
* on button click:
** copy the string to a temp variable (for speed and readability mainly)
** search for the nth (counter-th) space in the string) and save the position
** if the position > 0 (i.e. space found) highlight the corresponding entry in the related textbox

--
Private m_intSpaceCounter as integer

...
private sub form_Load()
'...
   m_intSpaceCounter=0
'...
end sub

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
    Text1.SelLength = 1
  endif
end sub

private function FindSpaceInText(SearchString as string, ItemNumber as integer) as integer
  dim iPosition as integer
  dim iIteration as integer

  iPosition=0
  iIteration =0
  do
    iPosition =instr(iPosition +1,SearchString, " ")
    if iPosition=0 then
      exit do
    end if
    iIteration =iIteration +1
    if iIteration >=ItemNumber then
       exit do
    endif
  loop

  FindSpaceInText = iPosition
end function

'---
Note that the above code is untested, but you only wanted the logic anyway.
 
0
 
Crash2100Commented:
Dim strings() As String
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
0
 
Crash2100Commented:
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
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
MysticJimCommented:
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
0
 
TimCotteeHead of Software ServicesCommented:
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.
0
 
rspahitzCommented:
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!)
0
 
ghadleyAuthor Commented:
I do not need to see highlight, i actually ask for msgbox to show the location of space and string.

Can you try again?
0
 
ghadleyAuthor Commented:
I do not need to see highlight, i actually ask for msgbox to show the location of space and string.

Can you try again?
0
 
rspahitzCommented:
change the Command1_Click procedure to:

private sub Command1_Click()
 dim intSpacePosition as integer

 m_intSpaceCounter=m_intSpaceCounter+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
0
 
Crash2100Commented:
ghadley, look at the second set of code I posted
0
 
Computer101Commented:
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
0
 
NetminderCommented:
Question abandoned; force-accepted and Admin notified.

Crash2100: points for you at http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20389226.html

Netminder
CS Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now