Solved

VB Simple Question (easy 50 pts)

Posted on 2002-05-21
13
229 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
  • 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now