Event Handlers - Form Load
Hi, I'm trying to re use my Event for the Form Load, when the user clicks a button in the different form, I'd like it to reuse the event Form Load for the main, which calls a lot of the methods to load data. I'm using this current code:
Code:
this.FormClosed += Forms.frmMain.frmMain_Load;
It somewhat works, with this, I'm able to show the MessageBox I have set there for testing purposes, but for some reason (sorry, a bit new at the whole event handling) it won't update my actual controls - my label should be set to something else, but isn't updating. My variable is though, because I'm showing that in the MessageBox.
Re: Event Handlers - Form Load
What is 'Forms'? My guess is that you are actually referring to two different instances of frmMain.
I would suggest that you are making two big mistakes design-wise:
1. Rather than the second form setting its own event handler referring to a method on the main form, it should be the other way around. Most likely it's the main form that creates the second form, so it should be adding the event handler then, from the outside.
2. You should absolutely not have a method named 'frmMain_Load' do anything other than handle the Load event of frmMain. If you want to handle the FormClosed event of a different form then you should create an appropriately named method and use that. If you want to do the same thing on both events then you should put that code into a third, appropriately named event and then call that method from the two event handlers. Doing it the other way will not stop it working but it's very poor design because what your code is telling the reader it's doing and what it's actually doing are two different things.
Sort out those design issues and I'm fairly sure your current problem will disappear.
Re: Event Handlers - Form Load
Alright, I guess my example was a bit rough.
Here's what I'm trying to accomplish, with zero luck.
I have a LOGIN Form and a MAIN Form.
I've declared both of these as Static so I'm able to reference them from any form, with Forms.FORMNAME
My idea is to have Login Form show -> User logins in (Works). I then want to have the user to Log out -> Log Back in, with may be a different Username, I then want it to update my frmMain (Doesn't work).
Here's what I've been using:
Code:
//UNDER MAIN FORM
public void frmMain_Load(object sender, EventArgs e)
{
lblWelcome.Text = "Welcome Back " + GlobalVariables.TeacherName;
classes.RetriveClassCodes();
for (int i = 0; i < GlobalVariables.allClassCode.Count; i++)
{
cmbSelectAClass.Items.Add(GlobalVariables.allClassCode[i]);
}
}
//SWITCH USER - SHOW NEW FORM
private void btnSwitchUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Forms.frmLogin.Show();
}
//UNDER LOGIN FORM:
private void btnLogin_Click(object sender, EventArgs e)
{
if (accounts.LoginVerify(txtUsername.Text, txtPassword.Text))
{
DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
else
{
DialogResult = System.Windows.Forms.DialogResult.None;
MessageBox.Show("Username or Password is incorrect. Please try again.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Re: Event Handlers - Form Load
You don't need anything declared static. You just need two regular old forms. You don't even actually need to handle any events of the login form. The main form should be creating the login form and displaying it by calling its ShowDialog method. When ShowDialog returns you know the login form has closed, so you go ahead and do whatever's appropriate. It should look something like this:
csharp Code:
private void MainForm_Load(object sender, EventArgs e)
{
this.Initialise();
}
private void logoutButton_Click(object sender, EventArgs e)
{
using (var dialogue = new LoginForm())
{
if (dialogue.ShowDialog() == DialogResult.OK)
{
this.Initialise();
}
else
{
this.Close();
}
}
}
private void Initialise()
{
// ...
}
The first login dialogue should be displayed in the Main method and the main form not even created if a successful login is not made.
Re: Event Handlers - Form Load
Hmm, I see what you did there, I like it. Thanks.