Catching the close event on a c# form - c#

Hey! I am not trying to push my luck here but I have another c# question.
I have tried every possible event I found using google.
Here is the code:
private void Form1_OnClose()
{
MessageBox.Show("I was closed -2");
}
private void Form1_Exit(object sender, EventArgs evArgs)
{
MessageBox.Show("I was closed -1");
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("I was closed 0");
}
private void Form1_Closed(object sender, EventArgs e)
{
MessageBox.Show("I was closed 1");
}
private void Form1_FormClosed(Object sender, FormClosedEventArgs e)
{
MessageBox.Show("I was closed 2");
}
Not one of these trigger anything when I either do Alt+f4 or click on the X button.
What am I doing wrong here?

You might be missing actual subscription code, which is something along these lines:
this.Closing += Form1_Closing;
Instead, try overriding OnXXX methods - this is the preferred way of doing things.

The error is likely that you aren't wiring the events at the right time. Check your program.cs file. It should look something like this:
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace Test
{
internal class Program
{
private static void Main(string[] args)
{
Form form = new Form2();
form.Closing += form_Closing;
Application.Run(form);
}
private static void form_Closing(object sender, CancelEventArgs e)
{
MessageBox.Show("Closing");
}
}
}
I just ran this and the event fired.

Are these methods actually assigned as event handlers? Go to design mode, select the form, then click the little lightning bolt above the properties window. Then find the event you want (Closing probably) and double click it.

Related

Is it possible to call this event handler in this case? [duplicate]

This question already has answers here:
WinForms: How to programmatically fire an event handler?
(4 answers)
Closed 2 years ago.
I am using C# in WinForms to use a trackbar as follows. At the beginning of the code I define the event handler:
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
And here is the implementation when one scrolls the trackbar:
private void trackBar1_Scroll(object sender, EventArgs e)
{
//do something...
}
So this works, but I need to call the above function from inside another event handler such as:
public void numericUpDown1_TextChanged(object sender, EventArgs e)
{
//what to do here to call trackBar1_Scroll ?
}
What could be done to call trackBar1_Scroll from inside numericUpDown1_TextChanged?
Very often you do not need the sender and e parameters. Therefore just create a parameterless method
private void DoSomething() // Hopefully with a better name
{
// do things ...
}
and then call it inside your event handlers.
private void trackBar1_Scroll(object sender, EventArgs e)
{
DoSomething();
}
public void numericUpDown1_TextChanged(object sender, EventArgs e)
{
DoSomething();
}
If you give the method a descriptive name, your code becomes easier to read. But of course you could as well just call the other event handler. The event handler is just a method after all
public void numericUpDown1_TextChanged(object sender, EventArgs e)
{
trackBar1_ValueChanged(sender, e);
}
Since both of these event handlers have the same signature (the same parameter list and return type), you could declare a single one and attach the same to both controls:
void HandleUpdates(object sender, EventArgs e)
{
// do things...
}
And assign it with
trackBar1.Scroll += HandleUpdates;
numericUpDown1.TextChanged += HandleUpdates;
You can also assign it in the properties window on the events tab. new System.EventHandler(...) is not necessary. C# does it automatically for you.

How to implement button_click event without clicking button [duplicate]

This question already has answers here:
Triggering a button click through code
(6 answers)
Closed 4 years ago.
private void btnSend_Click(object sender, EventArgs e)
{
Dosomething();
}
private void Something(object sender, EventArgs e)
{
btnSend_click( how to write in here?? )
}
I want to implement btnSend_Click in other section(?), but i don't know how to do this. i want to implement this code without using UI.
If you just need to call the method btnSend_Click from the method Something, you can pick any of these:
private void Something(object sender, EventArgs e)
{
btnSend_click(this, EventArgs.Empty);
}
Or
private void Something(object sender, EventArgs e)
{
btnSend_click(sender, e);
}
It is all about naming, that is, giving the methods a good name.
First, you write the underlying method that does the actual work.
public class Someone
{
public void DoSomething()
{
Console.Beep(); //make a noise
}
}
At this point, no UI is involved, no button, no sender, no EventArgs.
Then
Call this method when the button is clicked.
private void btnSend_Click(object sender, EventArgs e)
{
new Someone().DoSomething();
}
Call this method from any other place.
class AnotherOne
{
private void DoAnotherThing()
{
new Someone().DoSomething();
}
}
I don't answer your question on how to call btnSend_click from anywhere by passing faked sender and EventArgs.
Because that is not a good idea to write such calls - before long you will be confused by the names even if the code was written by yourself.

Close application when mouse leaves form

