|
-
Aug 1st, 2008, 12:53 PM
#1
Thread Starter
Addicted Member
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
-
Aug 1st, 2008, 12:55 PM
#2
Re: Select A String In A TextBox
Use the .SelectionStart and .SelectionLength methods.
-
Aug 1st, 2008, 01:00 PM
#3
Thread Starter
Addicted Member
Re: Select A String In A TextBox
-
Aug 1st, 2008, 01:03 PM
#4
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
-
Aug 1st, 2008, 01:10 PM
#5
Thread Starter
Addicted Member
Re: Select A String In A TextBox
I'm sorry, but I don't get it!
It's not working for me!!!
-
Aug 1st, 2008, 01:13 PM
#6
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.
-
Aug 1st, 2008, 01:24 PM
#7
Thread Starter
Addicted Member
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.
-
Aug 1st, 2008, 01:27 PM
#8
Hyperactive Member
Re: Select A String In A TextBox
remember to give the textbox focus 
vb Code:
If TextBox1.Text.Contains(TextBox2.Text) Then
TextBox1.Focus()
TextBox1.Select(TextBox1.Text.IndexOf(TextBox2.Text), TextBox2.Text.Length)
Else
MsgBox("Text Not Found")
End If
-
Aug 2nd, 2008, 10:48 AM
#9
Thread Starter
Addicted Member
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"?
-
Aug 2nd, 2008, 03:31 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|