I am trying to call one EventHandler method from another. For example, I would like to link Logout button with form exit, so I have this code:
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Bla, bla?", "Logout", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
and I want to call it from this event:
private void btnLogOut_Click(object sender, EventArgs e)
{
FormMain_FormClosing(null, 'not sure what goes here');
}
Try this:
private void btnLogOut_Click(object sender, EventArgs e)
{
FormMain_FormClosing(null, null);
}
or
private void button1_Click(object sender, EventArgs e)
{
Form1_FormClosing(
null,
new FormClosingEventArgs(CloseReason.UserClosing, false));
}
Even if my answer cover how to link event handlers part, this particular solution leads you to a problem: form won't close clicking button.
Correct solution is
private void button1_Click(object sender, EventArgs e)
{
Close();
}
Handling the event and asking for confirmation are seperate things:
private static bool UserConfirmedToLogout()
{
return MessageBox.Show("Bla, bla?", "Logout", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK;
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = !UserConfirmedToLogout();
}
private void btnLogOut_Click(object sender, EventArgs e)
{
Close();
}
When Close() is called, the FormClosing event is fired too.
Related
In my C# form I have two buttons
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.
Just let the function outside become another function, then you can call function although you hidden the button1.
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.
private void btn_Reload_Click(object sender, EventArgs e)
{
// reload here - maybe you reload all your employees from a datasource
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
// you can use functionality here from a another button and call the
btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
// ....
}
Maybe this solution is better even if you need the functionality at other workflows.
private void btn_Reload_Click(object sender, EventArgs e)
{
Reload();
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
Reload();
Calculate();
}
void Reload() { }
void Calculate() { }
Hello :) I Actually want to ask something
So my aim here is "If I click the button 1, the button 2 will be clicked too "
"Is it possible ? Click a one button so the other button will be clicked ?"
Here is my Code:
Button btn1 = sender as Button;
if (btn1 == button1){
button2.PerformClick();
}
It actually does not work it seems there is something wrong
I suggest extracting methods.
Before:
private void button1_Click(object sender, EventArgs e)
{
Routine 1 code ...
Routine 2 code ... // <- do not copy yourself; copy + paste is evil!
}
private void button2_Click(object sender, EventArgs e)
{
Routine 2 code ...
}
After:
//TODO: think over the right name
private void Routine1()
{
Routine 1 code ...
}
//TODO: think over the right name
private void Routine2()
{
Routine 2 code ...
}
...
private void button1_Click(object sender, EventArgs e)
{
Routine1();
Routine2();
}
private void button2_Click(object sender, EventArgs e)
{
Routine2();
}
It is very simple.
private void button1_Click(object sender, EventArgs e)
{
if ((sender as Button) == button1)
{
button2_Click(sender, e);
}
}
private void button2_Click(object sender, EventArgs e)
{
}
Unless you have a weird reason for doing this, don't!
You should prefer something like this :
void button1_Click(object sender, EventArgs e)
{
DoWork1();
DoWork2();
}
void button2_Click(object sender, EventArgs e)
{
DoWork2();
}
I have a button called btnChallenge. The desired action is when it is clicked, the form cannot be closed.
Here is what I have so far:
public void btnChallenge_Click(object sender, EventArgs e) { }
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// not sure on this if statement
if (btnChallenge.Click)
{
e.Cancel = true;
}
}
You could try it this way:
Declare a private variable inside a form:
private bool _closedFromMyButton;
Then on FormClosing event check that property:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (_closedFromMyButton) // If closed from MyButton, don't do anything. let the form close.
return;
Hide(); // Hide the form (or not, it's up to you; this is useful if application has an icon in the tray)
e.Cancel = true; // Cancel form closing
}
Then on some button click (if desired), put this code to close the form only from that button (or menuitem or toolbar button, etc.):
private void MyButtonClick(object sender, EventArgs e)
{
_closedFromMyButton = true;
Application.Exit(); // Or this.Close() if you just want to close the form.
}
You could define a variable which goes to true when you press the button and check on close if the variable is true
e.g.
private bool btnClicked = false;
public void btnChallenge_Click(object sender, EventArgs e)
{
btnClicked = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(btnClicked)
{
e.Cancel=true;
}
}
You can just call the this.Close() method, this will call the Form1_FormClosing event:
public void btnChallenge_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//code here...
}
If you want to prevent the users by closing your form just after they have pressed some other button, then this code will help you.
private bool close_state=false; // hold the button state
// method to change the close_state by button click
private void Button1(object sender, EventArgs e)
{
close_state = true;
// if required you can toggle the close_state using an if statement
}
// Then on FormClosing event check that property:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (close_state) {// If the button is pressed
e.Cancel = true; // Cancel form closing
}
}
You may implement some other way to close the form....
I have two forms Form1 and Form2.
Inside Form1 I call Form2 and I want to intercept user button click choice. If user on Form2 clicked on Ok or Cancel button, so I try
var editForm = new Form2();
editForm.ShowDialog();
if (editForm.DialogResult == DialogResult.OK)
{
MessageBox.Show("ok btn is pressed!");
editForm.Dispose();
}
else
{
MessageBox.Show("cancel btn is pressed!");
editForm.Dispose();
}
on Form2 I have click events
private void BtnOk_Click(object sender, EventArgs e)
{
_Repository.Create(mydata);
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
Display message is show only on Cancel button and not on Ok. What I'm doing wrong?
Set dialog result before closing.
private void BtnOk_Click(object sender, EventArgs e)
{
_Repository.Create(mydata);
DialogResult = DialogResult.Ok;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
On your Form1 :
using (Form2 editForm = new Form2())
{
editForm.ShowDialog();
if (editForm.DialogResult == DialogResult.OK)
{
MessageBox.Show("ok btn is pressed!");
editForm.Dispose();
}
else
{
MessageBox.Show("cancel btn is pressed!");
editForm.Dispose();
}
}
And on Form2 :
private void BtnOk_Click(object sender, EventArgs e)
{
_Repository.Create(mydata);
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
Here is the short sample code:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(object sender, EventArgs e); //can I call button1 event handler?
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
I wonder if it would be okay to code in the above way?
You can do that - although the code you provide can't be compiled. It should look like this:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(sender, e);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
But for best practice and code readability, you're probably better off doing this, especially as you are not making use of sender and e:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowMessageBox();
}
private void button1_Click(object sender, EventArgs e)
{
ShowMessageBox();
}
private void ShowMessageBox()
{
MessageBox.Show(txtbox1.Text);
}
Yes you can do that; an event handler is just another method.
However it might be worth creating a new method that shows the message box, and having both Click event handlers call that:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void button1_Click(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void ShowTextboxMessage()
{
MessageBox.Show(txtbox1.Text);
}
An event handler is nothing more than a method, so you can call it like any other.