3

Can I change the color on the portion of the following string represented by m_pwd and m_senior?

string m_pwd = "PWD";
string m_senior = "Senior";

lblMarquee.Text = "Hi" + m_senior + "and" + m_pwd;
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
NatsuDragneel
  • 297
  • 1
  • 4
  • 21

5 Answers5

2

You should create several labels with different colors to create that. You can use a panel if your goal is to hide/show the labels for easier manipulation.

Tonned
  • 286
  • 2
  • 9
2

You cannot change the "color" of a string, just like you can't change the color of a number or a list or any other data type that simply stores values.

What you need to do is to modify the visual representation of that data, in this case the Label control. Anything deriving from Control has an available ForeColor and BackColor property.

An easy way to do what you need in WinForms is to group several Labels next to each other in a Panel, change colors on each as desired, and then manipulate the Panel as a single entity as necessary.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
2

The Label control only supports one ForeColor at a time. If you need to handle more than one colour simultaneously, you could set up a FlowLayoutPanel that flows horizontally, then include multiple Labels in it that are coloured as you choose. It isn't the prettiest solution on earth programmatically speaking, but I believe it should work for you.

I say use a FlowLayoutPanel and not a Panel because this will allow you to use variable-length strings without worrying about positioning.

In essence, do something like this. The Control creation, of course, would generally occur in the designer. But you see how it could work.

FlowLayoutPanel flp = new FlowLayoutPanel();

Label lblA = new Label();
lblA.Text = "Hi ";
flp.Controls.Add(lblA);

Label lblB = new Label();
lblB.Text = m_senior;
lblB.ForeColor = Color.Red;
flp.Controls.Add(lblB);

Label lblC = new Label();
lblC.Text = " and "; // The spaces for this and "Hi " may or may not be necessary. They are in theory, but it's mostly dependent on the margins of the Labels. Just check which looks best.
flp.Controls.Add(lblC);

Label lblD = new Label();
lblD.Text = m_pwd;
lblD.ForeColor = Color.Red;
flp.Controls.Add(lblD);
Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54
0

Similar to Matthew Haugen's answer provided above, using the flow layout panel. But with my example the creation of the labels is done dynamically. This being useful by applying any Split string and distinguishing between what text you want colored and what text you dont.

FlowLayoutPanel flowPanel = new FlowLayoutPanel();
        string m_pwd = "PWD";
        string m_senior = "Senior";

        string toBeColored = "Hi " + m_senior + " and " + m_pwd;
        string[] splitColored = toBeColored.Split(' ');

        foreach (string s in splitColored)
        {
            Label l = new Label();
            l.Text = s;
            if (s == m_pwd)
            {
                l.ForeColor = Color.Red;
            }
            else if (s == m_senior)
            {
                l.ForeColor = Color.Blue;
            }

            flowPanel.Controls.Add(l);
        }
        this.Controls.Add(flowPanel); //was missing from earlier example 

Note: to make this visually pleasing I'd recommend sizing the flow layout panel according to your application

Spencer Waz
  • 155
  • 8
0

Here's mine but this one is for marquee purposes

      lbl_PWD.ForeColor = Color.Red;
        lbl_senior.ForeColor = Color.Red;

        if (panel1.Right > 0)
        {
            panel1.Left = panel1.Left - 5;
        }
        else if (panel1.Right <= 0)
        {
            panel1.Left = this.Width;
        }
NatsuDragneel
  • 297
  • 1
  • 4
  • 21