Highlighting text

How can I find the text within text box and highlight it?

For example:

I write "text highlight" in text box and in other box I write "high" for searching in first box.

Thanks.

[220 byte] By [BlackSeph] at [2007-12-23]
# 1

For searching and text highlighting you may want to consider the RichTextBox which has build in methods for performing those task!

Me.RichTextBox1.Find("high")

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

There is a problem:

Dim x As String

x = Me.RichTextBox1.Find(txtFind.Text)

Me.RichTextBox1.SelectedText = x

Me.RichTextBox1.SelectionLength = txtFind.Text.Length

Me.RichTextBox1.SelectionBackColor = Color.YellowGreen

It finds the word I was searching but then it changes the word into some number and doesnt continue searching and highlighting...

BlackSeph at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

The following is what you need

Dim x As Integer
x = Me.RichTextBox1.Find(TextBox1.Text)
Me.RichTextBox1.SelectionStart = x
Me.RichTextBox1.SelectionLength = TextBox1.Text.Length
Me.RichTextBox1.SelectionBackColor = Color.YellowGreen

spotty at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

Thanks for correction but now it starts highlighting in correct location and it continues to highlight to the end of text, I just need highlight of searched word trought whole text.

BlackSeph at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

Are you sure your code is correct as this should only highlight the specific word, which wis what I am seeing here.

This code is with the assumption that Textbox1.text contains the word being searched for. and Richtextbox1 contains the text to be search in.

Example - Enter "When shall we three meet again" in richtextbox1 and enter "we" in textbox1.

Put the previous code in a button click event.

When you click the button only we is highlighted.

spotty at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

The word repeats over and over.

For Example:

When shall we three meet again? When we find some time.

It highlights only the first "we".

BlackSeph at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

Time to experiment and look for the next occurance of your key word. Loops will work for that. What have you tried so far, to find the next occurance of the word(s) you are looking for?

james

aka:Trucker

Trucker at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8

I have been trying with do...loop for 3 hours and I can't get a result.

It should be like this:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=503223&SiteId=1

but I cant search entered text with that code (only the one that loads with form)...

BlackSeph at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9

' This goes on the main form1:

(If your form name is not 'Form1' and your richtextbox name is not 'Richtextbox1',

change the references in the code below.)

-

' Form1 calls the hilight form

Private Sub HighLightWordsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HighLightWordsToolStripMenuItem.Click

My.Forms.hiliteForm.ShowDialog()

End Sub

--

1. Then add a new form and name it hiliteForm.vb

2. Delete all text in hiliteForm's InitializeComponent area

3. Delete all text in hiliteForm.vb

4. Paste the following into hiliteForm.vb

Public Class hiliteForm

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

Friend WithEvents bttnhilite As System.Windows.Forms.Button

Friend WithEvents bttnClear As System.Windows.Forms.Button

Friend WithEvents txtSearchWord As System.Windows.Forms.TextBox

Friend WithEvents chkCase As System.Windows.Forms.CheckBox

Friend WithEvents chkWord As System.Windows.Forms.CheckBox

'Required by the Windows Form Designer

Private components As System.ComponentModel.Container

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.bttnClear = New System.Windows.Forms.Button

Me.chkWord = New System.Windows.Forms.CheckBox

Me.bttnhilite = New System.Windows.Forms.Button

Me.txtSearchWord = New System.Windows.Forms.TextBox

Me.chkCase = New System.Windows.Forms.CheckBox

Me.SuspendLayout()

'

'bttnClear

'

Me.bttnClear.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.bttnClear.Location = New System.Drawing.Point(224, 40)

Me.bttnClear.Name = "bttnClear"

Me.bttnClear.Size = New System.Drawing.Size(96, 23)

Me.bttnClear.TabIndex = 4

Me.bttnClear.Text = "Clear all"

'

'chkWord

'

Me.chkWord.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.chkWord.Location = New System.Drawing.Point(8, 96)

Me.chkWord.Name = "chkWord"

Me.chkWord.Size = New System.Drawing.Size(152, 24)

Me.chkWord.TabIndex = 7

Me.chkWord.Text = "Whole Word Only"

'

'bttnhilite

'

Me.bttnhilite.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.bttnhilite.Location = New System.Drawing.Point(224, 8)

