首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-04-24 
关于C#编写读心术的问题现在可以把图片和button按钮显示在窗体上了,但是点击按钮只能实现一次加载,再点击

关于C#编写读心术的问题
现在可以把图片和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;

  }
  }
   

  }
}

 
   
   


[解决办法]
再次点击重新load方法啊 试试
[解决办法]
好像是旧的控件没有被新的覆盖 你添加个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);
}
[解决办法]
Random myRandom = new Random(); 这个函数不应该放在按钮内部 应该声明在外部,这个是随机数的原则 否则可能出现随机数重复的问题
[解决办法]
只new一次Random是使用原则 详见MSDN

热点排行