[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

Change color of text within a label or radio button

I am trying to color a portion of the text within a radio button or a label in C# (Visual Studio 2005 .Net 2.0).  Is there a way to do this?

An example would be in the following bit of text if placed within a label: "This is some text, RED TEXT HERE back to black text here".  Is there a way to pass color values to be parsed by VS inside the text property of a radio button or a label?

Thanks in advance!
Nathan
0
ironwill96
Asked:
ironwill96
1 Solution
 
Bob LearnedCommented:
No, that is not natively possible.  You could probably find something or come up with something like a custom control.

Bob
0
 
DarkoLordCommented:
You can create a custom class that derives from OptionButton (or Label) and override the OnPaint event:

    public class ColorRadioButton : RadioButton
    {
        protected override void OnPaint(PaintEventArgs pevent)
        {
            // creates the control as usual
            base.OnPaint(pevent);
            // clears the previous text so it doesnt get overwritten (example only)
            base.Text = "             ";
            // draws the red and then blue text (manually positioned, though)
            pevent.Graphics.DrawString("red ", new Font("Arial", 10), Brushes.Red, 15, 0);
            pevent.Graphics.DrawString("blue", new Font("Arial", 10), Brushes.Blue, 35, 0);
        }
    }

Then just change the references to RadioButton class to the ColorRadioButton class in the designer codebehind file.

Note that this is a very simple example. You still have to think of a way how to pass the colors through the Text property and how to position the text portions, though.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now