How can i put formclosing event in Button - c#

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....

Related

How to perform Click-Event on a hidden button

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() { }

c# trigger/call events from another control

In VFP9, if i have two buttons (butt1 & butt2) If i want to trigger butt2 click event from butt1 i just do this from butt1 click event butt2.click() and it will call whatever code i have in the procedure/event in butt2. How do i do that in c#?
How do I call event of a certain control from another control like
below
.
private void button2_Click(object sender, EventArgs e)
{
button1.click()
}
You can call the handler directly like this button1_Click(button1, null);
Example Usage:
Button button1;
Button button2;
public Form1()
{
button1.Click += new EventHandler(button1_Click);
button2.Click += new EventHandler(button2_Click);
}
void button2_Click(object sender, EventArgs e)
{
button1_Click(button1, null);
}
public void button1_Click(object sender, EventArgs e)
{
//Action when click occurs
}
Why are you trying to simulate button click, if you just want to execute some logic?
Wrap this logic into separate method, an call it from click handlers you choose:
private Foo1() {}
private Foo2() {}
private button1_Click(object sender, EventArgs e)
{
Foo1();
Foo2();
}
private button2_Click(object sender, EventArgs e)
{
Foo2();
}
just bind the button1 click event to button2 click event in form_load or designer.csbutton2.Click+=new EventHandler(button1_Click);

c# request FormClosing

I have a form to ask for some data. At leaving of an input field (TextBox, DGV) the appropriate _Validating methode or _CellValueChanged methode is called.
If I want to end the program this methode is called, too - before the _FormClosing methode is called.
How can I fin out whether the program branches into the _FormClosing methode or not?
private void txb_Validating(object sender, CancelEventArgs e)
{
doLog("Text 1");
}
private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
doLog("Text 2");
}
private void doLog(string txt)
{
// this is first called at closing...
if( [FormClosing is active] )
{
// Do something
}
else
{
// Do someting different
}
}
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
// ... and this but later
// Write the Logfile
}
How have I to replace [FormClosing is active] to get to the right result?
I tried so
if ( this.FormClosing== true )
and so
this.FormClosing +=new FormClosingEventHandler(MyForm_FormClosing);
and so
FormClosingEventHandler cl = new FormClosingEventHandler(MyForm_FormClosing);
but I always was in a dead end.
This would do the trick:
public class YourForm : Form
{
private bool bIsClosing = false;
public YourClass()
{
InitializeComponent();
this.FormClosing +=
new FormClosingEventHandler(MyForm_FormClosing);
}
private void txb_Validating(object sender, CancelEventArgs e)
{
doLog("Text 1");
}
private void dgv_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
doLog("Text 2");
}
private void doLog(string txt)
{
// this is first called at closing...
if( bIsClosing )
{
// Do something
}
else
{
// Do someting different
}
}
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
bIsClosing = true;
// Write the Logfile
doLog("whatever");
}
}
this.FormClosing is an event that gets triggered once your form starts closing (like clicking the close button), hence the name. You need your application to register that event like so:
this.FormClosing +=new FormClosingEventHandler(MyForm_FormClosing);
This insures that once the FormClosing event gets triggered, your MyForm_FormClosing will be called.
You can create a flag like bool bIsFormClosing and set that flag once your closing function get called.
Edit:
As I understand now by reviewing your answer and your comments, you want to know in your doLog function if the form is closing.
Here is another approach
`
public class YourForm : Form
{
private bool bIsClosing = false;
Private bool bClosingHandled = false;
public YourClass()
{
InitializeComponent();
this.FormClosing +=
new FormClosingEventHandler(MyForm_FormClosing);
}
private void txb_Validating(object sender, CancelEventArgs e)
{
doLog("Text 1");
}
private void dgv_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
doLog("Text 2");
}
private void doLog(string txt)
{
// this is first called at closing...
if( bIsClosing )
{
// Do something
bClosingHandled = true;
this.close();
}
else
{
// Do someting different
}
}
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
If(!bClosingHandled)
{
bIsClosing = true;
e.Cancel = true;
return;
}
// Write the Logfile
doLog("whatever");
}
}`
This approach uses two flags... When you first receive a close event, you set the bIsClosing flag to true, cancels the event and return. Then once your dolog function get called, you force the close operation.

How to override closing control in Windows Form c#

I would like to make X control closing window to hide current display previous form.
In form1 I got:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Tag = this;
form2.Show(this);
Hide();
}
and then when I click X I would like to show previous and hide the current.
You should not override Form.OnFormClosing() for just this. The Form.FormClosing event provides this functionality for you:
void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Prevent the user from closing this window, minimize instead.
if (e.CloseReason == CloseReason.UserClosing)
{
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
}
You can override OnFormClosing to do this.
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown) return;
// DO WHATEVER HERE
}
You have to keep track of your instanced forms.
// Program.cs
public static FormA Instance;
public static void Main()
{
Instance = new FormA();
Instance.Show();
}
Then:
// FormB.cs
private void button1_Click(object sender, EventArgs e)
{
Hide(); // Hide current...
Program.Instance.Show(); // Show previous...
}

Calling one event method from another event method with passing a parameter

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.

Categories

Resources