Results 1 to 10 of 10

Thread: Select A String In A TextBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Select A String In A TextBox

    How do I select/highlight a string in a TextBox control?
    I can detect if text is there but I can't select the specified text.

    Code:
     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            If TextBox1.Text.Contains(TextBox2.Text) Then
    
                MsgBox("Found The Text")
    
            Else
    
                MsgBox("Text Not Found")
    
            End If
        End Sub

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Select A String In A TextBox

    Use the .SelectionStart and .SelectionLength methods.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Select A String In A TextBox

    How!!?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Select A String In A TextBox

    The same way you would use any other method or property:
    http://msdn.microsoft.com/en-us/libr...th(VS.85).aspx

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Select A String In A TextBox

    I'm sorry, but I don't get it!
    It's not working for me!!!

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Select A String In A TextBox

    Set your selection start property to the location of the text you find and set your selection length property to the length of the string.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Select A String In A TextBox

    Code:
    Public Property SelectionLength() As Integer
            Get
                Dim instance As TextBox
                Dim value As Integer
    
            End Get
            Set(ByVal value As Integer)
                value = instance.SelectionLength
    
                instance.SelectionLength = value
            End Set
        End Property
    X. Name 'Instance' is not declared.

  8. #8
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: Select A String In A TextBox

    remember to give the textbox focus

    vb Code:
    1. If TextBox1.Text.Contains(TextBox2.Text) Then
    2.             TextBox1.Focus()
    3.             TextBox1.Select(TextBox1.Text.IndexOf(TextBox2.Text), TextBox2.Text.Length)
    4.         Else
    5.             MsgBox("Text Not Found")
    6.         End If

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Select A String In A TextBox

    Thank you! I now have it working! Just one more question, if there is more than one instance of the string in a textbox, how would I do a "find next"?

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Select A String In A TextBox

    Are you trying to create a Find function like the one found in Notepad? If so you would be better of using a RichTextBox and it's 'Find' function. This will search for the text you specify, starting at the position you specify, and automatically select it for you. If you need to search for the next instance, simply call the Find function again, but this time set the starting position to the position of the previous 'hit', + 1... If you get what I mean.

    Eg, if you found your string at position 482 (the Find function will return this number for you), you call the Find function again but starting from position 483 (this way, you don't find the same instance again).

    If the string is not found in the specified range, the Find function returns -1 allowing you to handle that properly (messagebox etc..)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width