I am attempting to write a Winforms application that interfaces with an SQLite server for a class. One requirement is for users to be able to make an account, so I need to remove the login control and replace it with the actual app control. I figured that the best solution would be to make an event, but I cannot for the life of me get it to work. I’m not sure how to use delegates, but from my understanding I don’t strictly need a delegate for this purpose.
The code here is meant to be setting up an event that is called every time the user authentication goes through correctly. For now it just runs as long as any text at all is put inside of the username and password textboxes.
public partial class LoginControl : UserControl
{
public LoginControl()
{
InitializeComponent();
}
//event and bool for handling the login process
String exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string path = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
private bool isTrue = false;
public EventHandler boolIsChanged;
public void OnChange()
{
boolIsChanged?.Invoke(this, EventArgs.Empty);
}
private void btn_SubmitLogin_Click(object sender, EventArgs e)
{
// Example logic to handle login submission
string password = txtbx_Password.Text;
string username = txtbx_Username.Text;
// Perform your authentication logic here
bool loginSuccessful = AuthenticateUser(username, password);
if (loginSuccessful)
{
MessageBox.Show("Login successful!");
OnChange();
}
else
{
MessageBox.Show("Login failed. Please try again.");
}
}
The logic is (to the best of my knowledge) not called here, and is instead called from the Form directly. The code in the Form is this:
private LoginControl loginControl;
private AddFilesControl addFilesControl;
public Form1()
{
InitializeComponent();
loginControl = new LoginControl();
loginControl.Dock = DockStyle.Fill;
this.Controls.Add(loginControl);
// Initialize AddFilesControl
addFilesControl = new AddFilesControl();
addFilesControl.Dock = DockStyle.Fill;
addFilesControl.Visible = false; // Start with AddFilesControl hidden
this.Controls.Add(addFilesControl);
}
public void LoginControl_LoginSuccessChanged()
{
loginControl.boolIsChanged += (s, args) =>
{
MessageBox.Show("Bool Changed I think");
};
}
The logic for what happens is in LoginControl_LoginSuccessChanged(), for now the placeholder is just showing a MessageBox. This functions purpose right now is solely to make sure that the event is being called properly. The MessageBox of course doesn’t show up because LoginControl_LoginSuccessChanged is never called or referenced. I know that I need to make sure this function is called properly, but I’m unsure of how or where to do that. I was under the impression that it would just go when the event activated, but now I’m not so sure about that. I believe that I can’t put the logic for what happens in Form1 inside of the loginControl, so putting the logic inside of that class isn’t an option for me.
BenClites is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
- Your
public EventHandler boolIsChanged;
line is definition of delegate field, not an event.
You should usepublic event EventHandler boolIsChanged;
to make an event. - Your code example is incomplete to replicate your problem.
Form1.LoginControl_LoginSuccessChanged()
is not called.
Method name suggests that it’s meant to be an event handler, but neither it doesn’t followEventHandler
delegate convention, nor it doesn’t subscribe into any visible events. - If
Form1.LoginControl_LoginSuccessChanged()
is called in your code but not included in your question, It can be an order of execution issue.
Form1.LoginControl_LoginSuccessChanged()
should be invoked BEFORELoginControl.OnChange()
to make your “meant-to-be-anonymous-event-handler” properly invoked. - And I think subscribing an event on potentially-being-called-multiple-times event handler without check is a bad idea. It would cause duplicate subscription.