Question Switching BackColors of form and label by clicking button

wmincic

Member
Joined
Jun 1, 2016
Messages
6
Programming Experience
Beginner
  • When any one of the color buttons is clicked, the background color of the label is to be set to the background color of the color button that was clicked AND the background of the form is to be set to the color the label started with before being changed. (This means the color to the form will be changing at Runtime
This was a past assignment that i had that i couldn't figure out. Im not looking for answers but guidance. Beginning Visual C# class. I am not supposed to store colors into a variable. I am only supposed to be using the properties of the controls as stated:

  • As variables and object declarations have not been covered yet, do not try and use any variables when doing color switches. You will need to figure out how to switch colors and letters around using only the controls and various properties of those controls. Here is your hint on this: A control does not have to be visible for you to access the properties of the control.
My teacher is hard to get a hold of and we are having a test at the end of the week and im trying to get some studying in.

13318936_1724437764441777_1675734259_n.jpg

When i click a color button on the left the back color to the label is supposed to turn that color and the current backColor of the label is supposed to switch to the backColor of the form. I have to do this without using variables. My thought was to create 18 different labels and set the visible properties to false until the button was clicked but windows forms transparency does not support transparency to other labels just to the form. I can only think of storing the colors as variables but that is not what the instructions say to do. Any guidance would be greatly appreciated.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Default_Label.Visible = true;
            Default_LabelX.Visible = true;
            
        }


        private void label1_Click(object sender, EventArgs e)
        {


        }


        private void button1_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = true;
            Default_LabelX.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = false;
            yellow_label.Visible = false;
            green_label.Visible = false;
            cyan_label.Visible = false;
            blue_label.Visible = false;
            fuchsia_label.Visible = false;
            spring_label.Visible = false;
            crimson_label.Visible = false;
            Default_LabelX.Visible = false;


        }


        private void button2_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = true;
            yellow_label.Visible = false;
            green_label.Visible = false;
            cyan_label.Visible = false;
            blue_label.Visible = false;
            fuchsia_label.Visible = false;
            spring_label.Visible = false;
            crimson_label.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button3_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = false;
            yellow_label.Visible = true;
            green_label.Visible = false;
            cyan_label.Visible = false;
            blue_label.Visible = false;
            fuchsia_label.Visible = false;
            spring_label.Visible = false;
            crimson_label.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button4_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = false;
            yellow_label.Visible = false;
            green_label.Visible = true;
            cyan_label.Visible = false;
            blue_label.Visible = false;
            fuchsia_label.Visible = false;
            spring_label.Visible = false;
            crimson_label.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button5_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = false;
            yellow_label.Visible = false;
            green_label.Visible = false;
            cyan_label.Visible = true;
            blue_label.Visible = false;
            fuchsia_label.Visible = false;
            spring_label.Visible = false;
            crimson_label.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button6_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = false;
            yellow_label.Visible = false;
            green_label.Visible = false;
            cyan_label.Visible = false;
            blue_label.Visible = true;
            fuchsia_label.Visible = false;
            spring_label.Visible = false;
            crimson_label.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button7_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = false;
            yellow_label.Visible = false;
            green_label.Visible = false;
            cyan_label.Visible = false;
            blue_label.Visible = false;
            fuchsia_label.Visible = true;
            spring_label.Visible = false;
            crimson_label.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button8_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = false;
            yellow_label.Visible = false;
            green_label.Visible = false;
            cyan_label.Visible = false;
            blue_label.Visible = false;
            fuchsia_label.Visible = false;
            spring_label.Visible = true;
            crimson_label.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button9_Click(object sender, EventArgs e)
        {
            Red_Label.Visible = false;
            Default_Label.Visible = false;
            orange_label.Visible = false;
            yellow_label.Visible = false;
            green_label.Visible = false;
            cyan_label.Visible = false;
            blue_label.Visible = false;
            fuchsia_label.Visible = false;
            spring_label.Visible = false;
            crimson_label.Visible = true;
            Default_LabelX.Visible = false;
        }


        private void button6_ControlAdded(object sender, ControlEventArgs e)
        {


        }


        private void button6_BackColorChanged(object sender, EventArgs e)
        {


        }


        private void button14_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = false;
            
            Label_D.Visible = false;
            label_G.Visible = false;
            label_H.Visible = false;
            label_I.Visible = false;
            label_J.Visible = false;
            label_P.Visible = false;
            label_R.Visible = true;
            label_T.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void buttonY_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = true;
            
            Label_D.Visible = false;
            label_G.Visible = false;
            label_H.Visible = false;
            label_I.Visible = false;
            label_J.Visible = false;
            label_P.Visible = false;
            label_R.Visible = false;
            label_T.Visible = false;
            Default_LabelX.Visible = false;


        }


        private void buttonD_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = false;
            
            Label_D.Visible = true;
            label_G.Visible = false;
            label_H.Visible = false;
            label_I.Visible = false;
            label_J.Visible = false;
            label_P.Visible = false;
            label_R.Visible = false;
            label_T.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void buttonP_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = false;
            
            Label_D.Visible = false;
            label_G.Visible = false;
            label_H.Visible = false;
            label_I.Visible = false;
            label_J.Visible = false;
            label_P.Visible = true;
            label_R.Visible = false;
            label_T.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void buttonH_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = false;
            
            Label_D.Visible = false;
            label_G.Visible = false;
            label_H.Visible = true;
            label_I.Visible = false;
            label_J.Visible = false;
            label_P.Visible = false;
            label_R.Visible = false;
            label_T.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void buttonT_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = false;
            
            Label_D.Visible = false;
            label_G.Visible = false;
            label_H.Visible = false;
            label_I.Visible = false;
            label_J.Visible = false;
            label_P.Visible = false;
            label_R.Visible = false;
            label_T.Visible = true;
            Default_LabelX.Visible = false;
        }


        private void buttonG_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = false;
            
            Label_D.Visible = false;
            label_G.Visible = true;
            label_H.Visible = false;
            label_I.Visible = false;
            label_J.Visible = false;
            label_P.Visible = false;
            label_R.Visible = false;
            label_T.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button15_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = false;
            
            Label_D.Visible = false;
            label_G.Visible = false;
            label_H.Visible = false;
            label_I.Visible = false;
            label_J.Visible = true;
            label_P.Visible = false;
            label_R.Visible = false;
            label_T.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void button18_Click(object sender, EventArgs e)
        {
            Label_Y.Visible = false;
          
            Label_D.Visible = false;
            label_G.Visible = false;
            label_H.Visible = false;
            label_I.Visible = true;
            label_J.Visible = false;
            label_P.Visible = false;
            label_R.Visible = false;
            label_T.Visible = false;
            Default_LabelX.Visible = false;
        }


        private void cyan_label_Click(object sender, EventArgs e)
        {


        }
    }
}
 
