1

我正在动态创建一些图片框并点击图片框的事件如下

Image myImage = Image.FromFile("image/Untitled6.png"); 
PictureBox[] txtTeamNames = new PictureBox[5];        

for (int i = 0; i < txtTeamNames.Length; i++)
{
  var txt = new PictureBox();
  txtTeamNames[i] = txt;                
  txtTeamNames[i].Image = myImage;                
  txtTeamNames[i].Height = 53;
  txtTeamNames[i].Width = 48;                
  this.panel1.Controls.Add(txtTeamNames[i]);                
  txtTeamNames[i].Visible = true;
  txtTeamNames[i].Click += new EventHandler(this.clcikeventhandle);
}

当有人点击任何图片框时,我如何找到它的数组索引和名称?

void clickEventHandler(object sender, EventArgs e)
{          
  //???
}
4

4 回答 4

3

您可以PictureBox通过sender参数访问 。所以试试这个:

PictureBox[] txtTeamNames;

void YourMethod()
{
    Image myImage = Image.FromFile("image/Untitled6.png"); 
    txtTeamNames = new PictureBox[5];        
    //The same as your code
}   

void clcikeventhandle(object sender, EventArgs e)
{          
    int index = txtTeamNames.IndexOf(sender As PictureBox);
}

编辑:方法#2

但是,如果您对在类范围内声明该数组不满意,您可以尝试以下方法:

//Same as your code
for (int i = 0; i < txtTeamNames.Length; i++)
{
    //Save as your code
    txtTeamNames[i].Tag = i;                        // ADD THIS LINE
}

然后:

void clcikeventhandle(object sender, EventArgs e)
{            
    int index = int.Parse((sender as PictureBox).Tag.ToString());
}
于 2013-04-01T17:41:36.417 回答
1

另一个建议 - 创建一个自定义类,它继承自PictureBox. 它将有一个额外的Index属性。您可以在这两行之间进行设置:

txtTeamNames[i].Visible = true;
//assign the index here
txtTeamNames[i].Click += new EventHandler(this.clcikeventhandle);

像这样:

txtTeamNames[i].Index = i;

然后在处理程序中:

void clickEventHandle(object sender, EventArgs e)
{ 
  PictureBox pbox = sender As PictureBox;
  int index = pbox.Index();
  string name = pbox.Name();
}

您保持相同的变量范围,如果您担心它可能会很有用。如果您可以将范围升级txtTeamNames到班级级别,请参阅Hossein Narimani Rad的另一个答案

于 2013-04-01T17:54:39.013 回答
0
namespace your_name_project
{
    public partial class Form_Begin : Form
    {
        PictureBox[] pictureBoxs = new PictureBox[6];
        public Form_Begin()
        {
            InitializeComponent();
            pictureBoxs[0] = pictureBox1; pictureBoxs[1] = pictureBox2; pictureBoxs[2] = pictureBox3;
            pictureBoxs[3] = pictureBox4; pictureBoxs[4] = pictureBox5;   pictureBoxs[5] = pictureBox6;     
        }
//continue 
        List<PictureBox> pictureBoxes = new List<PictureBox>();

            private void buttonX1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i <3; i++)
                {
                    pictureBoxs[i].Image =your_name_project.Properties.Resources.image_1;// load image1 and Image_2from resource in property of picturebox  
                }
                for (int i = 3; i < 6; i++)
                {
                    pictureBoxs[i].Image = your_name_project.Properties.Resources.Image_2;
                }
            } 
        }
}
于 2016-03-30T08:51:59.987 回答
0

我已经尝试过上面引用的第一种方法,但它对我不起作用。我需要更改语法才能工作。我不确定为什么,但我的方法 1 的工作方式是:

PictureBox[] txtTeamNames;

void YourMethod()
{
    Image myImage = Image.FromFile("image/Untitled6.png"); 
    txtTeamNames = new PictureBox[5];        
    //The same as your code
}   

void clcikeventhandle(object sender, EventArgs e)
{          
    int index = Array.IndexOf(txtTeamNames, sender); //DIFFERENT HERE
}

如果有人有想法...

于 2021-12-28T03:50:06.563 回答