首先拖入一个label控件和一个button控件,将控件的text属性更改
将不想被点击的butto的MouseEnter事件写入代码,如下:
将想被点击的button的click事件写入代码:
如果想不点击按钮不允许退出时可以将FormClosing事件写入代码:
结果
完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_MouseEnter(object sender, EventArgs e)
{
Random r = new Random() ;
int x = r.Next(300);
int y = r.Next(300);
button2.Location = new Point(x, y);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("我也爱你");
MessageBox.Show("骗你的");
this.Dispose();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("想关?不可能");
e.Cancel = true;
}
}
}