foreach (Control pControl in Controls)
            {
                if (pControl is Panel)
                {
                    foreach (Control IControl in pControl.Controls)
                    {
                        if (IControl is PictureBox)
                        {
                            //IControl.Click += this.Image_Click;
                            
                            IControl.Click += new System.EventHandler(this.Image_Click);
                        }
                    }
                }
            }
        }
这一段代码是form_load中的,是遍历所有的picturebox控件,并加载相应的委托事件,如我下面写的。
但效果不理想,我只要点其中任何一个picturebox,会把所有的picturebox都单击一遍,所以我想在上面一段代码
传个参数给下面的代码,希望高人给点指点。拜谢。
            foreach (Control pControl in Controls)
            {
                if (pControl is Panel)
                {
                    foreach (Control IControl in pControl.Controls)
                    {
                        if (IControl is PictureBox)
                        {
                            if (IControl.Name.Length == 11)
                            {
                                int PictureIndex = Convert.ToInt32(IControl.Name.Substring(10, 1));
                                this.dataGridViewRegList(PictureIndex - 1);
                            }
                            if (IControl.Name.Length == 12)
                            {
                                int PictureIndex = Convert.ToInt32(IControl.Name.Substring(10, 2));
                                this.dataGridViewRegList(PictureIndex - 1);
                            }
                        }
                    }
                }
            }

解决方案 »

  1.   

    不需要在this.Image_Click中写:                            if ((sender as Control).Name.Length == 11)
                                 {
                                     int PictureIndex = Convert.ToInt32(IControl.Name.Substring(10, 1));
                                     this.dataGridViewRegList(PictureIndex - 1);
                                 }
                                 if ((sender as Control).Name.Length == 12)
                                 {
                                     int PictureIndex = Convert.ToInt32(IControl.Name.Substring(10, 2));
                                     this.dataGridViewRegList(PictureIndex - 1);
                                 }
      

  2.   

    你这样 给所有PictureBox都加上了相同的click事件,可以通过id去区分不能的PictureBox,然后去处理你需要处理的PictureBox.