遍历panel中所有的picturebox,用数组记录每一个picturebox的location.x,并且自动排序,就比如Loc[0]就表示picturebox中location.x最小的,依次Loc[7]就表示最大的(最多有8个picturebox),速度!

解决方案 »

  1.   

    遍历panel中的控件
    if(control is picturebox)
    {
      sortedlist.add(control);
    }
    你试下吧!
      

  2.   


    怎么让Loc[]与picturebox的location.x 一一对应。说白了就是 遍历出来的location.x的大小是从小到大,那么,Loc[i]也要从小到大与之对应
      

  3.   

            private void button1_Click(object sender, EventArgs e)
            {
                List<int> loc = new List<int>();
                foreach (Control c in panel1.Controls)
                {
                    if (c.GetType().ToString() == "System.Windows.Forms.PictureBox")
                    {
                        loc.Add(c.Location.X);
                        loc.Sort();
                    }
                }
            }
      

  4.   

     foreach (Control c in this.Controls)
                {
                    if (c.GetType() == typeof(PictureBox))
                    {
                        PictureBox lb = c as PictureBox;
                        ArrayList ass=new ArrayList();
                        int s = lb.Location.X;
                        ass.Add(s);
                     }
                  }
    实现IComparer 接口再实现排序!
                    
      

  5.   

    厄,看明白了。给你个建议,自己写一个排序的算法,反正就8个,不用考虑时间复杂度。注意,在ArrayList中直接保存PictureBox,不要只保存Location.X(往ArrayList里面添加的时候,直接实现排序再插入),否则你怎么知道哪个Location.X对应哪个PictureBox。现在有了顺序(ArrayList的顺序),有了PictureBox(ArrayList的value),还有什么不能做?
      

  6.   

    楼主这个PICTURE一系列问题都提了N次了!
      

  7.   

    System.Collections.Generic.List<int> x = new List<int>();
                foreach (Control c in panel1.Controls)
                {
                    if (c is PictureBox)
                    {
                        x.Add(c.Location.X);
                    }
                }
                x.Sort();
      

  8.   


    List<int> indexList=new List<int>();
     foreach (Control ctrl in panel1.Controls) 
                { 
                    if (ctrl is PictureBox)) 
                    { 
                        indexList.Add(((PictureBox)ctrl).Location.X); 
                     } 
                  } 实现IComparer 接口再实现排序! 
      

  9.   

    樓上所有的,排序之後,你們有辦法找出來哪個項目是對應哪個picturebox嗎?
      

  10.   

    1 2 3 4 5 6 7 8 
    我的目的就是Loc[0]对应1的location.x,Loc[1]对应2的location.x,依次类推
      

  11.   

    不用IComparer 接口能实现么
      

  12.   


                Hashtable hs = new Hashtable();
                foreach (Control c in panel1.Controls)
                {
                    if (c is PictureBox)
                    {
                        hs[c.Location.X]=c;
                    }
                }
                //hashtable會自動排序。
      

  13.   

    根據lz在17樓所述,不需要對location.x排序。更改如下:            Hashtable hs = new Hashtable();
                foreach (Control c in panel1.Controls)
                {
                    if (c is PictureBox)
                    {
                        hs[c]=c.Location.X;
                    }
                }當然只要使用一個數組也足以滿足,不過處理麻煩一丁點
      

  14.   


    没用过Hashtable 哈,所以不太熟悉,问一下:Hashtable 是个数组,但是我怎么用到他具体的值呢,比如我想用hs的第二个值,难道写ds[1]么,
      

  15.   

    我想用hs的第二个值,难道写ds[1]么
      

  16.   

    Hashtable 不是一般的数组,Hashtable里面不是连续存储的,而是一个key对应一个value,所以直接 hs[x]就可以了
      

  17.   


    不是太懂,hs[x],x 不知道是多少,怎么?我现在就要得到第二个Hashtable 中的数据,怎么写
      

  18.   

    c就是picturebox,做為key放入hashtable中,保存的value則是location.x.
    你要讀取某個picturebox的location,衹要調用hs[picturebox]即可。
    也可以這樣枚舉            foreach (object key in hs.Keys)
                {
                    PictureBox pic = key as PictureBox;
                    //pic就是對應的PictureBox
                    int value = (int)hs[key];
                    //value就是存儲進去的Location.X
                }
      

  19.   


    非常感谢,但是比如我想写if(c.location.x==hs///)
    具体的应该怎么写,
      

  20.   

    我的意思是还要继续多所有 panel中的picturebox操作,我需要判断到具体的picturebox对应的情况
    比如,如果有3个picturebox的话,那么我要让3个picturebox按我的要求重新显示位置(有一定规律)
    if(......location.x最小)
    {
      c.location=new point(//////);
    }
    else if(.....location.x最大)
    {
      c.location=new point(//////);
    }
    else
    {
      c.location=new poing(///////);
    }不知道够明白没有
      

  21.   

    --------------
    前面都都已经说的很清楚了吗,LZ到底想做什么,连(int)hs[key]都得出来了,你想怎么处理就怎么处理呀
      

  22.   

    SortedList sl = new SortedList();
                
                foreach (Control c in this.Controls)
                {
                    if (c is Button)
                    {
                        sl[c.Location.X] = c;
                    }
                }
                bool min=true;
                Button minB=null, maxB=null;            foreach (DictionaryEntry de in sl)
                {
                    if (min)
                    {
                        minB = (de.Value as Button);
                        min = false;
                    }                maxB = (de.Value as Button);
                    //MessageBox.Show(de.Value.ToString(), "Button Caption");               
                }            //MessageBox.Show(maxB.Location.X.ToString(), "Button Caption");            foreach (Control c in this.Controls)
                {
                    if (c ==minB)
                    {
                        MessageBox.Show(minB.Location.X.ToString(), "Button Caption");
                    }
                    else if (c == maxB)
                    {
                        MessageBox.Show(maxB.Location.X.ToString(), "Button Caption");
                    }
                    else
                    {
                    }
                }
    测试通过...把里面的Button换成你的PictureBox 就可以了,给分吧 .嘿嘿......
      

  23.   

    minB,maxB 分别代表location.x的最小和最大值的button....