c#获取窗体中所有PictureBox控件,然后存入一个pictureBox集合中,请问怎么实现啊?

解决方案 »

  1.   

    foreach (Control c in window.Controls)
    {
      if (c is PictureBox)
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication7
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button5;
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.PictureBox pictureBox2;
    private System.Windows.Forms.PictureBox pictureBox3;
    private System.Windows.Forms.PictureBox pictureBox4;
    private System.Windows.Forms.PictureBox pictureBox5;
    private System.Windows.Forms.PictureBox pictureBox6;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.button5 = new System.Windows.Forms.Button();
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.pictureBox2 = new System.Windows.Forms.PictureBox();
    this.pictureBox3 = new System.Windows.Forms.PictureBox();
    this.pictureBox4 = new System.Windows.Forms.PictureBox();
    this.pictureBox5 = new System.Windows.Forms.PictureBox();
    this.pictureBox6 = new System.Windows.Forms.PictureBox();
    this.SuspendLayout();
    // 
    // button5
    // 
    this.button5.Location = new System.Drawing.Point(344, 284);
    this.button5.Name = "button5";
    this.button5.Size = new System.Drawing.Size(120, 76);
    this.button5.TabIndex = 4;
    this.button5.Text = "button5";
    this.button5.Click += new System.EventHandler(this.button5_Click);
    // 
    // pictureBox1
    // 
    this.pictureBox1.Location = new System.Drawing.Point(24, 52);
    this.pictureBox1.Name = "pictureBox1";
    this.pictureBox1.Size = new System.Drawing.Size(80, 32);
    this.pictureBox1.TabIndex = 5;
    this.pictureBox1.TabStop = false;
    // 
    // pictureBox2
    // 
    this.pictureBox2.Location = new System.Drawing.Point(20, 112);
    this.pictureBox2.Name = "pictureBox2";
    this.pictureBox2.Size = new System.Drawing.Size(76, 28);
    this.pictureBox2.TabIndex = 6;
    this.pictureBox2.TabStop = false;
    // 
    // pictureBox3
    // 
    this.pictureBox3.Location = new System.Drawing.Point(20, 192);
    this.pictureBox3.Name = "pictureBox3";
    this.pictureBox3.TabIndex = 7;
    this.pictureBox3.TabStop = false;
    // 
    // pictureBox4
    // 
    this.pictureBox4.Location = new System.Drawing.Point(172, 60);
    this.pictureBox4.Name = "pictureBox4";
    this.pictureBox4.TabIndex = 8;
    this.pictureBox4.TabStop = false;
    // 
    // pictureBox5
    // 
    this.pictureBox5.Location = new System.Drawing.Point(164, 156);
    this.pictureBox5.Name = "pictureBox5";
    this.pictureBox5.TabIndex = 9;
    this.pictureBox5.TabStop = false;
    // 
    // pictureBox6
    // 
    this.pictureBox6.Location = new System.Drawing.Point(168, 244);
    this.pictureBox6.Name = "pictureBox6";
    this.pictureBox6.TabIndex = 10;
    this.pictureBox6.TabStop = false;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(492, 406);
    this.Controls.Add(this.pictureBox6);
    this.Controls.Add(this.pictureBox5);
    this.Controls.Add(this.pictureBox4);
    this.Controls.Add(this.pictureBox3);
    this.Controls.Add(this.pictureBox2);
    this.Controls.Add(this.pictureBox1);
    this.Controls.Add(this.button5);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    { } private void button5_Click(object sender, System.EventArgs e)
    {
    int iPB = 0;
    foreach(System.Windows.Forms.Control c in this.Controls)
    {
    if(c is PictureBox)
    {
    iPB++;
    }
    }
    PictureBox[] pbs = new PictureBox[iPB] ;
    iPB = 0;
    foreach(System.Windows.Forms.Control c in this.Controls)
    {
    if(c is PictureBox)
    {
    pbs[iPB] = (PictureBox)c;
    iPB++;
    }
    }
    }
    }
    }
      

  3.   

    关键代码://测试通过
    private void button5_Click(object sender, System.EventArgs e)
    {
    int iPB = 0;
    foreach(System.Windows.Forms.Control c in this.Controls)
    {
    if(c is PictureBox)
    {
    iPB++;
    }
    }
    PictureBox[] pbs = new PictureBox[iPB] ;
    iPB = 0;
    foreach(System.Windows.Forms.Control c in this.Controls)
    {
    if(c is PictureBox)
    {
    pbs[iPB] = (PictureBox)c;
    iPB++;
    }
    }
    }
      

  4.   

    for(int i = 0; i < this.Controls.Count; i++)
    {
    if(this.Controls[i] is PictureBox)
    //加入数组
    }
      

  5.   

    不如把数组修改成数组列表,那样方便些.
    private void button5_Click(object sender, System.EventArgs e)
    {
        ArrayList pics=new ArrayList();
        foreach(System.Windows.Forms.Control c in this.Controls)
        {
    if(c is PictureBox)
        pics.Add(c);
        }
    }