步步紧追哦^_^

Windows Form 应用程序,New 一个Form--New 一个Button并将其Text更名为"Here!^_^", 然后双击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 Cha08Ex01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            this.Controls.Remove(btn);
            btn.Dispose();


            Button button = new Button() { Text = "Here!^_^", Width = 60, Height = 24 };

            Random random = new Random();
            int tmp = random.Next() % this.Width;
            button.Left = tmp > this.ClientRectangle.Width - button.Width ? this.ClientRectangle.Width - button.Width : tmp;
            tmp = random.Next() % this.Height;
            button.Top = tmp > this.ClientRectangle.Height - button.Height ? this.ClientRectangle.Height - button.Height : tmp;

            button.Click += new EventHandler(button1_Click);

            this.Controls.Add(button);

            this.Refresh();
        }

       
    }
}

看看效果吧 嘻嘻!

 里面要特别注意Button随机位置的设定

 

 

参照《C#入门经典》P160 Ch08Ex01改编 

posted @ 2010-07-19 17:32  AnnieBy  阅读(336)  评论(0编辑  收藏  举报