Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

winforms - How do I pass an instance of a class back through an Event Handler in C#

So I am just getting my head around how Event Handlers work.

I have a WinForms app which has a panel control on the main form. I fill the panel with a login page. The login page will search the DB for the user and check the password is correct when the user clicks the login button. Providing the login details are correct, it then triggers the event handler to tell the main form the login was successful. It should then store the Users details in the main form ready for when the panel is filled with the next control. When the user logs in, it creates an instance of a class called User. How can I pass that User back to the main form so it knows which user is currently logged in?

I hope this makes sense.

This is in the ctrl:

public partial class ctrlLoginPage : UserControl
{
    public event EventHandler RegisterButtonClicked;
    public event EventHandler LoginButtonClicked;
    public User activeUser = new User();
    public ctrlLoginPage()
    {
        InitializeComponent();
    }

    private void btnRegister_Click(object sender, EventArgs e)
    {
        RegisterButtonClicked(this, e);
    }

    public void btnLogon_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "server=DESKTOP-05NVBR2\SQLEXPRESS;database=GymAppPrototype;UID=sa;password=Mabg123";
        con.Open();

        SqlDataReader dataReader = null;
        SqlCommand myCommand = new SqlCommand("select * from Customer where Email ='" + txtEmail.Text + "';", con);
        dataReader = myCommand.ExecuteReader();
        while (dataReader.Read())
        {
            activeUser.forename = dataReader["Forename"].ToString();
            activeUser.surname = dataReader["Surname"].ToString();
            activeUser.email = dataReader["Email"].ToString();
            activeUser.password = dataReader["Password"].ToString();
            activeUser.mobile = dataReader["MobileNumber"].ToString();
        }
        con.Close();
        if (txtPassword.Text == activeUser.password)
        {
            MessageBox.Show("Login Successful");
            LoginButtonClicked(this, e);
        }
        else
        {
            MessageBox.Show("Login Failed");
        }
        
    }
   
}
question from:https://stackoverflow.com/questions/65882614/how-do-i-pass-an-instance-of-a-class-back-through-an-event-handler-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It all depends on the way you design your classes and their integration but given the example above you can use a custom EventArgs or better yet a custom EventHandler.

Don't pass 'e' to the 'LoginButtonClicked' event. Instead use custom event args class containing logged in user data. For that you need to define your event handler a tab bit different though:

event EventHandler<MyEventArgs> LoginButtonClicked;

Then define your event args class:

public class MyEventArgs : EventArgs{
public MyEventArgs(User activeUser) {
    ActiveUser = activeUser;
}
  public User ActiveUser { get; }
}

Now you can pass a new instance of the new class instead of 'e':

LoginButtonClicked(this, new MyEventArgs(activeUser));

The subscriber of the event can then use e.ActiveUser to access the logged in user info, for example:

myLoginPanel.LoginButtonClicked += (s, e) => { /* do something with e.ActiveUser */ };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...