Solved

VB Simple Question (easy 50 pts)

Posted on 2002-05-21
13
234 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 50 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

696 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