遍历页面中所有的picturebox,取得picturebox的location.x的最大值和最小值,新人,急!3Q!

解决方案 »

  1.   

    foreach 所有controls,当类型为picturebox是取x
      

  2.   

    知道foreach
    foreach (Control c in this.Controls) 

    if (c.GetType() == typeof(System.Windows.Forms.PictureBox)) 
    {????????}

    }
    ?????????怎么写
      

  3.   

    oreach (Control c in this.Controls)
                {
                    if (c.GetType() == typeof(Picturebox))
                    {
                        Picturebox lb = c as Picturebox;
                        MessageBox.Show(lb.Location.X.ToString());
     
                    }
      

  4.   

    foreach (Control c in this.Controls) 
                { 
                    if (c.GetType() == typeof(Picturebox)) 
                    { 
                        Picturebox lb = (PictureBox)c; 
                        MessageBox.Show(lb.Location.X.ToString() + lb.Location.Y.ToString()); 
      
                    }
      

  5.   

    foreach (Control c in this.Controls)   
    {   
       if (c is PictureBox)   
       {   
          Picturebox lb = c as PictureBox;   
          MessageBox.Show(lb.Location.X.ToString() + lb.Location.Y.ToString());   
       }
    }
    这样比较好看吧
      

  6.   

                int max_x = 0, min_x = int.MaxValue; //Or: min_x = this.WIdth;            foreach (Control c in this.Controls)
                {
                    if (c is PictureBox)
                    {
                        PictureBox lb = c as PictureBox;
                        if (lb.Location.X > max_x) max_x = lb.Location.X;
                        if (lb.Location.X < min_x) min_x = lb.Location.X;
                    }
                }
                MessageBox.Show(String.Format("max:{0} min:{1}", max_x, min_x));
      

  7.   

    楼主是要最大和最小值吧。。
    int max=0,min=0;
    foreach (Control c in this.Controls)   
                {   
                    if (c.GetType() == typeof(Picturebox))   
                    {   
                        if(c.Location.X > max)
                           max = c.Location.X;
                        else if(c.Location.X < min)
                           min = c.Location.X;  
                    }
                }
    MessageBox.Show("最大值" + max.ToString() + "最小值" + min.ToString());   
      

  8.   

    你那样不对了 有可能最小值永远是0或最大值永远是0
    我那个写的也不一定保证准确
    准确的应该是
    int max = int.maxvalue, min = int.minvalue
      

  9.   

    不好意思写反了
    应该是int max = int.minvalue, min = int.maxvalue;