Can we use parameter in mouse click event c#

2019-07-28 01:41发布

I use the loop for a two dimensions array of buttons. I don't know how to know exactly which buttons in array were clicked or not

Here are my code:

 for (int i = 0; i < 100 ; i++)
        {
            for (int j=0 ; j< 100; i++)
            {
                arrButton[i, j] = new Button();
                arrButton[i,j].Size = new Size(size1button, size1button);
                arrButton[i,j].Location = new Point(j*size1button, i*size1button);
                arrButton.Click += new EventHandler(arrButton_Click);
            }
        }

Can I use parameters i, j for mouse click event like:

     private void arrButton_Click(object sender, EventArgs e, int i, int j)
       {
         //my idea : add i, j to another int[,] array to keep track of buttons which were clicked
       }

If this exits, how to write it correctly? Or can you recommend or method to know exactly where the button was clicked in array ?

3条回答
老娘就宠你
2楼-- · 2019-07-28 02:11

Try this

public class Indeces
{
   public int IndexI { get; set; }
   public int IndexJ { get; set; }
}

Now in loop set Tag

    for (int i = 0; i < 100 ; i++)
    {
        for (int j=0 ; j< 100; i++)
        {
            arrButton[i, j] = new Button();
            arrButton[i,j].Size = new Size(size1button, size1button);
            arrButton[i,j].Location = new Point(j*size1button, i*size1button);
            arrButton.Click += new EventHandler(arrButton_Click);
            arrButton.Tag = new Indeces {IndexI = i,IndexJ = j};
        }
    }

Get values from Tag here as

 private void arrButton_Click(object sender, EventArgs e)
 {
     var button = sender as Button;
     var indeces = (Indeces) button.Tag;//get indeces here
     var i = indeces.IndexI;
     var j = indeces.IndexJ;
 }
查看更多
对你真心纯属浪费
3楼-- · 2019-07-28 02:22

You cannot change the EventHandler signature to include your i and j.

However, you can get that information from what is already passed to the arrButton_Click method. Since you set the location of each button as new Point(j*size1button, i*size1button), you can get each i and j component back by dividing the location of your button by size1button.

To get that location, you can use the sender, which is your Button (a cast is necessary):

private void arrButton_Click(object sender, EventArgs e)
{
    Button btnClicked = (Button) sender;
    int i = btnClicked.Location.Y / size1button;
    int j = btnClicked.Location.X / size1button;
}

Also, the code you're currently using to create the buttons have a couple errors.

First, you're never incrementing j; the second loop does i++.

Second, if you want your buttons to appear, you have to add them to your Form's Controls.

Finally, I don't think you can have 10 000 active buttons on your form, try a lower number, like 25.

So the corrected code would look like:

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        arrButton[i, j] = new Button();

        arrButton[i, j].Size = new Size(size1button, size1button);
        arrButton[i, j].Location = new Point(j*size1button, i*size1button);
        arrButton[i, j].Click += arrButton_Click;
        Controls.Add(arrButton[i,j]);
    }
}

You can also notice that I removed your declaration of new EventHandler, which was redundant.

查看更多
叛逆
4楼-- · 2019-07-28 02:29

If you are only interested in Location then whats wrong with this? -

private void arrButton_Click(object sender, EventArgs e)
       {
         var button = sender as Button;
         //now use button.Location
       }

But if you want more data other than just location. Here is an example

Use a custom button class -

public class CustomButton<T> : Button {
    public T Data{get;set;}
    public CustomButton(T data){
        this.Data = data; //i didn't compile it, so data type might mismatch.
    }
}

Then use this button class -

for (int i = 0; i < 100 ; i++)
    {
        for (int j=0 ; j< 100; i++)
        {
            arrButton[i, j] = new CustomButton<T> (...some data);
            arrButton[i,j].Size = new Size(size1button, size1button);
            arrButton[i,j].Location = new Point(j*size1button, i*size1button);
            arrButton.Click += new EventHandler(arrButton_Click);
        }
    }

In the event handler Cast to CustomButton and voila, there is your location -

private void arrButton_Click(object sender, EventArgs e)
   {
       var cButton = sender as CustomButton<T>;
       // cButton.Datais your point. Have fun
   }

BTW, you cannot change the default signature of event handlers, if you want you have to implement your own event/delegate.

查看更多
登录 后发表回答