首先新建一个原始窗体,如下:
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 WindowsFormsApplication11{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void tsSave_Click(object sender, EventArgs e) { if (rdb02.Checked) { FrmPwd f = new FrmPwd(); f.FormClosed += new FormClosedEventHandler(f_FormClosed); //事件通知 f.ShowDialog(); } else { test(); } } void test() { MessageBox.Show("test"); } void f_FormClosed(object sender, FormClosedEventArgs e) { if ((sender as FrmPwd).isPASS)//isPASS 为接收密码输入的那个窗体里面的定义的全局变量,sender 为触发这个事件的参数 { test(); } else { MessageBox.Show("密码错误"); } } }}
密码接收的窗体:
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 WindowsFormsApplication11{ public partial class FrmPwd : Form { public bool isPASS = false; //如果密码正确,则赋值为true,默认为false public FrmPwd() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string strPwd = this.textBox1.Text.Trim(); if (strPwd == "12345") { isPASS = true; } this.Hide(); this.Close(); } }}
需要校验密码的情况如下:
不需要校验密码的情况如下,直接执行对应的函数: