Results 1 to 5 of 5

Thread: Search a string in a textbox

  1. #1
    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?

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Search a string in a textbox

    Use a For Each loop:

    Code:
    For Each searchmot in TextBox5. Text. ToLower 
     ... 
    Next

  3. #3
    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?

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    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

  5. #5
    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.

Similar Threads

  1. Replies: 2
    Last Post: 20-08-2010, 01:23 AM
  2. Search character in a string
    By Zool in forum Software Development
    Replies: 3
    Last Post: 14-12-2009, 02:45 PM
  3. Code to search for a string
    By Allan.d in forum Software Development
    Replies: 3
    Last Post: 03-12-2009, 12:55 PM
  4. Function search in a string
    By Amandev in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 02:19 PM
  5. Search the presence of n words in a string
    By cobra2008 in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 09:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,906,146.75958 seconds with 16 queries