Getting values from two or more forms C#

可紊 提交于 2019-12-11 10:09:21

问题


Okay, after a successful user login, an alertbox will display the username and then another form will pop up that enables the user to change his password. I want to use the value of the username (from login form) in a conditional statement to be able to change the password.

How do i get this value that came from another form? Thanks.

View image here:


回答1:


What does confuse u? Pass the username to the next form, I don't see the reason why the 3rd form has to know about any other. It would be one more useless dependency.




回答2:


One way to do this is to use use a public property in Login form for username that you can access in other form.

For example:

Inside LoginForm, create a public property

public string UserName { get; private set;}

Fill this property in LoginForm and pass it to other form. At the time of creating object of second form, you can access the data from first form and then pass it to property/constructor of other form.

For example,

FacultyForm facultyForm=new FacultyForm();
facultyForm.UserName = loginForm.UserName; //in this case you have to create a property in other form too. 

This is just one of the way to do this.

Does this solve the problem?

Another way to do this is using Delegate/Events.




回答3:


Make the UserName textbox of login form public, so that you can acess from any other form.

You can change the code in LoginForm.designer.cs




回答4:


You can create a Custom Event that will be raised on succesfull login, you can pass the information with a CustomEventArgs and then use it when you lauch your 2nd Form.

Something like this:

Form1

public partial class Form1 : Form
{

    Logon logon;
    PasswordChange pass;

    public Form1()
    {
        InitializeComponent();

        logon = new Logon();
        logon.raiseLoginEvent += new Logon.LoginSuccesful(logon_raiseLoginEvent);
        logon.ShowDialog();

    }

    void logon_raiseLoginEvent(object sender, LoginEventArgs e)
    {
        pass = new PasswordChange();
        pass.LoginName = e.Login;
        pass.ShowDialog();
    }


}

Logon

public partial class Logon : Form
{
    public delegate void LoginSuccesful(object sender, LoginEventArgs e);
    public event LoginSuccesful raiseLoginEvent;

    public Logon()
    {
        InitializeComponent();
    }


    private void Logon_FormClosing(object sender, FormClosingEventArgs e)
    {
        LoginEventArgs ev = new LoginEventArgs("Admin");
        raiseLoginEvent(this, ev);
    }
}

public class LoginEventArgs : EventArgs
{
    public LoginEventArgs(string s)
    {
        loginName = s;
    }
    private string loginName;
    public string Login
    {
        get { return loginName; }
        set { loginName = value; }
    } 
}

PasswordChange

public partial class PasswordChange : Form
{
    public PasswordChange()
    {
        InitializeComponent();
    }

    public string LoginName
    {
        get {return  textBox1.Text; }
        set { textBox1.Text = value; }
    }

}


来源:https://stackoverflow.com/questions/11498308/getting-values-from-two-or-more-forms-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!