1

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: http://i49.tinypic.com/11r7zw8.png

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • One way to do this is to use use a public property in Login form for username that you can access in other form. – Adil Jul 16 '12 at 05:00

4 Answers4

2

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.

Artiom
  • 7,694
  • 3
  • 38
  • 45
  • depends on how do you show 3rd form. Show us some internal logic. But generally it should be like that (I suppose): If authorization was successfull you are opening welcome dialog. Than you open 3rd form, right? Create there a property called UserName (or create a constructor with username parameter). May be there're some nuances I don't know. Let me know to be more specific in this case – Artiom Jul 16 '12 at 05:09
  • I tried this. It doesn't have any error but the value is not showing up. http://msdn.microsoft.com/en-us/library/f6525896(v=vs.90).aspx – Cant Handle Me Babe Jul 16 '12 at 05:56
  • It's bad practice to set public modifiers for controls. Often it means problems in your architecture. As I said use property or constructor for this case. Here is an example I made for you. [Download](http://dl.dropbox.com/u/62983561/Pro/delWin.7z) – Artiom Jul 16 '12 at 06:09
  • @Artiom Thank you so much. Does this work with two or more values to be passed as well? – Cant Handle Me Babe Jul 16 '12 at 21:42
  • @CantHandleMeBabe sure, it's up to your imagination. – Artiom Jul 17 '12 at 04:19
0

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

}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
0

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.

Adil
  • 3,248
  • 1
  • 22
  • 27
  • Just found a relatively similar post as well http://stackoverflow.com/questions/3552169/sharing-a-variable-between-two-winforms – Adil Jul 16 '12 at 05:06
  • How do I pass it to another form? – Cant Handle Me Babe Jul 16 '12 at 05:15
  • 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. – Adil Jul 16 '12 at 05:37
0

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

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
  • public controls is a bad prarice. It's better to avoid it. By the way - there is no reason to do that – Artiom Jul 16 '12 at 06:17