c#在panel中搜索picturebox控件并且是它们显示图片
代码如下privatevoidbutton2_Click(objectsender,EventArgse){Buttonplayer1=newButton();playe...
代码如下
private void button2_Click(object sender, EventArgs e)
{
Button player1 = new Button();
player1.Location = new Point(30, 30);
player1.Text = "Player1";
panel1.Controls.Add(player1);
Button player2 = new Button();
player1.Location = new Point(30, 30);
player2.Text = "Player2";
panel2.Controls.Add(player2);
PictureBox player1P1 = new PictureBox();
player1P1.Location = new Point(10, 100);
player1.Visible = true;
panel1.Controls.Add(player1P1);
PictureBox player1P2 = new PictureBox();
player1P2.Location = new Point(150, 100);
panel1.Controls.Add(player1P2);
}
private void player1_Click(object sender, EventArgs e)
{
for (int i = 0; i < panel1.Controls.Count; i++)
{
Control ctl = panel1.Controls[i];
{
if (ctl is PictureBox)
{
PictureBox temp = ctl as PictureBox;
Image img = Image.FromFile(@"D:\1.png");
temp.Image = img;
}
}
}
}
我通过button2 对panel1添加了一个按钮player1(player2不用管)和两个picturebox,现在我想通过点击添加的player1按钮来搜索panel1里面的picturebox并且使它们显示图片,现在的问题是调试时button2创建player1以后,点击player1没有反应,两个picturebox不显示任何图片,请问问题出在哪里啊?该怎么解决? 展开
private void button2_Click(object sender, EventArgs e)
{
Button player1 = new Button();
player1.Location = new Point(30, 30);
player1.Text = "Player1";
panel1.Controls.Add(player1);
Button player2 = new Button();
player1.Location = new Point(30, 30);
player2.Text = "Player2";
panel2.Controls.Add(player2);
PictureBox player1P1 = new PictureBox();
player1P1.Location = new Point(10, 100);
player1.Visible = true;
panel1.Controls.Add(player1P1);
PictureBox player1P2 = new PictureBox();
player1P2.Location = new Point(150, 100);
panel1.Controls.Add(player1P2);
}
private void player1_Click(object sender, EventArgs e)
{
for (int i = 0; i < panel1.Controls.Count; i++)
{
Control ctl = panel1.Controls[i];
{
if (ctl is PictureBox)
{
PictureBox temp = ctl as PictureBox;
Image img = Image.FromFile(@"D:\1.png");
temp.Image = img;
}
}
}
}
我通过button2 对panel1添加了一个按钮player1(player2不用管)和两个picturebox,现在我想通过点击添加的player1按钮来搜索panel1里面的picturebox并且使它们显示图片,现在的问题是调试时button2创建player1以后,点击player1没有反应,两个picturebox不显示任何图片,请问问题出在哪里啊?该怎么解决? 展开
展开全部
foreach(Control ctl in panel1)
{
if(ctl is PictureBox)
{
ctl.Image=....;
}
}
这样就可以了
更多追问追答
追问
不行啊,VS2010 提示说错误 1 “(System.Windows.Forms.Panel”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“System.Windows.Forms.Panel”)类型的变量 貌似后面还要加个什么
追答
我用的就是2010,是因为我写错了:
foreach(Control ctl in panel1.Controls)
这样才对
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询