0

I would like to use a variable in multiple functions in Visual Studio. I'm trying to get value_1 to be used to show as a label for private void button1_Click.

If I try it this way I get this error message:

'Black1_CheckedChanged' matches delegate 'EventHandler'

What am I missing? I would really appreciate the help. Thank you.

public partial class Form1 : Form
{
    int value_1 = 20;
    int value_2 = 20;
    int value_3 = 20;

    public Form1()
    {
        InitializeComponent();
    }

    // Groupe Colour 1
    private void Black1_CheckedChanged(object sender, EventArgs e, int value_1)
    {

        colour1.BackColor = System.Drawing.Color.Black;
        groupBox1.BackColor = System.Drawing.Color.Black;

        value_1 = 0;
    }

    private void Brown1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Brown;
        groupBox1.BackColor = System.Drawing.Color.Brown;

        value_1 = 1;
    }

    private void Red1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Red;
        groupBox1.BackColor = System.Drawing.Color.Red;

        value_1 = 2;
    }

    private void Orange1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Orange;
        groupBox1.BackColor = System.Drawing.Color.Orange;

        value_1 = 3;
    }

    private void Yellow1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Yellow;
        groupBox1.BackColor = System.Drawing.Color.Yellow;

        value_1 = 4;
    }

    private void Green1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Green;
        groupBox1.BackColor = System.Drawing.Color.Green;

        value_1 = 5;
    }

    private void Blue1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Blue;
        groupBox1.BackColor = System.Drawing.Color.Blue;

        value_1 = 6;
    }

    private void Purple1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Purple;
        groupBox1.BackColor = System.Drawing.Color.Purple;

        value_1 = 7;
    }

    private void Grey1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Gray;
        groupBox1.BackColor = System.Drawing.Color.Gray;

        value_1 = 8;
    }

    private void White1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.White;
        groupBox1.BackColor = System.Drawing.Color.White;

        value_1 = 9;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        value_1 = int.Parse(label_total.Text);
    }
}
CryogenicNeo
  • 937
  • 12
  • 25
Adi T
  • 1
  • 2
    `(object sender, EventArgs e, int value_1)` should be `(object sender, EventArgs e)`. Also, nowhere are you assigning the value of `value_1` to the button's text. – ProgrammingLlama Apr 25 '18 at 02:55
  • Note that event handlers are effectively contracts that they accept certain parameters, you can't just change the parameters. Think of it like a lock and key, if they don't match, they don't work. – ProgrammingLlama Apr 25 '18 at 02:58
  • Declare your `int`s in your Class scope (under `public partial class Form1 : Form`). Then you can access their values anywhere in that class. Don't touch the event handlers declaration. They won't match the delegate form anymore. – Jimi Apr 25 '18 at 03:02
  • @Jimi OP already has. – ProgrammingLlama Apr 25 '18 at 03:03
  • 1
    @john HA. You're right. The indentation fooled me. They seemed to be declared along InitializeComponent(); – Jimi Apr 25 '18 at 03:05

1 Answers1

0

Black1.CheckedChanged expects a method of specific signature (with arguments object and EventArgs). The number of parameters for Black1_CheckedChanged function was changed. With more parameters than two parameters, the call to the function won't find the value for the third parameter int value_1 and that error raises. That's why error is thrown.

To solve that, you must change this part of your code:

private void Black1_CheckedChanged(object sender, EventArgs e, int value_1)
{
    colour1.BackColor = System.Drawing.Color.Black;
    groupBox1.BackColor = System.Drawing.Color.Black;

    value_1 = 0;
}

To this:

private void Black1_CheckedChanged(object sender, EventArgs e)
{
    colour1.BackColor = System.Drawing.Color.Black;
    groupBox1.BackColor = System.Drawing.Color.Black;

    value_1 = 0;
}

Or, as you can see in this answer, you can use a lambda expression as an adapter to your event handler and you can use your current function without changes:

Black_1.CheckedChanged += new CheckedChanged((sender, e) => Black1_CheckedChanged(sender, e, value_1));

With the previous solution, your code will be like this:

public partial class Form1 : Form
{
    int value_1 = 20;
    int value_2 = 20;
    int value_3 = 20;

    public Form1()
    {
        InitializeComponent();
        Black_1.CheckedChanged += new CheckedChanged((sender, e) => Black1_CheckedChanged(sender, e, value_1));
    }

    ...

    private void Black1_CheckedChanged(object sender, EventArgs e, int value_1)
    {
        colour1.BackColor = System.Drawing.Color.Black;
        groupBox1.BackColor = System.Drawing.Color.Black;

        value_1 = 0;
    }
}

If you need to pass a new parameter in a simple way, you can pass the value by other way, like using other variable in class scope (class property) to store the value you need.

For example:

public partial class Form1 : Form
{
    int value_1 = 20;
    int value_2 = 20;
    int value_3 = 20;
    int temp = 0;

    public Form1()
    {
        InitializeComponent();
    }

    ...

    private void Black1_CheckedChanged(object sender, EventArgs e)
    {
        colour1.BackColor = System.Drawing.Color.Black;
        groupBox1.BackColor = System.Drawing.Color.Black;

        temp = 0;
    }
}
CryogenicNeo
  • 937
  • 12
  • 25