遍历panel中所有的picturebox,用数组记录每一个picturebox的location.x,并且自动排序,就比如Loc[0]就表示picturebox中location.x最小的,依次Loc[7]就表示最大的(最多有8个picturebox),速度!
解决方案 »
- 求解:DataGridView数据导入到Excel问题
- InstallShield X Express v10.0.SP2 制作安装包时想加入安装google工具栏的功能。救命,今晚得有结果,谢谢各位高手!另送50分
- xml
- 程序的可变参数保存的方法(Reg or INI)
- keyDown事件捕捉不到方向键的问题?
- DataTable中的数据查找问题
- 请指教,为什么我的这个数据库连接不能成功???
- 接口异步问题
- 请问谁有c#操作directshow完成视频捕捉和音频不做的例子的!
- RC3的C#语法和Beta2差别大吗?
- 怎么在Form1中派一个进程守护
- 急:ajax工程在global.asax页面中使用出错?
if(control is picturebox)
{
sortedlist.add(control);
}
你试下吧!
怎么让Loc[]与picturebox的location.x 一一对应。说白了就是 遍历出来的location.x的大小是从小到大,那么,Loc[i]也要从小到大与之对应
{
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();
}
}
}
{
if (c.GetType() == typeof(PictureBox))
{
PictureBox lb = c as PictureBox;
ArrayList ass=new ArrayList();
int s = lb.Location.X;
ass.Add(s);
}
}
实现IComparer 接口再实现排序!
foreach (Control c in panel1.Controls)
{
if (c is PictureBox)
{
x.Add(c.Location.X);
}
}
x.Sort();
List<int> indexList=new List<int>();
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is PictureBox))
{
indexList.Add(((PictureBox)ctrl).Location.X);
}
} 实现IComparer 接口再实现排序!
我的目的就是Loc[0]对应1的location.x,Loc[1]对应2的location.x,依次类推
Hashtable hs = new Hashtable();
foreach (Control c in panel1.Controls)
{
if (c is PictureBox)
{
hs[c.Location.X]=c;
}
}
//hashtable會自動排序。
foreach (Control c in panel1.Controls)
{
if (c is PictureBox)
{
hs[c]=c.Location.X;
}
}當然只要使用一個數組也足以滿足,不過處理麻煩一丁點
没用过Hashtable 哈,所以不太熟悉,问一下:Hashtable 是个数组,但是我怎么用到他具体的值呢,比如我想用hs的第二个值,难道写ds[1]么,
不是太懂,hs[x],x 不知道是多少,怎么?我现在就要得到第二个Hashtable 中的数据,怎么写
你要讀取某個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
}
非常感谢,但是比如我想写if(c.location.x==hs///)
具体的应该怎么写,
比如,如果有3个picturebox的话,那么我要让3个picturebox按我的要求重新显示位置(有一定规律)
if(......location.x最小)
{
c.location=new point(//////);
}
else if(.....location.x最大)
{
c.location=new point(//////);
}
else
{
c.location=new poing(///////);
}不知道够明白没有
前面都都已经说的很清楚了吗,LZ到底想做什么,连(int)hs[key]都得出来了,你想怎么处理就怎么处理呀
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 就可以了,给分吧 .嘿嘿......