Home > Net >  Changing a string variable from another Form in Visual Studio (c#)
Changing a string variable from another Form in Visual Studio (c#)

Time:05-26

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)
        {

        }
    }
}

CodePudding user response:

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

CodePudding user response:

make Password variable static

like this:

public static string Password = "123";
  • Related