Getting values from two or more forms C#

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:

Answers


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.


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.


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


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

}

Need Your Help

Dbf to sql datetime not importing

sql sql-server import dbf dbase

I am converting all of our databses from dbase visual foxpro files over to sql server. the .dbf table that I'm having trouble with has a bunch of character fields and a datetime field. I have creat...

iOS 7 resize issue of UIDatePicker

ios iphone objective-c cocoa-touch ios7

In my application I have to resize UIDatePicker (with mode UIDatePickerModeDateAndTime) and add it in custom view it's working very well in iOS 6 but issue occur in iOS 7.