我有一个小程序,它在 2D 数组中保留 4 个按钮我想要做的是在消息框中显示它的“X”和“Y”坐标(单击时)
我尝试了多种方法,有些不起作用,有些起作用,但我无法让它显示“X”和“Y”值
下图显示了我到目前为止所拥有的:
这是我想出的代码:
namespace _2DArray
{
public partial class Form1 : Form
{
private Button[,] b;
public Form1()
{
InitializeComponent();
b = new Button[2, 2];
b = new Button[,] { {button1,button2 },
{button3, button4}};
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Button bt in b)
{
bt.Click += new System.EventHandler(this.ClickedButton);
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button:" + s);
}
}
}