Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 07-05-2009
Member
 
Join Date: Mar 2009
Posts: 1,360
Search a string in a textbox

I developed a program that opens a text file containing customers. I added the program that has a button which allows to compare the string you search for my textbox which contains the text file

Here is my code that works:

Code:
Private Sub Button1_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1. Click 

          Dim searchmot As String 
          Dim PosMot As Integer 

          searchmot = TextBox1. Text
          PosMot = InStr (TextBox5.Text.ToLower, TextBox1.Text.ToLower)

          If PosMot <> 0 Then 

              'PosMot from 1 to Len (Text1.Text), while SelStart ranges from 0 to Len (Text1.Text) -1 
              'We must therefore remove 1 

              TextBox5. SelectionStart = PosMot - 1 'set cursor position 
              TextBox5. SelectionLength = Len (searchmot) 'defined length highlight 
              TextBox5. Focus () "gives the focus to the box text1 

          Else 

              MsgBox ( "Unknown Client") 

          End If 
      End Sub
The problem is that if I take a string and then add a button that allows you to see if it exists.

I search my program highlights the first but impossible to know if my list contains another.

Can anyone please help me?
Reply With Quote
  #2  
Old 07-05-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Search a string in a textbox

Use a For Each loop:

Code:
For Each searchmot in TextBox5. Text. ToLower 
 ... 
Next
Reply With Quote
  #3  
Old 07-05-2009
Member
 
Join Date: Mar 2009
Posts: 1,360
Re: Search a string in a textbox

Could you explain me in detail in my case hot to use "for each" loop and give me a little more information on the same?
Reply With Quote
  #4  
Old 07-05-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Search a string in a textbox

The for each loop may not be ideal for strings which are a series of characters ...

But you can do something like:

Code:
  Private Sub Button1_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1. Click 
          Dim TextBoxWitness As New TextBox
          TextBoxWitness. Text = TextBox5. Text 
          Dim searchmot As String 
          Dim PosMot As Integer 
          Dim numIndex = 0
          Dim counter = 0 

          searchmot = TextBox1. Text
          PosMot = InStr (TextBoxWitness.Text.ToLower, TextBox1.Text.ToLower)

          For Each character In TextBoxWitness.Text 

              If PosMot <> 0 Then 
                  counter = 1 
                  'PosMot from 1 to Len (Text1.Text), while SelStart ranges from 0 to Len (Text1.Text) -1 
                  'We must therefore remove 1 

                  numIndex = TextBoxWitness.Text.indexOf (searchmot) 
                  TextBoxWitness. Text = TextBoxWitness.Text.Remove (numIndex, searchmot.Length) 
                  PosMot = InStr (TextBoxWitness.Text.ToLower, searchmot.ToLower) 

                  TextBox5.SelectionStart = PosMot 'set cursor position 
                  TextBox5.SelectionLength = Len (searchmot) 'defined length highlight 
                  TextBox5.Focus () "gives the focus to the box text1 

              Else 

                  If counter = 0 Then 
                      MsgBox ( "Unknown Client") 
                      Exit For 
                  End If 
              End If 
          Next 

      End Sub
Reply With Quote
  #5  
Old 07-05-2009
Member
 
Join Date: May 2008
Posts: 945
Re: Search a string in a textbox

The easiest way would be to build the list of possible index for the word (you know the length of the word, only the index is sufficient). Something like

Code:
Dim indexes As List (Of Integer) = New List (Of Integer) () 
  Dim string As String = "This is TechArena Website" 
  Dim search As String = "TechArena" 
  Dim pos As Integer = - 1 
  Do 
      pos = string. IndexOf (search, pos + 1) 
      If pos <> - 1 Then 
          indexes. Add (pos) 
      End If 
  End While  
  While pos <> - 1
Once you have your list index is simple. When you click on after you take the next index of the list and you changed the SelectionStart based, the length does not change. You can easily make a back button too.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Search a string in a textbox"
Thread Thread Starter Forum Replies Last Post
Excel/VBA: Copying text from UserForm Textbox to a Worksheet Textbox Neil Ives Software Development 2 20-08-2010 01:23 AM
Search character in a string Zool Software Development 3 14-12-2009 02:45 PM
Code to search for a string Allan.d Software Development 3 03-12-2009 12:55 PM
Function search in a string Amandev Software Development 3 24-11-2009 02:19 PM
Search the presence of n words in a string cobra2008 Software Development 3 09-10-2009 09:50 PM


All times are GMT +5.5. The time now is 01:32 PM.