Last edited:
That's really way too much code to be posting. If you can't narrow it down more than that then you haven't thought about the problem hard enough. Not only that, you' didn't format the code and you also included empty methods, which are clearly of no value to us.

Anyway, without having looked at your code, the way to do this would be to simply have a Label that was hidden. You then swap the colours in exactly the same way as you'd swap two numbers, i.e. move one Color to the temp location, move the other Color to the first and then the temp value to the other. It's simply using three locations to swap two values.
 
Sorry it is my first post how should i format that for future reference. Wouldn't storing a location take using a variable though? That's where i was getting caught up on.
 
Sorry it is my first post how should i format that for future reference.
Generally speaking, you should do it like this:

[xcode=c#]your code here[/xcode]

That will maintain the indenting in code copied from VS and also apply appropriate syntax highlighting. If you need to add your own formatting, e.g. bold and/or colour for emphasis, then you can do this:

[code]your code here[/code]

That also honours indenting but does not apply any other formatting. Note that you can type either yourself or use the button on the toolbar of the advanced editor. If you use the button for the first case then you must enter the C# option when prompted.
Wouldn't storing a location take using a variable though? That's where i was getting caught up on.
In actual fact, yes. There's really no way to do this without an extra variable. The question is where and how that variable is declared. If you add an extra Label in the designer and hide it then the IDE will declare the variable for you, just as it does for all your other controls. Normally you would simply declare a Color variable yourself and using a Label just for a Color property is huge overkill but being rather contrived is not unusual for tasks intended as learning experiences.
 
This is how i did it:

private void button1_Click(object sender, EventArgs e)
        {
            this.BackColor = label1.BackColor; 
            label1.BackColor = button1.BackColor;
        }


I was only able to think of using a variable as well until i bounced an idea off a friend and it clicked. I guess you essentially are using the properties of each control as variables.
 
Last edited:
I guess you essentially are using the properties of each control as variables.

And they are variables under the hood anyway. Your 'label1' is a variable; simply one that the IDE generated for you. Also, the BackColor property of a control is basically implemented like this:
private Color _backColor;

public Color BackColor
{
    get
    {
        return _backColor;
    }
    set
    {
        _backColor = value;
    }
}
 
Now say that i wanted to create a button that would switch the two properties would it just be:

private void button3_Click(object sender, EventArgs e)
        {
            label1.BackColor = this.BackColor;
            this.BackColor = label1.BackColor;
            
            
        }


Edit: that only seems to change one to the other not switch what was once the BackColor of the label to the BackColor of the form and vice versa.
 
Last edited:
Now say that i wanted to create a button that would switch the two properties would it just be:

private void button3_Click(object sender, EventArgs e)
        {
            label1.BackColor = this.BackColor;
            this.BackColor = label1.BackColor;
            
            
        }


Edit: that only seems to change one to the other not switch what was once the BackColor of the label to the BackColor of the form and vice versa.

If the code you posted here is different to what you posted before then it should be no surprise that you get a different result. I said back in my first reply that you need three locations to swap two values. How many locations do you have there?
 
Yeah i worked through it. What was getting me caught up was i wasn't setting the 3rd label visible property so it was showing up. Then i changed the visible property to false and i understand what you are saying now. Thank you
 
Back
Top Bottom