Can we use parameter in mouse click event C #

I am using a loop for a 2D array of buttons. I don't know how to know exactly which buttons in the array were pressed or not.

Here is 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 i, j parameters for the 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 it comes out, how do I write it down correctly? Or can you recommend or a method to know exactly where the button was clicked in the array?

+1


source to share


3 answers


You cannot change the EventHandler signature to include i

and j

.

However, you can get this information from what is already passed to the method arrButton_Click

. Since you are setting the location of each button as new Point(j*size1button, i*size1button)

, you can return each component i

and j

by dividing the location of your button by size1button

.

To get this location, you can use sender

which is yours Button

(listing needed):

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 are using to create the buttons has a couple of bugs.



First, you never increase j

; the second cycle has i++

.

Second, if you want your buttons to appear, you must add them to your form 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 will look like this:

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 may also notice that I removed your ad new EventHandler

that was redundant.

0


source


If you are only interested in Location

what is wrong with that? -

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 space. Here's an example

Use your own 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 class of buttons -

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 Cast

before CustomButton

and voila event handler 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 to implement your own event / delegate.

0


source


try it

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

      

Now in the loop set the 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 like

 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;
 }

      

0


source







All Articles