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 FrmPrint { public partial class Login : Form { public delegate void SetTextValue(); public event SetTextValue SetFormTextValue; Form1 f1; public delegate void ChangeTextHandler(string str); //定义委托 public event ChangeTextHandler ChangeText; //定义事件 public Login() { InitializeComponent(); } public Login(Form1 f1) : this() { this.f1 = f1; if (f1.UserName == "用户名") { textBox2.Text = ""; } else { textBox2.Text = f1.UserName; } } private void Login_Load(object sender, EventArgs e) { button1.Text = "确认"; button2.Text = "注销"; } private void button2_Click(object sender, EventArgs e) { ChangeText("用户名");//执行委托实例 MessageBox.Show("注销成功"); this.Close(); } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "123456") { if (ChangeText != null)//判断事件是否为空 { ChangeText("管理员");//执行委托实例 } this.Close(); } else { MessageBox.Show("请输入管理员密码!"); } } } }