I try to use mouse leave event handler but i can not work it.
private void Form1_MouseLeave(object sender, EventArgs e)
{
this.Close();
}
I don't want to fade. It should be closed immediately when the mouse cursor goes out of the form screen.
To close the whole application use
Environment.Exit(0);
The MouseLeave event will fire if the mouse enters a child control, which is probably not what you want.
Try using a timer:
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private bool mouseEntered = false;
public Form1() {
InitializeComponent();
timer.Tick += timer_Tick;
timer.Start();
}
protected override void OnMouseEnter(EventArgs e) {
mouseEntered = true;
base.OnMouseEnter(e);
}
void timer_Tick(object sender, EventArgs e) {
if (mouseEntered && !this.Bounds.Contains(Cursor.Position)) {
this.Close();
}
}
If you have several forms I prefer Application.Exit().
Form.Close() only works for a single form application.
If there is only one form in that application you are using, then Application.Exit will close the application itself.
If there is one more form that you are navigating to from the main form, then in the new form, use Form2.Close(), this will take you to the back to the main application that you are using.
If you want to close both of them, then first give Form2.Close() and then give Application.Exit(). That would suffice.
I made a little test program to recreate your problem.
namespace OddFormTest
{
using System;
using System.Windows.Forms;
public class OddForm : Form
{
public OddForm()
{
this.Leave += Leaver;
}
[STAThread]
internal static void Main()
{
Application.Run(new OddForm);
}
private void Leave(object sender, EventArgs e)
{
this.Close()
}
}
}
As you infer in the question. The Leave event doesn't fire after you leave your application.

Events convention

I'm making an XNA game and I have a question about the convention for events.
I made a menu which has buttons, those buttons have 3 events naimly: onClick, onMouseEnter and onMouseLeave.
Atm my code looks like this :
public static void PlayonClick(Button sender, EventArgs args)
{
}
public static void PlayonMouseEnter(Button sender, EventArgs args)
{
}
public static void PlayonMouseLeave(Button sender, EventArgs args)
{
}
This code will repeat for every button in the menu.
Now I think it would be better if had 1 event and eventargs will contain what happend (onClick,onMouseLeave,onMouseEnter)
Note: onMouseEnter and onMouseLeave are acttualy the same for every button. So I'm thinking to subscribe all events to 1 method
So, What is they best way to implement this ?
Assuming you're attaching your handlers in code:
button.onClick += PlayonEvent;
button.onMouseEnter += PlayonEvent;
button.onMouseLeave += PlayonEvent;
If attaching in XAML, do the same sort of thing, which is simply to say that all of the events are handled by the same handler. Either way, define such a handler like:
public static void PlayonEvent(Button sender, EventArgs args)
{
// do something based on EventArgs, which can give you an idea of what's going on
}

C# - Changing button image event is not rising

I am making a simple program that changes the image of a button when the user interact with it. When the computer mouse gets inside the button the image highlights, when the mouse clicks it gets darker and when the mouse leaves the button, the image gets norma. To accomplish this behavior I am changing the button image between 3 images, BUT when I click the button and it displays a modal control, e.g. a MessageBox, when it goes back from the MessageBox, the image in the button is still the "mouse inside" image, but the mouse is out of the button already...so I think the button1_MouseLeave event is not rising, but I dont understand why and how to repair it...can you guys help me out on this one?
The code I am using is simple:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace teste1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Clique));
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
}
}
}
The mouse sure doesn't leave, because the focus on the button is stolen before that, so it doesn't receive those messages. You should also listen to the LostFocus event and switch to normal image when the button loses focus.
private void button1_LostFocus(object sender, EventArgs e)
{
button1.Image = (System.Drawing.Image)Properties.Resources.Botao_Del_Normal;
}
Make sure to actually listen to the LostFocus event! Just adding the code won't do a thing.
Note that since the signatures of expected LostFocus and MouseLeave delegates are the same, you could actually listen to both events with the same method (and call it something like button1_MouseLeaveOrLostFocus or anything that suits you)
Please try button1_MouseHover event instead of button1_MouseEnter Event.
I will work.
This is to be expected, as according to your code the image gets reset when you stop clicking, which will be triggered by the window losing focus due to the messageBox.
Delete the MouseUp handler to achieve the behaviour you desire
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "Hello World!";
}
private void button1_MouseHover(object sender, EventArgs e)
{
button1.Text = "Mouse Hover";
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1.Text = "Mouse Down";
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
button1.Text = "Mouse Up";
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Text = "Mouse Leave";
}
}
}
SOLVED:
Ok guys, I figured how to make it work! I had to remove the MouseClick event, and added all the processing to the MouseUp event, so now MouseLeave is called after MouseUp as it should (even with ALT+TAB).
The code now is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace teste1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Clique));
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
}
private void button1_Click(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
// Processing is made here!
}
}
}

Categories