pkp23
pkp23

Reputation: 21

How to set the backcolor of a label in a separate form C#

I have the code

            Results toResults = new Results();
            correct = 0;
            incorrect = 0;
            //Indicates clearly which answers are correct
            if (userGuessArray[0]==decompTimeArray[0])
            {
                toResults.yourAnswerLabel1.BackColor = Color.Green;
                lblone.BackColor = Color.Green;
                correct++;
            }
            else
            {
                toResults.yourAnswerLabel1.BackColor = Color.Red;
                lblone.BackColor = Color.Red;
                incorrect++;
            }

It doesn't seem to work for setting the backcolor of the label on the second form. I made it set the color on itself and that works but it just won't go to the other form. How might I remedy this?

I have indeed searched for many solutions to this but haven't found any.

Here is the code that actually shows it.

private void ShowResults()
        {
            //Shows the Results form.
            toResults.Show();
        }



private void resultsButton_Click(object sender, EventArgs e)
        {

           ShowResults();

        }

Upvotes: 1

Views: 72

Answers (2)

Telan Niranga
Telan Niranga

Reputation: 457

Since this is Windows form application use Static variable.

Create static variable in your main form and keep that to manage your color for the form.

Always read color from that static variable to apply where you want.

Upvotes: 1

DunningKrugerEffect
DunningKrugerEffect

Reputation: 613

In your second form create a property that will set those values for you.

Second form:

public Color _labelBackColor
{
  get { return myLabel.BackColor; }
  set { myLabel.BackColor = value; }
}

You can then change the value by calling

toResults._labelBackColor = Color.Green;

or by setting it when you instantiate the second form

var toResults = new Results { _labelBackColor = Color.Green };

Upvotes: 1

Related Questions