C# 将按钮链接在一起

C# 将按钮链接在一起,c#,winforms,C#,Winforms,我试图在表单上的一些按钮之间创建某种链接,这样当我单击一个按钮时,它会高亮显示之前的所有按钮[某种音量控制器] 把它想象成一个音量控制器。所有这些彩色按钮都将是灰色的,我想实现的是,当你点击一个按钮时,它会给它前面的所有按钮上色;然而IDK什么是在不涉及大量无用代码的情况下做出这种行为的最佳方式 把按钮排成一列 创建一个单击事件,查找数组中按钮(sender)的索引n,并适当设置每个按钮的样式0-n 将每个按钮连接到单击事件 尽可能多地独立完成任务,并根据需要提出具体问题。首先,您需要将所有按

我试图在表单上的一些按钮之间创建某种链接,这样当我单击一个按钮时,它会高亮显示之前的所有按钮[某种音量控制器]

把它想象成一个音量控制器。所有这些彩色按钮都将是灰色的,我想实现的是,当你点击一个按钮时,它会给它前面的所有按钮上色;然而IDK什么是在不涉及大量无用代码的情况下做出这种行为的最佳方式

  • 把按钮排成一列
  • 创建一个单击事件,查找数组中按钮(
    sender
    )的索引
    n
    ,并适当设置每个按钮的样式
    0
    -
    n
  • 将每个按钮连接到单击事件

  • 尽可能多地独立完成任务,并根据需要提出具体问题。

    首先,您需要将所有按钮添加到一个数组中,然后从那里开始处理

    代码
    //创建一个按钮数组并连接每个按钮的单击事件
    私有按钮[]卷按钮{get;set;}
    公用干管()
    {
    初始化组件();
    //假设您有21个按钮,因为它出现在您的图片。。。
    VolumeButtons=新按钮[21];
    音量按钮[0]=按钮1;
    音量按钮[1]=按钮2;
    音量按钮[2]=按钮3;
    音量按钮[3]=按钮4;
    音量按钮[4]=按钮5;
    音量按钮[5]=按钮6;
    音量按钮[6]=按钮7;
    音量按钮[7]=按钮8;
    音量按钮[8]=按钮9;
    音量按钮[9]=按钮10;
    音量按钮[10]=按钮11;
    音量按钮[11]=按钮12;
    音量按钮[12]=按钮13;
    音量按钮[13]=按钮14;
    音量按钮[14]=按钮15;
    音量按钮[15]=按钮16;
    音量按钮[16]=按钮17;
    音量按钮[17]=按钮18;
    音量按钮[18]=按钮19;
    音量按钮[19]=按钮20;
    音量按钮[20]=按钮21;
    foreach(VolumeButtons中的变量volumeButton)
    音量按钮。单击+=音量按钮\u单击;
    }
    void VolumeButton_单击(对象发送方,事件参数e)
    {
    //查找已单击按钮的索引
    int index=Array.FindIndex(VolumeButtons,0,VolumeButtons.Length,button=>button==((按钮)发送器));
    //将之前所有按钮的颜色设置为Aqua,将所有前进按钮的颜色设置为灰色[您可以使用它来匹配您的颜色]
    for(int i=0;iVolumeButtons[i].BackColor=i如何查找单击按钮的索引?非常感谢@RuneS,它非常有用WinForms或WPF?我已根据您接受的答案在您的问题中添加了[WinForms]。嗯,我曾尝试在您的问题中添加[WinForms],但现在它说我的编辑正在等待“同行评审”…如果你真的想变得更花哨,你可以在运行时创建按钮控件。这可以避免20条赋值语句的难看集合,但可能不值得这么麻烦。
    
    //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;
    }