|
-
Aug 11th, 2007, 05:43 PM
#1
Thread Starter
Hyperactive Member
Highlighting text
Im using command to find a string in a rich text box:
Code:
RichTextBox1.Find(TextBox1.Text)
How do I highlight the text after it has been found?
Hello, Im a complete beginner so I if I ask obvious questions don't laugh. I have had no special training and I thought I'd learn and I thought this is the best place to start.
I started off using VB 6.0 but when I bought a new computer I decided to upgrade to VB 2005, so its a little bit different to what i'm used to
-
Aug 11th, 2007, 05:52 PM
#2
Re: Highlighting text
vb.net Code:
Me.RichTextBox1.SelectionBackColor = Color.Blue
-
Aug 11th, 2007, 05:56 PM
#3
Thread Starter
Hyperactive Member
Re: Highlighting text
how would I remove the highlightin when the text is changed
Hello, Im a complete beginner so I if I ask obvious questions don't laugh. I have had no special training and I thought I'd learn and I thought this is the best place to start.
I started off using VB 6.0 but when I bought a new computer I decided to upgrade to VB 2005, so its a little bit different to what i'm used to
-
Aug 11th, 2007, 06:01 PM
#4
Re: Highlighting text
This would highlight the found text
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.Focus()
End Sub
You also can change its backcolor
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.SelectionBackColor = Color.Yellow
End Sub
and the forcolor
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
-
Aug 11th, 2007, 07:12 PM
#5
Re: Highlighting text
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
Last edited by Bulldog; Aug 11th, 2007 at 07:16 PM.
-
Aug 12th, 2007, 03:06 AM
#6
Re: Highlighting text
why are people giving such lengthy codes for this?
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 , then
Code:
RichTextBox1.Find(TextBox1.Text)
will highlight the text it finds
you can set HideSelection through code also like this...
Code:
RichTextBox1.HideSelection = False
RichTextBox1.Find(TextBox1.Text)
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 12th, 2007, 06:23 AM
#7
Thread Starter
Hyperactive Member
Re: Highlighting text
Thanks! is there a way so that when the user clicks the button again it will highlight the next one found
Hello, Im a complete beginner so I if I ask obvious questions don't laugh. I have had no special training and I thought I'd learn and I thought this is the best place to start.
I started off using VB 6.0 but when I bought a new computer I decided to upgrade to VB 2005, so its a little bit different to what i'm used to
-
Aug 12th, 2007, 08:14 AM
#8
Re: Highlighting text
yes buddy, you can expand on the Find function
Code:
'/// find first instance ( only matching whole words )
RichTextBox1.Find(TextBox1.Text, RichTextBoxFinds.WholeWord)
then find next item ...
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)
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 12th, 2007, 09:50 AM
#9
Thread Starter
Hyperactive Member
Hello, Im a complete beginner so I if I ask obvious questions don't laugh. I have had no special training and I thought I'd learn and I thought this is the best place to start.
I started off using VB 6.0 but when I bought a new computer I decided to upgrade to VB 2005, so its a little bit different to what i'm used to
-
Aug 12th, 2007, 10:06 AM
#10
Re: Highlighting text
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?
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 12th, 2007, 10:12 AM
#11
Re: Highlighting text
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
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Aug 12th, 2007, 11:35 AM
#12
Re: Highlighting text
 Originally Posted by dynamic_sysop
why are people giving such lengthy codes for this?
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 , then
Code:
RichTextBox1.Find(TextBox1.Text)
will highlight the text it finds
you can set HideSelection through code also like this...
Code:
RichTextBox1.HideSelection = False
RichTextBox1.Find(TextBox1.Text)

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?
I think it would be better to set the HideSelection property in design time so that it won’t blink at run time.
Last edited by VBDT; Aug 12th, 2007 at 11:47 AM.
-
Aug 12th, 2007, 01:47 PM
#13
Re: Highlighting text
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 post
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
the 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.
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.
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
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
|