Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking Buttons Together

Tags:

c#

winforms

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]

enter image description here

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...

like image 545
Daniel Eugen Avatar asked Aug 15 '13 01:08

Daniel Eugen


People also ask

How do I make a button go to another link?

To create a button link to another page in HTML,just add <a> tag and wrap it around the simple Html button. Inside a <a> tag simply use href=“” attribute to give the path of the desired page. The output shows that, after clicking the “Click” button, you will be navigated to “Google” instantly.

How do I align two buttons on the same line?

If you have multiple buttons that should sit side-by-side on the same line, add the data-inline="true" attribute to each button. This will style the buttons to be the width of their content and float the buttons so they sit on the same line.

How do I link two buttons in HTML?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Can buttons be links?

There are differences as to when a button should be used rather than a link. UX Movement wrote an article about this, and they came up with a simple rule: Buttons are used for actions that affect the website's front-end or back-end; links are used for navigation and actions that don't affect the website at all.

How do you align buttons?

We can align the buttons horizontally as well as vertically. We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.

Can a button be a child of a button?

It is not valid to put a <button> inside a <button> element. In the W3C recomendation for the button element you can read: Content model: Phrasing content, but there must be no interactive content descendant.


2 Answers

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;
}
like image 127
Roman Ratskey Avatar answered Oct 07 '22 23:10

Roman Ratskey


  1. Put the buttons in an array
  2. Create a click event that looks up the index n of the button (sender) in the array and sets the style of each button 0 - n appropriately.
  3. Wire up each button to the click event

Do as much as you can on your own and come back with specific questions as necessary.

like image 40
D Stanley Avatar answered Oct 07 '22 23:10

D Stanley