Me.bttnhilite.Name = "bttnhilite"

Me.bttnhilite.Size = New System.Drawing.Size(96, 23)

Me.bttnhilite.TabIndex = 2

Me.bttnhilite.Text = "HighLight All"

'

'txtSearchWord

'

Me.txtSearchWord.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.txtSearchWord.Location = New System.Drawing.Point(8, 8)

Me.txtSearchWord.Name = "txtSearchWord"

Me.txtSearchWord.Size = New System.Drawing.Size(200, 22)

Me.txtSearchWord.TabIndex = 0

'

'chkCase

'

Me.chkCase.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.chkCase.Location = New System.Drawing.Point(8, 72)

Me.chkCase.Name = "chkCase"

Me.chkCase.Size = New System.Drawing.Size(152, 24)

Me.chkCase.TabIndex = 6

Me.chkCase.Text = "Case Sensitive"

'

'hiliteForm

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(440, 125)

Me.Controls.Add(Me.chkWord)

Me.Controls.Add(Me.chkCase)

Me.Controls.Add(Me.bttnClear)

Me.Controls.Add(Me.bttnhilite)

Me.Controls.Add(Me.txtSearchWord)

Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog

Me.MaximizeBox = False

Me.Name = "hiliteForm"

Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

Me.Text = "HighLight Words"

Me.TopMost = True

Me.ResumeLayout(False)

Me.PerformLayout()

End Sub

#End Region

Private Sub txtSearchWord_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchWord.TextChanged

If txtSearchWord.Text.Length > 0 Then

bttnhilite.Enabled = True

bttnClear.Enabled = True

Else

bttnhilite.Enabled = False

bttnClear.Enabled = False

End If

End Sub

Public wordAt As Integer

Private Sub bttn_HiLite_or_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnhilite.Click, bttnClear.Click

Dim count As Integer = 0

Dim srchMode As RichTextBoxFinds

srchMode = SetSearchMode()

wordAt = 0

Do While wordAt <> -1

wordAt = Form1.RichTextBox1.Find(txtSearchWord.Text, wordAt, srchMode)

If wordAt = -1 Then

Form1.RichTextBox1.ScrollToCaret()

MsgBox(count.ToString & " Matches were changed", , "HighLighter")

Exit Do

Else

Form1.RichTextBox1.Select(wordAt, txtSearchWord.Text.Length)

If sender Is bttnhilite Then

Form1.RichTextBox1.SelectionBackColor = Color.Yellow

Else

Form1.RichTextBox1.SelectionBackColor = Form1.RichTextBox1.BackColor

End If

wordAt += 1

count += 1

End If

Loop

End Sub

Function SetSearchMode() As RichTextBoxFinds

Dim mode As RichTextBoxFinds

If chkCase.Checked = True Then

mode = RichTextBoxFinds.MatchCase

Else

mode = RichTextBoxFinds.None

End If

If chkWord.Checked = True Then

mode = mode Or RichTextBoxFinds.WholeWord

Else

mode = mode Or RichTextBoxFinds.None

End If

SetSearchMode = mode

End Function

Private Sub hiliteForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If txtSearchWord.Text.Length > 0 Then

bttnhilite.Enabled = True

bttnClear.Enabled = True

Else

bttnhilite.Enabled = False

bttnClear.Enabled = False

End If

End Sub

End Class

TallDude at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10

It's working, thanks a lot!

Edit:

I have cut some parts of code I wont use and I got this (code for highlighting on same form with one RTB, textbox and button)

Dim count As Integer = 0

Dim wordat As Integer

Do While wordat <> -1

wordat = Me.RichTextBox1.Find(TextBox1.Text, wordat, RichTextBoxFinds.WholeWord)

If wordat = -1 Then

Me.RichTextBox1.ScrollToCaret()

Exit Do

Else

Me.RichTextBox1.Select(wordat, TextBox1.Text.Length)

If sender Is Button1 Then

Me.RichTextBox1.SelectionBackColor = Color.YellowGreen

Else

Me.RichTextBox1.SelectionBackColor = Me.RichTextBox1.BackColor

End If

wordat += 1

count += 1

End If

Loop

BlackSeph at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 11

That is correct and intentional that it will only highlight the first occurence.

spotty at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...