将字符串变量从Visual Studio(C#)中的另一个形式更改

发布于 2025-02-02 01:48:44 字数 3571 浏览 3 评论 0原文

我可以得到一些帮助吗?我想在Visual Studio中制作一个应用程序,并且我创建了一个密码重置表单,我想在其中更改密码。但是,如何用Form2的NewPassword字符串从Form1中替换密码字符串变量?

form1:

namespace Aplicatie
{

    public partial class Aplicatie : Form
    {
        public string Password = "123";

        public Aplicatie()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }

        public void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        public void textUsername_TextChanged(object sender, EventArgs e)
        {

        }
        

        public void ButtonLogin_Click(object sender, EventArgs e)
        {
            
            
            if (textUsername.Text == "augnova001")
            {
                if (textPassword.Text == Password)
                {
                    MessageBox.Show("Autentification Succesfull");
                    new Form2().Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("ACCESS DENIED!");
                    MessageBox.Show("Have a great day!");
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("ACCESS DENIED!");
                MessageBox.Show("Have a great day!");
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new PasswordReset().Show();
            this.Hide();
        }

        public void textPassword_TextChanged(object sender, EventArgs e)
        {

        }

        private void unhideButton_Click(object sender, EventArgs e)
        {
            if (textPassword.PasswordChar == '*')
            {
                hideButton.BringToFront();
                textPassword.PasswordChar = '\0';
            }
        }

        private void hideButton_Click(object sender, EventArgs e)
        {
            if (textPassword.PasswordChar ==  '\0')
            {
                unhideButton.BringToFront();
                textPassword.PasswordChar = '*';

            }
        }
    }
}

密码reset表格:

namespace Aplicatie
{    
    public partial class PasswordReset : Form
    {

        public PasswordReset()
        {
            InitializeComponent();
        }

        public void buttonReset_Click(object sender, EventArgs e)
        {
            Aplicatie app = new Aplicatie();
            if (textOldPassword.Text == app.Password)
            {
                if (textNewPassword.Text == textRetypeNewPassword.Text)
                {
                    Pass = textNewPassword.Text;
                    app.Password = Pass;
                    MessageBox.Show("The Password has been changed succesfully!");
                }
                else
                {
                    MessageBox.Show("The Passwords do NOT correspond! Please Try Again... ");
                }
            }
            else
            {
                MessageBox.Show("The old password is not correct! Please Try Again... ");
            }

        }

        public void textOldPassword_TextChanged(object sender, EventArgs e)
        {

        }

        public void textNewPassword_TextChanged(object sender, EventArgs e)
        {

        }

        public void textRetypeNewPassword_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Can I get some help? I wanna make an app in Visual Studio and I've created a password Reset Form in which I want to change the password. However, how can I replace the password string variable from the Form1 with the NewPassword string from Form2?

Form1:

namespace Aplicatie
{

    public partial class Aplicatie : Form
    {
        public string Password = "123";

        public Aplicatie()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }

        public void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        public void textUsername_TextChanged(object sender, EventArgs e)
        {

        }
        

        public void ButtonLogin_Click(object sender, EventArgs e)
        {
            
            
            if (textUsername.Text == "augnova001")
            {
                if (textPassword.Text == Password)
                {
                    MessageBox.Show("Autentification Succesfull");
                    new Form2().Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("ACCESS DENIED!");
                    MessageBox.Show("Have a great day!");
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("ACCESS DENIED!");
                MessageBox.Show("Have a great day!");
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new PasswordReset().Show();
            this.Hide();
        }

        public void textPassword_TextChanged(object sender, EventArgs e)
        {

        }

        private void unhideButton_Click(object sender, EventArgs e)
        {
            if (textPassword.PasswordChar == '*')
            {
                hideButton.BringToFront();
                textPassword.PasswordChar = '\0';
            }
        }

        private void hideButton_Click(object sender, EventArgs e)
        {
            if (textPassword.PasswordChar ==  '\0')
            {
                unhideButton.BringToFront();
                textPassword.PasswordChar = '*';

            }
        }
    }
}

PasswordReset Form:

namespace Aplicatie
{    
    public partial class PasswordReset : Form
    {

        public PasswordReset()
        {
            InitializeComponent();
        }

        public void buttonReset_Click(object sender, EventArgs e)
        {
            Aplicatie app = new Aplicatie();
            if (textOldPassword.Text == app.Password)
            {
                if (textNewPassword.Text == textRetypeNewPassword.Text)
                {
                    Pass = textNewPassword.Text;
                    app.Password = Pass;
                    MessageBox.Show("The Password has been changed succesfully!");
                }
                else
                {
                    MessageBox.Show("The Passwords do NOT correspond! Please Try Again... ");
                }
            }
            else
            {
                MessageBox.Show("The old password is not correct! Please Try Again... ");
            }

        }

        public void textOldPassword_TextChanged(object sender, EventArgs e)
        {

        }

        public void textNewPassword_TextChanged(object sender, EventArgs e)
        {

        }

        public void textRetypeNewPassword_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动次打次papapa 2025-02-09 01:48:44

用密码表格创建事件,然后在密码表单中单击按钮时将其发送到调用表格。

儿童

using System;
using System.Windows.Forms;

namespace WinApp1
{
    public partial class PasswordResetForm : Form
    {
        public delegate void PasswordChanged(string sender);
        public event PasswordChanged OnPasswordChanged;

        public PasswordResetForm()
        {
            InitializeComponent();
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            OnPasswordChanged?.Invoke(PasswordTextBox.Text);
        }
    }
}

呼叫表格

using System;
using System.Windows.Forms;

namespace WinApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ShowResetFormButton_Click(object sender, EventArgs e)
        {
            PasswordResetForm f = new PasswordResetForm();
            f.OnPasswordChanged += OnPasswordChanged;
            
            if (f.ShowDialog() == DialogResult.OK)
            {
                // TODO
            }

            f.OnPasswordChanged -= OnPasswordChanged;
        }

        private void OnPasswordChanged(string sender)
        {
            // sender contains password from PasswordResetForm
        }
    }
}

Create an event in the password form then when a button is clicked in the password form send it to the calling form.

Child form

using System;
using System.Windows.Forms;

namespace WinApp1
{
    public partial class PasswordResetForm : Form
    {
        public delegate void PasswordChanged(string sender);
        public event PasswordChanged OnPasswordChanged;

        public PasswordResetForm()
        {
            InitializeComponent();
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            OnPasswordChanged?.Invoke(PasswordTextBox.Text);
        }
    }
}

Calling form

using System;
using System.Windows.Forms;

namespace WinApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ShowResetFormButton_Click(object sender, EventArgs e)
        {
            PasswordResetForm f = new PasswordResetForm();
            f.OnPasswordChanged += OnPasswordChanged;
            
            if (f.ShowDialog() == DialogResult.OK)
            {
                // TODO
            }

            f.OnPasswordChanged -= OnPasswordChanged;
        }

        private void OnPasswordChanged(string sender)
        {
            // sender contains password from PasswordResetForm
        }
    }
}
顾挽 2025-02-09 01:48:44

使密码变量静态

public static string Password = "123";

make Password variable static

like this:

public static string Password = "123";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文