C# - 单击事件发送一个值

标签 c# picturebox

我正在使用 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/

相关文章:

c# - 如果隐式已在 C# 中重载,则重载显式运算符是否有好处?

c# - Google recaptcha 公钥和私钥

c# - 移动按钮和图片框 vb/c#

c# - Socket.ReceiveAsync 什么时候同步返回?

c# - 在这种情况下如何使用 outputcache?

c# - 图片框 C# 中的圆角边缘

c# - 在图片盒中绘制颜色?

c# - 在 PictureBox 上绘图

c# - 为什么 .NET 无法检测具有长文件路径的目录?

c# - PictureBox 大小不正确