Reputation: 41
Good to know is that I just started programming, so go easy on me ;)
In my program I make a board consisting of several buttons (btn[i, j]
), which I create by using two for loops. These buttons are given a coordinate pair/index [i, j]
, then I pass this to a 2d array called valueBtn
and I give that coordinate pair a value on the corresponding index.
public void board(object obj, EventArgs ea)
{
int n = 6;
Button[,] btn = new Button[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int p = 60 * i + 100;
int q = 60 * j + 100;
btn[i, j] = new Button();
btn[i, j].Location = new Point(p, q);
btn[i, j].Size = new Size(60, 60);
int[,] valueBtn = new int[n, n];
valueBtn[i, j] = 0;
this.Controls.Add(btn[i, j]);
}
}
btn[i, j].Click += btnPress;
}
Next I have a new method btnPress
which is linked to the EventHandler btn[i, j].Click
. The intention is that in this method I find out which button has been pressed, and which coordinates/index belongs to this so that I can find the corresponding value in the 2d array valueBtn
and eventually draw this value in a new function drawValue
.
public void btnPress(object sender, EventArgs ea)
{
Button pressedBtn = sender as Button;
// Here I want to know which button is pressed and the index [i,j] of the button
// so that I can find the value that belongs to the button in the 2d valueBtn array
.
.
.
this.Paint += drawValue;
}
I've tried a lot with references to the EventHandler, but I just can't figure it out.
Thank you very much in advance for your time and help!
Upvotes: 1
Views: 252
Reputation: 1
You can set the Text
property of each button like btn[i, j].Text = some i j combination
. and then get it in btnPress
method like this string s = (sender as Button).Text;
. You can also use Tag
property, so basically concept is same.
Upvotes: 1