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改编 

相关文章:

  • 2021-10-05
  • 2021-08-13
  • 2021-11-02
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
猜你喜欢
  • 2021-06-01
  • 2021-10-24
  • 2021-12-08
  • 2021-06-06
  • 2021-11-04
  • 2021-06-04
  • 2021-07-10
相关资源
相似解决方案