Change font color in richtextbox only for selected string

Posted on 2007-10-17
Last Modified: 2008-02-07
I have a richtextbox on a form with a button that brings up a colordialog.  Currently if I use this method when I choose a color it changes everything in the richtextbox to that particular color.
here is what I would like ...
I would like to be able to highlight or select text and change the color only for the selected string.

Thanks in advance.
Question by:H-SC
    LVL 96

    Expert Comment

    by:Bob Learned
    Try this:

            Me.RichTextBox1.SelectionStart = 50
            Me.RichTextBox1.SelectionLength = 10
            Me.RichTextBox1.SelectionColor = Color.Red

    LVL 1

    Author Comment


    Do I put this code on the button click?
    LVL 85

    Accepted Solution

    Right...something like:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                RichTextBox1.SelectionColor = ColorDialog1.Color
            End If
        End Sub
    LVL 1

    Author Comment

    Idle Mind,

    That worked great !

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now