关于C#编写读心术的问题

qin1826681768 2012-04-04 04:21:36
现在可以把图片和button按钮显示在窗体上了,但是点击按钮只能实现一次加载,再点击图片不能更新,代码如下,请高手帮忙解决。谢谢各位!!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _111
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Button myButton = new Button();
myButton.Text = "重试";
myButton.Height = 40;
myButton.Width = 60;
myButton.Top = 15;
myButton.Left = 100;
myButton.Click += new EventHandler(myButton_Click);
this.Controls.Add(myButton);
//Random myRandom = new Random();
//int top = 60, left = 60;
//int top1 = 100, left1 = 75;
//Label[] myLabel = new Label[10];
//int s = 1;
//PictureBox[] myPicturebox = new PictureBox[10];
//for (int i = 0; i < 10; i++)
//{
// for (int j = 0; j < 9; j++)
// {
// string str = s.ToString();
// myLabel[j] = new Label();//初始化类的新实例
// myLabel[j].Text = str;
// myLabel[j].Width = 20;
// myLabel[j].Height = 20;
// myLabel[j].Top = top1;
// myLabel[j].Left = left1;
// this.Controls.Add(myLabel[j]);
// left1 += 60;
// myPicturebox[j] = new PictureBox();//初始化picturebox的新实例
// myPicturebox[j].Image = System.Drawing.Bitmap.FromFile(@"icon\" + myRandom.Next(1, 40) + ".bmp");
// myPicturebox[j].Width = 40;
// myPicturebox[j].Height = 40;
// myPicturebox[j].Top = top;
// myPicturebox[j].Left = left;
// this.Controls.Add(myPicturebox[j]);
// left += 60;
// s = s + 1;
// }
// top1 += 60;
// left1 = 75;
// top += 60;
// left = 60;

//}

}
private void myButton_Click(object sender,EventArgs e)
{
Random myRandom = new Random();
int top = 60, left = 60;
int top1 = 100, left1 = 75;
Label[] myLabel = new Label[10];
int s = 1;
PictureBox[] myPicturebox = new PictureBox[10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
myPicturebox[j] = new PictureBox();//初始化picturebox的新实例
myPicturebox[j].Image = System.Drawing.Bitmap.FromFile(@"icon\" + myRandom.Next(1, 40) + ".bmp");
myPicturebox[j].Width = 40;
myPicturebox[j].Height = 40;
myPicturebox[j].Top = top;
myPicturebox[j].Left = left;
this.Controls.Add(myPicturebox[j]);
left += 60;
string str = s.ToString();
myLabel[j] = new Label();//初始化类的新实例
myLabel[j].Text = str;
myLabel[j].Width = 40;
myLabel[j].Height = 20;
myLabel[j].Top = top1;
myLabel[j].Left = left1;
this.Controls.Add(myLabel[j]);
left1 += 60;
s = s + 1;
}
top1 += 60;
left1 = 75;
top += 60;
left = 60;

}
}


}
}




...全文
189 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
orochiheart 2012-04-04
  • 打赏
  • 举报
回复
只new一次Random是使用原则 详见MSDN
orochiheart 2012-04-04
  • 打赏
  • 举报
回复
Random myRandom = new Random(); 这个函数不应该放在按钮内部 应该声明在外部,这个是随机数的原则 否则可能出现随机数重复的问题
dyx2010 2012-04-04
  • 打赏
  • 举报
回复
好像是旧的控件没有被新的覆盖 你添加个button清除那些控件
这样可以重新加载图片

private void button1_Clicked(object sender, EventArgs e)
{

this.Controls.Clear();
this.Controls.Add(mybutton);)//这为新的控件button
this.Controls.Add(button1);
}
private void Form1_Load(object sender, EventArgs e)
{
mybutton.Click += new EventHandler(mybutton_Clicked);
button1.Click += new EventHandler(button1_Click);
this.Controls.Add(mybutton);
}
wy811007 2012-04-04
  • 打赏
  • 举报
回复
再次点击重新load方法啊 试试

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