繁体   English   中英

如何使用密码保护文本框,以保护Winform C#中的输入

[英]how to password protect a textbox, to secure input in winform c#

因此,我试图用密码保护添加到列表中的文本框。 正如我在文本框中输入文本,然后弹出一个对话框,要求输入密码。 那可能吗? 在下面,我将插入代码片段。

private void button6_Click_1(object sender, EventArgs e)
        {

           BlockList.Add (textBox2.Text );              // adds url to block list


        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            BlockList.Remove(textBox2.Text);
        }

如果我理解正确,则需要创建一个Costum表单,其中包含一个文本框和一个按钮作为单独的类,然后,您需要创建该类的实例并在其上调用.showDialog()方法,然后用户可以只在对话框中输入一些内容(例如MessageBox)。 之后,您将需要从班级获得输入的密码,并确定密码是否正确(我假设您只是想要一种“简单”的保护,而不是加密的保护)。 我想到的最简单的解决方案是将您的密码传递给另一个类,然后在那里检查您的密码是否正确,然后返回一个DialogResult,您只需对其进行评估即可。 这样的东西(对于您的方法button_6_Click_1()):

const string password = "123456789";    //just an example password

            string url = textBox1.Text;

            // Get if the user entered the right password
            GetPass pass = new GetPass(password);

            // Check this with a dialog result
            DialogResult result = pass.ShowDialog();

            if (result == DialogResult.OK)
            {
                    BlockList.Add(url);
                    MessageBox.Show("Added " + url + " to blocklist.");
                    textBox1.Clear();

            }

这将是另一个WinForm类的代码:

public partial class GetPass : Form
    {
        // Use a texBox called textBox1 and a button called btn_confirm
        private string refPassword;

        public GetPass(string password)
        {
            InitializeComponent();
            refPassword = password;
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            string password = textBox1.Text;
            if (password.CompareTo(refPassword) == 0)
            {
                this.DialogResult = DialogResult.OK;
            }
        }
    }

我将让您完成扩展工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM