Linking Buttons Together

c#, winforms

Solution

First you will need to add all the buttons to an array then handle it from there

Code

//Create an array of buttons and hook up the Click event of each of them
private Button[] VolumeButtons { get; set; }

public Main()
{
    InitializeComponent();

    //Assuming that you have 21 buttons as it appears in your picture...
    VolumeButtons = new Button[21];
    VolumeButtons[0] = button1;
    VolumeButtons[1] = button2;
    VolumeButtons[2] = button3;
    VolumeButtons[3] = button4;
    VolumeButtons[4] = button5;
    VolumeButtons[5] = button6;
    VolumeButtons[6] = button7;
    VolumeButtons[7] = button8;
    VolumeButtons[8] = button9;
    VolumeButtons[9] = button10;
    VolumeButtons[10] = button11;
    VolumeButtons[11] = button12;
    VolumeButtons[12] = button13;
    VolumeButtons[13] = button14;
    VolumeButtons[14] = button15;
    VolumeButtons[15] = button16;
    VolumeButtons[16] = button17;
    VolumeButtons[17] = button18;
    VolumeButtons[18] = button19;
    VolumeButtons[19] = button20;
    VolumeButtons[20] = button21;

    foreach (var volumeButton in VolumeButtons)
        volumeButton.Click += VolumeButton_Click;
}

void VolumeButton_Click(object sender, EventArgs e)
{
    //Find the index of the clicked button
    int index = Array.FindIndex(VolumeButtons, 0, VolumeButtons.Length, button => button == ((Button)sender));

    //Set the color of all the previous buttons to Aqua, and all the forward buttons to gray [ you may play with it to match your colors then ]
    for (int i = 0; i < VolumeButtons.Length; i++)
        VolumeButtons[i].BackColor = i <= index ? Color.Aqua : Color.Gray;
}

Problem

I am trying to create some kind of linkage between some buttons on the form so that when i click a button it highlights all the previous buttons to it [some kind of volume controller] Imagine it as a volume controller. All these colored buttons will be gray and what i want to achieve is as you click on a button it colorizes all the buttons before it; However IDK what is the best way to make a behavior like this without involving tons of useless codes...

Original source