简体   繁体   中英

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

So I'm trying to password protect the textbox which adds to my list. As in I input text into the textbox and get a popup asking for the password. Is that possible? Below I will insert snippets of the code.

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);
        }

If I got you right, you need to create a costum form which has a textbox and a button as a separate class, then, you need to create an instance of that class and call a .showDialog() method on it then the user can only enter something into the dialog (like a MessageBox). Afterwards you will need to get the entered password from your class and evauate if the password is correct (I assume you just want an "easy" protection and not a encrypted one). The easiest solution that came into my mind is to pass your password to the other class and then check there if your password is correct and return a DialogResult which you just need to evauate. Somethig like this (for your method 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();

            }

This would be the code for the other WinForm-class:

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;
            }
        }
    }

I will let you do the work of expanding this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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