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.
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.
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")There is a problem:
Dim
x As Stringx =
Me.RichTextBox1.Find(txtFind.Text)Me.RichTextBox1.SelectedText = xMe.RichTextBox1.SelectionLength = txtFind.Text.LengthMe.RichTextBox1.SelectionBackColor = Color.YellowGreenIt finds the word I was searching but then it changes the word into some number and doesnt continue searching and highlighting...
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
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.
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.
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".
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
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)...
' 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.ClickMy.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 hiliteFormInherits 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() callEnd Sub'Form overrides dispose to clean up the component list.Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)If disposing ThenIf Not (components Is Nothing) Thencomponents.Dispose()
End IfEnd IfMyBase.Dispose(disposing)End SubFriend WithEvents bttnhilite As System.Windows.Forms.ButtonFriend WithEvents bttnClear As System.Windows.Forms.ButtonFriend WithEvents txtSearchWord As System.Windows.Forms.TextBoxFriend WithEvents chkCase As System.Windows.Forms.CheckBoxFriend WithEvents chkWord As System.Windows.Forms.CheckBox'Required by the Windows Form DesignerPrivate 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.ButtonMe.chkWord = New System.Windows.Forms.CheckBoxMe.bttnhilite = New System.Windows.Forms.ButtonMe.txtSearchWord = New System.Windows.Forms.TextBoxMe.chkCase = New System.Windows.Forms.CheckBoxMe.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 = 4Me.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 = 7Me.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 = 2Me.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 = 6Me.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.FixedDialogMe.MaximizeBox = FalseMe.Name = "hiliteForm"Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreenMe.Text = "HighLight Words"Me.TopMost = TrueMe.ResumeLayout(False)Me.PerformLayout()End Sub#
End RegionPrivate Sub txtSearchWord_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchWord.TextChangedIf txtSearchWord.Text.Length > 0 Thenbttnhilite.Enabled =
TruebttnClear.Enabled =
TrueElsebttnhilite.Enabled =
FalsebttnClear.Enabled =
FalseEnd IfEnd SubPublic wordAt As IntegerPrivate Sub bttn_HiLite_or_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnhilite.Click, bttnClear.ClickDim count As Integer = 0Dim srchMode As RichTextBoxFindssrchMode = SetSearchMode()
wordAt = 0
Do While wordAt <> -1wordAt = Form1.RichTextBox1.Find(txtSearchWord.Text, wordAt, srchMode)
If wordAt = -1 ThenForm1.RichTextBox1.ScrollToCaret()
MsgBox(count.ToString &
" Matches were changed", , "HighLighter")Exit DoElseForm1.RichTextBox1.Select(wordAt, txtSearchWord.Text.Length)
If sender Is bttnhilite ThenForm1.RichTextBox1.SelectionBackColor = Color.Yellow
ElseForm1.RichTextBox1.SelectionBackColor = Form1.RichTextBox1.BackColor
End IfwordAt += 1
count += 1
End IfLoopEnd SubFunction SetSearchMode() As RichTextBoxFindsDim mode As RichTextBoxFindsIf chkCase.Checked = True Thenmode = RichTextBoxFinds.MatchCase
Elsemode = RichTextBoxFinds.None
End IfIf chkWord.Checked = True Thenmode = mode
Or RichTextBoxFinds.WholeWordElsemode = mode
Or RichTextBoxFinds.NoneEnd IfSetSearchMode = mode
End FunctionPrivate Sub hiliteForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadIf txtSearchWord.Text.Length > 0 Thenbttnhilite.Enabled =
TruebttnClear.Enabled =
TrueElsebttnhilite.Enabled =
FalsebttnClear.Enabled =
FalseEnd IfEnd SubEnd
Class
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 = 0Dim wordat As IntegerDo While wordat <> -1wordat =
Me.RichTextBox1.Find(TextBox1.Text, wordat, RichTextBoxFinds.WholeWord)If wordat = -1 ThenMe.RichTextBox1.ScrollToCaret()Exit DoElseMe.RichTextBox1.Select(wordat, TextBox1.Text.Length)If sender Is Button1 ThenMe.RichTextBox1.SelectionBackColor = Color.YellowGreenElseMe.RichTextBox1.SelectionBackColor = Me.RichTextBox1.BackColorEnd Ifwordat += 1
count += 1
End IfLoop