知道foreach foreach (Control c in this.Controls) { if (c.GetType() == typeof(System.Windows.Forms.PictureBox)) {????????}
} ?????????怎么写
oreach (Control c in this.Controls) { if (c.GetType() == typeof(Picturebox)) { Picturebox lb = c as Picturebox; MessageBox.Show(lb.Location.X.ToString());
}
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());
}
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()); } } 这样比较好看吧
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));
楼主是要最大和最小值吧。。 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());
你那样不对了 有可能最小值永远是0或最大值永远是0 我那个写的也不一定保证准确 准确的应该是 int max = int.maxvalue, min = int.minvalue
不好意思写反了 应该是int max = int.minvalue, min = int.maxvalue;
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(System.Windows.Forms.PictureBox))
{????????}
}
?????????怎么写
{
if (c.GetType() == typeof(Picturebox))
{
Picturebox lb = c as Picturebox;
MessageBox.Show(lb.Location.X.ToString());
}
{
if (c.GetType() == typeof(Picturebox))
{
Picturebox lb = (PictureBox)c;
MessageBox.Show(lb.Location.X.ToString() + lb.Location.Y.ToString());
}
{
if (c is PictureBox)
{
Picturebox lb = c as PictureBox;
MessageBox.Show(lb.Location.X.ToString() + lb.Location.Y.ToString());
}
}
这样比较好看吧
{
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));
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());
我那个写的也不一定保证准确
准确的应该是
int max = int.maxvalue, min = int.minvalue
应该是int max = int.minvalue, min = int.maxvalue;