C# 操作的时候接收用户输入密码进行确认

偏执的太偏执、 2022-03-27 07:38 319阅读 0赞

首先新建一个原始窗体,如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication11
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. private void tsSave_Click(object sender, EventArgs e)
  18. {
  19. if (rdb02.Checked)
  20. {
  21. FrmPwd f = new FrmPwd();
  22. f.FormClosed += new FormClosedEventHandler(f_FormClosed); //事件通知
  23. f.ShowDialog();
  24. }
  25. else
  26. {
  27. test();
  28. }
  29. }
  30. void test()
  31. {
  32. MessageBox.Show("test");
  33. }
  34. void f_FormClosed(object sender, FormClosedEventArgs e)
  35. {
  36. if ((sender as FrmPwd).isPASS)//isPASS 为接收密码输入的那个窗体里面的定义的全局变量,sender 为触发这个事件的参数
  37. {
  38. test();
  39. }
  40. else
  41. {
  42. MessageBox.Show("密码错误");
  43. }
  44. }
  45. }
  46. }

密码接收的窗体:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication11
  10. {
  11. public partial class FrmPwd : Form
  12. {
  13. public bool isPASS = false; //如果密码正确,则赋值为true,默认为false
  14. public FrmPwd()
  15. {
  16. InitializeComponent();
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. string strPwd = this.textBox1.Text.Trim();
  21. if (strPwd == "12345")
  22. {
  23. isPASS = true;
  24. }
  25. this.Hide();
  26. this.Close();
  27. }
  28. }
  29. }

需要校验密码的情况如下:

不需要校验密码的情况如下,直接执行对应的函数:

发表评论

表情:
评论列表 (有 0 条评论,319人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C语言模拟用户输入密码

    C语言模拟用户输入密码 题目://3.编写代码模拟三次密码输入的场景。最多能输入三次密码,密码正确,提示“登录成功”, 密码错误,可以重新输入,最多输入三次。三次均错,则