Results 1 to 4 of 4

Thread: [2008] Highlighting syntax

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    21

    [2008] Highlighting syntax

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Highlighting syntax

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] Highlighting syntax

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Hyperactive Member .NetNinja's Avatar
    Join Date
    Oct 2008
    Location
    USA
    Posts
    281

    Re: [2008] Highlighting syntax

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width