Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Simple Question (easy 50 pts)

Posted on 2002-05-21
13
Medium Priority
?
239 Views
Last Modified: 2010-05-02
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
Comment
Question by:ghadley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
13 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 200 total points
ID: 7025757
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
 
LVL 18

Expert Comment

by:Crash2100
ID: 7025767
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
 
LVL 18

Expert Comment

by:Crash2100
ID: 7025783
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:MysticJim
ID: 7026856
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 7027100
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 7027304
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
 

Author Comment

by:ghadley
ID: 7028161
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
 

Author Comment

by:ghadley
ID: 7028202
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 7028303
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
 
LVL 18

Expert Comment

by:Crash2100
ID: 7028362
ghadley, look at the second set of code I posted
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7375407
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7394892
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
 
LVL 5

Expert Comment

by:Netminder
ID: 7403046
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question