Im using command to find a string in a rich text box:
How do I highlight the text after it has been found?Code:RichTextBox1.Find(TextBox1.Text)
Printable View
Im using command to find a string in a rich text box:
How do I highlight the text after it has been found?Code:RichTextBox1.Find(TextBox1.Text)
vb.net Code:
Me.RichTextBox1.SelectionBackColor = Color.Blue
how would I remove the highlightin when the text is changed
This would highlight the found text
You also can change its backcolorvb Code:
Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click Dim index As Integer = Me.RichTextBox1.Text.IndexOf(Me.FindTextBox.Text) Me.RichTextBox1.SelectionStart = index Me.RichTextBox1.SelectionLength = Me.FindTextBox.Text.Length Me.RichTextBox1.Focus() End Sub
and the forcolorvb Code:
Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click Dim index As Integer = Me.RichTextBox1.Text.IndexOf(Me.FindTextBox.Text) Me.RichTextBox1.SelectionStart = index Me.RichTextBox1.SelectionLength = Me.FindTextBox.Text.Length Me.RichTextBox1.SelectionBackColor = Color.Yellow End Sub
vb Code:
Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click Dim index As Integer = Me.RichTextBox1.Text.IndexOf(Me.FindTextBox.Text) Me.RichTextBox1.SelectionStart = index Me.RichTextBox1.SelectionLength = Me.FindTextBox.Text.Length Me.RichTextBox1.SelectionColor = Color.Red End Sub
To highlight/unhighlight, just use the same code as VBDT posted;
Code:'Highlight selected text
Private Sub HighlightSelection_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles HighlightSelection.Click
RichTextBox1.SelectionColor = Color.Blue
End Sub
'UnHighlight selected text
Private Sub UnHighlightSelection_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles UnHighlightSelection.Click
RichTextBox1.SelectionColor = Me.ForeColor
End Sub
'Clear all highlighted text
Private Sub UnHighlight_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles UnHighlight.Click
RichTextBox1.SelectAll()
RichTextBox1.SelectionColor = Me.ForeColor
RichTextBox1.DeselectAll()
End Sub
why are people giving such lengthy codes for this? :confused:
the RichTextBox has a setting in the properties window called HideSelection it's default is True ( so as not to highlight )
Just set that to False , thenwill highlight the text it finds :)Code:RichTextBox1.Find(TextBox1.Text)
you can set HideSelection through code also like this...
:)Code:RichTextBox1.HideSelection = False
RichTextBox1.Find(TextBox1.Text)
Thanks! is there a way so that when the user clicks the button again it will highlight the next one found
yes buddy, you can expand on the Find function
then find next item ...Code:'/// find first instance ( only matching whole words )
RichTextBox1.Find(TextBox1.Text, RichTextBoxFinds.WholeWord)
:)Code:'/// specify the index to start searching from ( anything above the start of the current selection will do even RichTextBox1.SelectionStart + 1 )
RichTextBox1.Find(TextBox1.Text, RichTextBox1.SelectionStart + RichTextBox1.SelectionLength, RichTextBoxFinds.WholeWord)
Nothings happening
are you clicking a different button than the one to select the 1st word? you aint clicking the same button?
also have you tried RichTextBox1.Find(TextBox1.Text, " position to search from " ) without the last parameter?
i put 2 buttons on a form, one to search for the 1st instance of the word, the 2nd button to find subsequent instances of the same word. it works fine here. this is all i did ...
Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RichTextBox1.HideSelection = False
RichTextBox1.Find(TextBox1.Text, RichTextBoxFinds.WholeWord)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'/// with RichTextBox1.HideSelection set to False
'/// this searches for subsequent instances of the same word & highlights them.
RichTextBox1.Find(TextBox1.Text, RichTextBox1.SelectionStart + RichTextBox1.SelectionLength, RichTextBoxFinds.WholeWord)
End Sub
I tested your code and it highlights the word but the RichTextBox box blinks when I click on the button. Do you think that is normal? So what is the advantage of your code?Quote:
Originally Posted by dynamic_sysop
I think it would be better to set the HideSelection property in design time so that it won’t blink at run time.
yes it's very true that it's best to set HideSelection @ design time.
I was only showing the function is there by running it under a button click :) that's why i said to do so in my 1st postthe advantage of the Find method & the HideSelection property is that that's what they are there for. should be fine with the HideSelection set at design in the properties window.Quote:
RichTextBox has a setting in the properties window called HideSelection it's default is True ( so as not to highlight )
Just set that to False , then
i spent a few years making chatroom bots for msn chat ( until they closed it ) through VB5 to VB6, then on to the .NET / C#, this gave me a great insight into the Richtextbox, due to the amount of work i had to do with them.
Regards.