简体   繁体   English

无法获取点击按钮的索引

[英]Cant get the index of the clicked button

I have in my resources images named: {1.jpg ,2.jpg, ............., 30.jpg} and i have buttons{btn1,btn2,.............,btn30} all i want to do is to put by exemple image 1.jpg in a panel by clicking btn1...same goes to image2 for btn2 ..till image 30 for btn30 in the same panel this is what i was writing in my code... 我的资源中有名为{1.jpg,2.jpg,.............,30.jpg}的图像,并且有按钮{btn1,btn2,...... .......,btn30}我要做的就是通过单击btn1将示例图像1.jpg放置在面板中...相同的内容转到btn2的图像2 ..直到btn30的图像30放在同一面板中这就是我在代码中编写的内容...

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Button[] buttons = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14, btn15, btn16, btn17, btn18, btn19, btn20, btn21, btn22, btn22, btn23, btn24, btn25, btn26, btn27, btn28, btn29, btn30 };

        //did this becz couldnt fill the array buttons with a for loop...hope if u know to tell me how
        for(int i=0;i<30;i++) buttons[i].Click += myEventHandler;

    }

    void myEventHandler(object sender, EventArgs e)
    { 
        Button button = sender as Button;
        for(int i=0;i<30;i++)
        {
            if (sender.Equals ////howw to get the index of the button
        }

        Bitmap b=new Bitmap(myProject.Properties.Resources  //how to apply the index of the button in getting the name of the image;
        panel2.BackgroundImage=b;
    }
}

Keep your buttons in a private List in the class and use IndexOf in the event handler. 将按钮放在类的私有List中,并在事件处理程序中使用IndexOf。

private List<Button> buttons;

private void Form1_Load(object sender, EventArgs e)
{
    buttons = new [] { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14, btn15, btn16, btn17, btn18, btn19, btn20, btn21, btn22, btn22, btn23, btn24, btn25, btn26, btn27, btn28, btn29, btn30 }.ToList();
    buttons.ForEach(x => x.Click += myEventHandler);
}

void myEventHandler(object sender, EventArgs e)
{ 
    Button button = sender as Button;
    int idx = buttons.IndexOf(button);
}

As I said on the comments I would, personally, refactor a little and use lambda expressions that call a generic 'ChangeImage' method. 正如我在评论中所说,我个人将进行一些重构,并使用调用通用“ ChangeImage”方法的lambda表达式。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Button[] buttons = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14, btn15, btn16, btn17, btn18, btn19, btn20, btn21, btn22, btn22, btn23, btn24, btn25, btn26, btn27, btn28, btn29, btn30 };

        //did this becz couldnt fill the array buttons with a for loop...hope if u know to tell me how


        for (int i = 0; i < 30; i++)
        {
            int n = i;

            buttons[i].Click += (object s, EventArgs ea) => ChangeImage(n);
        }
    }

    void ChangeImage(int id)
            {
Bitmap b=new Bitmap(myProject.Properties.Resources  //how to apply the index of the button in getting the name of the image;
                panel2.BackgroundImage=b;


            }
}

You can then use the ChangeImage method to chanmge your background to the image with the ID passed in via the id parameter :) 然后,您可以使用ChangeImage方法将背景更改为具有通过id参数传递的ID的图像:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM