Well, let's suppose I have a textbox called 'txtData', so how do I highlight specified words? Please note I don't want to highlight any word of any known language like VB or C++ or whatever else, but words known by me.
Printable View
Well, let's suppose I have a textbox called 'txtData', so how do I highlight specified words? Please note I don't want to highlight any word of any known language like VB or C++ or whatever else, but words known by me.
You can't do it in a TextBox for starters. You can change certain formatting in a TextBox but it must be applied to the entire text. If you want variable formatting then you need to use a RichTextBox. There's a submission in the VB.NET CodeBank forum on this topic. I haven't read it myself but you probably should.
http://www.vbforums.com/showthread.php?t=517116
Code:Dim srch4 As String = "how"
TextBox1.Text = "Hello, how are you?"
Dim id As Integer = TextBox1.Text.IndexOf(srch4)
If id <> -1 Then
TextBox1.Select() 'set focus to control
TextBox1.Select(id, srch.Length)
End If
RichTextBox2.Text = RichTextBox2.Text.Insert(0, "ABhowCDEFGHIJKLMNOPQRSTUVWXYZ") 'insert text at beginning
id = RichTextBox2.Text.IndexOf(srch4)
If id <> -1 Then
RichTextBox2.Select() '<<<<<<<<<<<<<<<<<<<<< make sure the control has focus
RichTextBox2.Select(id, srch4.Length) 'start and end according to index of text
RichTextBox2.SelectionColor = Color.Blue
Dim FontStyle1 As System.Drawing.FontStyle
FontStyle1 = CType((FontStyle.Underline + FontStyle.Bold), FontStyle)
RichTextBox2.SelectionFont = New Font(RichTextBox2.SelectionFont.FontFamily, RichTextBox2.SelectionFont.Size, FontStyle1)
End If
I have done this before and I will warn if there is quite a bit of data in the Rich text box then you are going to have poor performance when the app is trying to highlight words. Just an FYI.