我正在使用 for 循环将值添加到 PictureBox 数组中,并将点击事件绑定(bind)到每个值。我正在寻找一种在单击 PictureBox 后获取其数据的方法。由于它是一个数组,我正在考虑发送循环计数器的值,它可以识别点击了哪个。
我的代码是这样的:
PictureBox[] picboxes = new PictureBox[result];
for (int i = 0; i < results; i++)
{
picboxes[i] = new PictureBox();
picboxes[i].ImageLocation = @FormIni.RetRes((i * 5) + 5 + i);
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click);
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
label1.Text = "here I need the value of the picboxes[i] image location";
}
这看起来很愚蠢,但我想到了类似的东西:
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click(i))
和
private void PictureBoxes_Click(object sender, EventArgs e, int i)
简而言之:当我通过代码单击在数组中创建的 PictureBox 时,如何获取其值(在单击事件处理程序中)?
编辑!
很抱歉在提出这个问题后才找到它,但我已经找到了this解决方案,它可能适用于我的情况,对吗?
最佳答案
尝试这样做
PictureBox[] picboxes = new PictureBox[result];
for (int i = 0; i < results; i++)
{
picboxes[i] = new PictureBox();
picboxes[i].Name = (i+1).ToString();
picboxes[i].ImageLocation = @FormIni.RetRes((i * 5) + 5 + i);
picboxes[i].Click += new System.EventHandler(PictureBoxes_Click);
}
private void PictureBoxes_Click(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
string j = p.Name;
label1.Text = j;
}
关于C# - 单击事件发送一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16868409/